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.3.tar.gz (3.6 MB 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.3-py3-none-any.whl (33.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fysvm-0.0.3.tar.gz
  • Upload date:
  • Size: 3.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for fysvm-0.0.3.tar.gz
Algorithm Hash digest
SHA256 45f1e4fe41163c69186bc9ed9c512b7b11754a265e0c58a4481a76e1dba2c544
MD5 3a9081564aacde65318c3b9fdd02f9e1
BLAKE2b-256 ba5bcec29c6a795f160b419da6160f6f0defcd37daee2669b23f963fac69478b

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for fysvm-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 27615f9446bd5863533dbf27e66993a34d1a345a9a318e331b9924338bb17dff
MD5 b44b27059ad782d52ea37c2ef0df340a
BLAKE2b-256 9dc4a2b51427c2d6ddbf34ac4cf4b79d584aab5adc69ecc549dfd0f31c56012b

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