Skip to main content

Fuzzy rule-space max-margin classifiers built on scikit-learn.

Project description

fysvm

GitHub Release Build Status codecov

Intrinsically interpretable fuzzy SVM-style classifiers built on scikit-learn.

fysvm trains linear max-margin classifiers over fuzzy rule activations instead of raw feature coordinates. Every learned dimension is a human-readable linguistic rule such as:

IF glucose is high AND bmi is high THEN positive

Installation

uv pip install fysvm==0.0.1

Requires Python ≥ 3.14 and depends only on numpy, scipy, and scikit-learn.

Quickstart

from fysvm import FuzzyRuleSVM
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(
    data.data, data.target, test_size=0.3, random_state=0
)

clf = FuzzyRuleSVM(
    max_rule_length=2,
    max_rules=128,
    penalty="l1",
    feature_names=data.feature_names,
    random_state=0,
)
clf.fit(X_train, y_train)

print(clf.score(X_test, y_test))

How it works

The estimator has three stages:

1. Fuzzy concepts. Each numeric feature is summarized by three linguistic terms — low, medium, high — defined by triangular membership functions anchored at data quantiles. Every sample gets a membership score in [0, 1] for each concept.

2. Rule generation. All conjunctions of up to max_rule_length concepts are enumerated as candidate rules (e.g. glucose is high AND bmi is high). Candidates are scored by discriminative power and coverage; the top max_rules are kept.

3. Max-margin learning. Each sample is mapped to a firing-strength vector φ(x) ∈ [0, 1]^K where entry k is the fuzzy AND of the concepts in rule k. A sparse linear SVM is trained on this activation space:

f(x) = Σ_k β_k · φ_k(x) + b

Explaining predictions

Because the decision function is a weighted sum of fuzzy rule firings, explanations are the model computation — no proxy, no approximation.

Per-sample explanation

explanation = clf.explain(X_test[:1])[0]

Returns the bias, the net margin, and the top-contributing rules sorted by absolute contribution. Each rule has:

  • rule — a human-readable string like IF feature is low THEN class_A
  • firing — how strongly the sample matches the rule antecedent in [0, 1]
  • weight — the learned SVM coefficient β_k
  • contributionfiring × weight, the rule's dollar-value impact on the margin

Contributions sum with the bias to the predicted margin:

assert abs(explanation["margin"]
           - (explanation["bias"] + explanation["net_rule_contribution"])) < 1e-12

Global rule inspection

for item in clf.support_rules():
    print(item["rule"], item["weight"])

Returns rules with non-zero coefficients, sorted by absolute weight.

Fuzzy concept membership

concepts = clf.concept_memberships(X_test[:1])[0]
# {"glucose": {"low": 0.0, "medium": 0.3, "high": 0.7}, ...}

Fuzzy margin violations

violations = clf.fuzzy_violations(X_test, y_test)
# Each item: {"slack": 0.42, "memberships": {"cleanly_classified": 0.58,
#             "borderline": 0.42, "strong_violation": 0.0}}

API Reference

FuzzyRuleSVM (aliased as SparseMaxMarginFuzzyRuleMachine)

Parameter Default Description
C 1.0 Inverse regularization strength
penalty "l1" "l1" or "l2" SVM penalty
max_rule_length 2 Max conjuncts per rule (≤ n_features)
max_rules 256 Max candidate rules kept
min_rule_coverage 0.02 Minimum fuzzy support for a candidate
and_operator "min" "min", "product", or "softmin"
feature_names None Column names for readable rule strings
class_weight None "balanced" or dict for imbalanced classes

See the docstrings for the full parameter list.

Example notebooks

See the examples/ directory:

Citation

If you use fysvm in published work, please cite the accompanying paper:

@misc{davidsen2026fuzzy,
  author = {Davidsen, S. A.},
  title  = {Fuzzy Rule-Space Max-Margin Classifiers},
  year   = {2026}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fysvm-0.0.1.tar.gz (24.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fysvm-0.0.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file fysvm-0.0.1.tar.gz.

File metadata

  • Download URL: fysvm-0.0.1.tar.gz
  • Upload date:
  • Size: 24.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fysvm-0.0.1.tar.gz
Algorithm Hash digest
SHA256 732334bc9eb8c57056166670eeb4f254f4fb94ac3e9cf5671a07137e915d3d81
MD5 a2003c709385de43cf96d897b766a3ac
BLAKE2b-256 d83a319e2786cb34a160fc990aa637c5609c12a51cb3551864f383add49ee10a

See more details on using hashes here.

File details

Details for the file fysvm-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: fysvm-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fysvm-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9e388180c5c2200f63a1d29d7705ede43d4892f5d5cf4ec57eb586440787723c
MD5 0d649baf3119ad34265482a93d7168e5
BLAKE2b-256 e3b532952ed217a15815e4e0ce65dfb04f8950d22a19b82d6c997e8b00d4470b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page