fuzzyclust.semisupervised.SemiSupervisedFCM#

class fuzzyclust.semisupervised.SemiSupervisedFCM(n_clusters=3, alpha=1.0, m=2.0, tol=0.0001, max_iter=300, init='labels', n_init=1, random_state=None, verbose=0)[source]#

Bases: BaseFuzzyClusterer

Fuzzy c-means with partial supervision (Pedrycz and Waletzky, 1997).

A small subset of the samples carries a class label (or a fuzzy label vector). The c-means functional is extended by a term that measures the disagreement between the memberships of the labeled samples and their labels, weighted by \(\alpha\); the unlabeled samples are clustered as usual, and the labels steer the whole partition, because the prototypes are shared. In the scikit-learn convention of the semi-supervised module, y == -1 marks an unlabeled sample.

Parameters:
n_clustersint, default=3

Number of clusters \(c\). Must be at least the number of classes in y; class \(j\) is tied to cluster \(j\) (in the order of classes_), surplus clusters are unsupervised.

alphafloat, default=1.0

Weight \(\alpha\ge0\) of the supervision term. 0 gives plain FCM; the paper suggests \(\alpha\) proportional to \(N/M\) (number of samples over number of labeled samples), so that the labeled minority weighs as much as the unlabeled majority. alpha="auto" sets \(\alpha=N/M\).

mfloat, default=2.0

Fuzziness exponent. The paper’s closed-form update exists for \(m=2\) only, which is the value implemented; another value raises ValueError.

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

"labels" places the prototypes of the supervised clusters at the means of their labeled samples (random prototypes for the surplus clusters and when no labels are given); the other options are those of BaseFuzzyClusterer. Since class \(j\) is tied to cluster \(j\), a random start that puts cluster \(j\) next to another class makes the supervision fight the geometry and ends in a poor local optimum; starting from the labels (or using n_init restarts, which keep the lowest supervised objective) avoids this.

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

Sorted class labels seen in y (without -1).

label_mapping_ndarray of shape (n_clusters,)

Class label tied to every cluster, -1 for unsupervised clusters.

label_membership_ndarray of shape (n_samples, n_clusters)

The label matrix \(f_{ik}\) used (zero rows for unlabeled samples).

labelled_mask_ndarray of shape (n_samples,)

Boolean indicator \(b_k\) of the labeled samples.

alpha_float

The weight actually used.

covariances_ndarray of shape (n_clusters, n_features, n_features)

Fuzzy covariances of the final partition (for plotting).

distances_, objective_, objective_history_, n_iter_

See BaseFuzzyClusterer.

Notes

With the indicator \(b_k\in\{0,1\}\) of the labeled samples and their label memberships \(f_{ik}\) (\(\sum_i f_{ik}=1\) for \(b_k=1\)), the objective is

\[J = \sum_{i=1}^{c}\sum_{k=1}^{N}\mu_{ik}^2 D_{ik}^2 + \alpha\sum_{i=1}^{c}\sum_{k=1}^{N}(\mu_{ik}-f_{ik}b_k)^2 D_{ik}^2 .\]

Its necessary conditions give the membership update

\[\mu_{ik} = \frac{1}{1+\alpha}\left[ \frac{1+\alpha\big(1-b_k\sum_{j=1}^{c}f_{jk}\big)} {\sum_{j=1}^{c} D_{ik}^2/D_{jk}^2} + \alpha f_{ik}b_k\right]\]

and the prototype update

\[\mathbf{v}_i = \frac{\sum_k\big[\mu_{ik}^2+\alpha(\mu_{ik}-f_{ik}b_k)^2\big]\mathbf{x}_k} {\sum_k\big[\mu_{ik}^2+\alpha(\mu_{ik}-f_{ik}b_k)^2\big]} .\]

For an unlabeled sample the bracket reduces to the FCM formula; for a labeled one the membership is a convex combination of the FCM membership and the label, with weight \(\alpha/(1+\alpha)\) on the label. New samples (predict_proba()) carry no label and receive FCM memberships to the supervised prototypes.

References

[1]

W. Pedrycz, J. Waletzky, “Fuzzy clustering with partial supervision”, IEEE Transactions on Systems, Man, and Cybernetics, Part B 27(5), 787–795, 1997.

Examples

>>> import numpy as np
>>> from fuzzyclust.semisupervised import SemiSupervisedFCM, make_partially_labeled_pair
>>> X, y, y_partial = make_partially_labeled_pair(random_state=0)
>>> ssfcm = SemiSupervisedFCM(n_clusters=2, alpha="auto", random_state=0).fit(X, y_partial)
>>> ssfcm.membership_.shape, int((y_partial >= 0).sum()) == int(ssfcm.labelled_mask_.sum())
((300, 2), True)
fit(X, y=None)[source]#

Cluster X steered by the partial labels y.

Parameters:
Xarray_like of shape (n_samples, n_features)
yarray_like of shape (n_samples,) or (n_samples, n_clusters), optional

Class labels with -1 for unlabeled samples, or a matrix of label memberships \(f_{ik}\) whose zero rows mark the unlabeled samples. None means no supervision (plain FCM).

Returns:
selfobject
predict_classes(X)[source]#

Class label of (new) samples: the class tied to the most likely cluster.

Returns -1 where the winning cluster is unsupervised.

score_labels(X, y)[source]#

Fraction of the labeled samples of y (!= -1) whose class is predicted.

Examples using fuzzyclust.semisupervised.SemiSupervisedFCM#

Fuzzy c-means with partial supervision: label fraction and weight

Fuzzy c-means with partial supervision: label fraction and weight