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:
BaseFuzzyClustererFuzzy 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 == -1marks an unlabeled sample.- Parameters:
- n_clusters
int, 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 ofclasses_), surplus clusters are unsupervised.- alpha
float, default=1.0 Weight \(\alpha\ge0\) of the supervision term.
0gives 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\).- m
float, 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.- tol
float, default=1e-4 - max_iter
int, default=300 - init{“labels”, “random”, “kmeans++”}
orndarray, 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 ofBaseFuzzyClusterer. 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 usingn_initrestarts, which keep the lowest supervised objective) avoids this.- n_init
int, default=1 - random_state
int,RandomStateinstanceorNone, default=None - verbose
int, default=0
- n_clusters
- Attributes:
- cluster_centers_
ndarrayofshape(n_clusters,n_features) - membership_
ndarrayofshape(n_samples,n_clusters) - labels_
ndarrayofshape(n_samples,) - classes_
ndarrayofshape(n_classes,) Sorted class labels seen in
y(without-1).- label_mapping_
ndarrayofshape(n_clusters,) Class label tied to every cluster,
-1for unsupervised clusters.- label_membership_
ndarrayofshape(n_samples,n_clusters) The label matrix \(f_{ik}\) used (zero rows for unlabeled samples).
- labelled_mask_
ndarrayofshape(n_samples,) Boolean indicator \(b_k\) of the labeled samples.
- alpha_
float The weight actually used.
- covariances_
ndarrayofshape(n_clusters,n_features,n_features) Fuzzy covariances of the final partition (for plotting).
- distances_, objective_, objective_history_, n_iter_
See
BaseFuzzyClusterer.
- cluster_centers_
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
Xsteered by the partial labelsy.- Parameters:
- Xarray_like
ofshape(n_samples,n_features) - yarray_like
ofshape(n_samples,)or(n_samples,n_clusters),optional Class labels with
-1for unlabeled samples, or a matrix of label memberships \(f_{ik}\) whose zero rows mark the unlabeled samples.Nonemeans no supervision (plain FCM).
- Xarray_like
- Returns:
- self
object
- self
Examples using fuzzyclust.semisupervised.SemiSupervisedFCM#
Fuzzy c-means with partial supervision: label fraction and weight