fuzzyclust.models.SupervisedFuzzyClassifier#

class fuzzyclust.models.SupervisedFuzzyClassifier(n_clusters=3, m=2.0, gamma=0.0, var_floor=1e-06, normalize_density=True, tol=0.0001, max_iter=300, init='kmeans++', n_init=3, random_state=None, verbose=0)[source]#

Bases: ClassifierMixin, BaseFuzzyClusterer

Fuzzy rule-based classifier identified by supervised Gath–Geva type clustering.

Every cluster is a rule with axis-parallel Gaussian antecedents and a class-probability vector as consequent,

\[r_i:\ \text{If } x_1 \text{ is } A_{i1}(x_1) \text{ and } \dots \text{ and } x_n \text{ is } A_{in}(x_n) \text{ then } \hat y = c_1 \text{ with } P(c_1\mid r_i), \dots,\ \hat y = c_C \text{ with } P(c_C\mid r_i)\ [w_i],\]

so a rule may describe several classes and a class may need several rules (the number of rules \(R\) can exceed the number of classes \(C\)). The classifier is a quadratic Bayes classifier whose class-conditional densities are mixtures of axis-parallel Gaussians. The clustering uses the class labels: the distance of a labeled sample to a cluster is the reciprocal of the probability that the cluster generated both the input and the label [1].

Parameters:
n_clustersint, default=3

Number of rules \(R\) (may be larger than the number of classes).

mfloat, default=2.0

Fuzziness exponent.

gammafloat, default=0.0

Variance regularization \(\gamma\in[0,1]\): every antecedent variance is shrunk toward the variance of the feature, \(\sigma^2_{ij}\leftarrow(1-\gamma)\sigma^2_{ij}+\gamma\,\mathrm{var}(x_j)\) (the diagonal analog of the covariance regularization of GG). A small value (0.05–0.2) prevents the collapse of a rule onto a few samples, a known weakness of the exponential distance.

var_floorfloat, default=1e-6

Lower bound of the antecedent variances relative to the feature variances.

normalize_densitybool, default=True

True: the Gaussians of the distance carry their normalizing constants, so \(1/D^2_{ik}\) is the joint probability \(p(\mathbf{x}_k, y_k, r_i)\) (the Gath–Geva distance of eq. (5) and the rule weights of eq. (18) in [1] contain these constants, and for \(m=2\) the clustering is then the EM algorithm of the mixture). False: the constants are dropped during clustering, which is eq. (20) of [1] literally; clusters with small variances are then not favored by the distance. Classification always uses the weights \(w_i\) of eq. (18).

tolfloat, default=1e-4
max_iterint, default=300
init{“random”, “kmeans++”} or ndarray, default=”kmeans++”
n_initint, default=3

Number of random restarts; the run with the largest log-likelihood \(\sum_k\log\sum_i 1/D^2_{ik}\) is kept (ignored when init is an array).

random_stateint, RandomState instance or None, default=None
verboseint, default=0
Attributes:
classes_ndarray of shape (n_classes,)
cluster_centers_ndarray of shape (n_clusters, n_features)

Centers \(v_{ij}\) of the Gaussian antecedent sets.

input_variances_ndarray of shape (n_clusters, n_features)

Variances \(\sigma^2_{ij}\).

class_probs_ndarray of shape (n_clusters, n_classes)

Consequents \(P(c_j\mid r_i)\).

priors_ndarray of shape (n_clusters,)

\(P(r_i)\).

rule_weights_ndarray of shape (n_clusters,)

\(w_i = P(r_i)\prod_j (2\pi\sigma^2_{ij})^{-1/2}\).

log_likelihood_float

\(\sum_k\log\sum_i 1/D^2_{ik}\) of the kept run.

membership_, labels_, distances_, objective_, objective_history_, n_iter_

See BaseFuzzyClusterer (labels_ are cluster indices of the training samples, not classes).

Notes

The distance of \(\mathbf{z}_k=[\mathbf{x}_k^T\,y_k]^T\) to rule \(i\) is

\[\frac{1}{D^2_{ik}} = P(r_i)\prod_{j=1}^{n}\frac{1}{\sqrt{2\pi\sigma^2_{ij}}} \exp\!\Big(-\tfrac12\frac{(x_{jk}-v_{ij})^2}{\sigma^2_{ij}}\Big)\;P(c_j=y_k\mid r_i),\]

the Gath–Geva distance of the inputs multiplied by the probability that the rule describes the class of the sample (eq. 20 of [1], written with the normalizing constants of the Gaussians so that \(1/D^2_{ik}\) is the joint probability \(p(\mathbf{x}_k, y_k, r_i)\); the constants are exactly the rule weights \(w_i\) of the paper). The updates are

\[\mathbf{v}_i=\frac{\sum_k\mu_{ik}^m\mathbf{x}_k}{\sum_k\mu_{ik}^m},\quad \sigma^2_{ij}=\frac{\sum_k\mu_{ik}^m(x_{jk}-v_{ij})^2}{\sum_k\mu_{ik}^m},\quad P(c_j\mid r_i)=\frac{\sum_{k:\,y_k=c_j}\mu_{ik}^m}{\sum_k\mu_{ik}^m},\quad P(r_i)=\frac1N\sum_k\mu_{ik}^m ,\]

followed by the usual membership update. Classification of a new input aggregates the rules with the normalized firing strengths \(\beta_i(\mathbf{x})=w_i\prod_j A_{ij}(x_j)\):

\[P(c_j\mid\mathbf{x}) = \frac{\sum_i \beta_i(\mathbf{x})\,P(c_j\mid r_i)} {\sum_i\beta_i(\mathbf{x})},\qquad \hat y = \arg\max_j P(c_j\mid\mathbf{x}).\]

Hence predict_proba() returns class posteriors (as any scikit-learn classifier), not cluster memberships; the latter are available through cluster_membership().

References

[1] (1,2,3,4)

J. Abonyi, F. Szeifert, “Supervised fuzzy clustering for the identification of fuzzy classifiers”, Pattern Recognition Letters, 24(14), 2195–2207, 2003.

Examples

>>> from fuzzyclust.datasets import load_iris
>>> from fuzzyclust.models import SupervisedFuzzyClassifier
>>> iris = load_iris()
>>> clf = SupervisedFuzzyClassifier(n_clusters=3, random_state=0).fit(iris.data, iris.target)
>>> clf.predict_proba(iris.data).shape
(150, 3)
>>> clf.score(iris.data, iris.target) > 0.9
True
cluster_membership(X, y=None)[source]#

Memberships in the rules (clusters), (n_samples, n_clusters).

Without y: normalized firing strengths \(p(r_i\mid\mathbf{x})\). With y: the class term enters as during fitting, \(p(r_i\mid\mathbf{x}, y)\).

feature_importance(criterion='det')[source]#

Separability \(J_{-j}\) of the rule base when feature \(j\) is left out.

The feature with the largest \(J_{-j}\) is the one whose removal harms the separability least, i.e. the least needed feature; the step-wise elimination of Abonyi and Szeifert (2003) is implemented in select_features_by_separability().

Returns:
J_minusndarray of shape (n_features_in_,)
fit(X, y=None)[source]#

Identify the rules from labeled data.

Parameters:
Xarray_like of shape (n_samples, n_features)
yarray_like of shape (n_samples,)

Class labels (any hashable values); required.

Returns:
selfobject
fit_predict(X, y=None, **kwargs)[source]#

Fit and return the predicted class labels of the training samples.

fit_transform(X, y=None, **fit_params)[source]#

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

Parameters:
Xarray_like of shape (n_samples, n_features)

Input samples.

yarray_like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).

**fit_paramsdict

Additional fit parameters. Pass only if the estimator accepts additional params in its fit method.

Returns:
X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

interclass_separability(features=None, criterion='det')[source]#

Fisher interclass separability of the rules, \(J=\det\mathbf{F}_B/\det\mathbf{F}_W\).

With the rule priors \(P(r_i)\), centers \(\mathbf{v}_i\) and (diagonal) covariances \(\mathbf{F}_i=\operatorname{diag}(\sigma^2_{i1},\dots)\),

\[\mathbf{F}_W=\sum_i P(r_i)\mathbf{F}_i,\qquad \mathbf{F}_B=\sum_i P(r_i)(\mathbf{v}_i-\mathbf{v}_0)(\mathbf{v}_i-\mathbf{v}_0)^T,\qquad \mathbf{v}_0=\sum_i P(r_i)\mathbf{v}_i\]

(eq. 27–28 of Abonyi and Szeifert (2003)). A large \(J\) means well separated, compact rules. features restricts the computation to a subset of the features; the importance of a feature is the value of \(J\) obtained without it (see feature_importance()).

\(\mathbf{F}_B\) has rank at most \(R-1\), so for fewer rules than features its determinant is zero; the product of its \(\min(R-1, n)\) largest eigenvalues (pseudo-determinant) is used in that case, which keeps \(J\) comparable between feature subsets of the same size. With criterion="trace" the classical Fisher criterion \(\operatorname{tr}(\mathbf{F}_W^{-1}\mathbf{F}_B)\) is returned instead.

predict(X)[source]#

Predicted class labels.

predict_proba(X)[source]#

Class posteriors \(P(c_j\mid\mathbf{x})\), shape (n_samples, n_classes).

rule_base(feature_names=None, class_names=None)[source]#

Rule base as a table: rule weight, prior, antecedent sets and class probabilities.

transform(X)[source]#

Antecedent distances \(D_{ik}\) of X to the rules ((n_samples, n_clusters)).

Examples using fuzzyclust.models.SupervisedFuzzyClassifier#

Supervised fuzzy clustering: classifier rules that carry class probabilities

Supervised fuzzy clustering: classifier rules that carry class probabilities