fuzzyclust.semisupervised.PairwiseConstrainedFCM#

class fuzzyclust.semisupervised.PairwiseConstrainedFCM(n_clusters=3, alpha=1.0, agglomeration=0.0, agglomeration_decay=10.0, min_cardinality=1.0, m=2.0, tol=0.0001, max_iter=300, init='random', n_init=1, random_state=None, verbose=0)[source]#

Bases: BaseFuzzyClusterer

Fuzzy clustering with pairwise constraints, PCCA (Grira, Crucianu, and Boujemaa).

Knowledge about the data comes as pairs: two samples that must end up in the same cluster (must-link) or in different clusters (cannot-link). Such constraints are far easier to collect than class labels – a user only has to say whether two images belong together – and they need no agreed set of classes. The algorithm adds to the c-means functional a penalty for violated constraints, and (optionally) the competitive agglomeration term of Frigui and Krishnapuram, which lets the number of clusters shrink from an over-estimate to the number the data and the constraints support.

Parameters:
n_clustersint, default=3

Number of clusters \(c\) (the initial, maximal number when agglomeration > 0).

alphafloat or “auto”, default=1.0

Weight \(\alpha\ge0\) of the constraint penalty, in the units of the squared distances. "auto" renews it at every iteration as \(\sum_{ik}\mu_{ik}^2D_{ik}^2/|\mathcal{M}\cup\mathcal{C}|\), so that the few constraints weigh as much as the data. The published rule, \(\alpha = N\sum_{ik}\mu_{ik}^2D_{ik}^2 / \big(|\mathcal{M}\cup\mathcal{C}|\sum_{ik}\mu_{ik}^2\big)\), also divides by the mean squared membership; the two coincide for a crisp partition.

agglomerationfloat, default=0.0

\(\eta_0\) of the competitive agglomeration term; 0 keeps the number of clusters fixed (pure pairwise-constrained FCM).

agglomeration_decayfloat, default=10.0

Time constant \(\tau\) of the schedule \(\eta(l)=\eta_0 e^{-l/\tau}\).

min_cardinalityfloat, default=1.0

A cluster whose fuzzy cardinality \(N_i=\sum_k\mu_{ik}\) falls below this value is discarded (only when agglomeration > 0).

mfloat, default=2.0

The paper’s updates are derived for \(m=2\); other values raise.

tolfloat, default=1e-4
max_iterint, default=300
init{“random”, “kmeans++”} or ndarray, default=”random”
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,)
n_clusters_int

Number of clusters at convergence (n_clusters unless clusters were discarded).

must_link_, cannot_link_ndarray of shape (n_pairs, 2)

The validated constraints.

must_link_satisfied_, cannot_link_satisfied_float

Share of constraints satisfied by the hard partition.

alpha_float

Penalty weight at the last iteration.

cardinalities_ndarray of shape (n_clusters_,)

Fuzzy cardinalities \(N_i\) of the final clusters.

covariances_ndarray of shape (n_clusters_, n_features, n_features)
distances_, objective_, objective_history_, n_iter_

See BaseFuzzyClusterer.

Notes

With the sets \(\mathcal{M}\) of must-link and \(\mathcal{C}\) of cannot-link pairs the objective is

\[J = \sum_{i=1}^{c}\sum_{k=1}^{N}\mu_{ik}^2 D_{ik}^2 + \alpha\Big[\sum_{(k,l)\in\mathcal{M}}\sum_{i=1}^{c}\sum_{j\ne i}\mu_{ik}\mu_{jl} + \sum_{(k,l)\in\mathcal{C}}\sum_{i=1}^{c}\mu_{ik}\mu_{il}\Big] - \beta\sum_{i=1}^{c}N_i^2,\qquad N_i=\sum_{k=1}^{N}\mu_{ik},\]

the bracket being the expected number of violated constraints and the last term the competitive agglomeration bias. The necessary conditions give a membership that is the FCM membership plus two corrections,

\[\mu_{ik} = \frac{1/D_{ik}^2}{\sum_j 1/D_{jk}^2} + \frac{\alpha}{2D_{ik}^2}\big(\bar{C}_k - C_{ik}\big) + \frac{\beta}{D_{ik}^2}\big(N_i - \bar{N}_k\big),\]

where \(C_{ik}=\sum_{l:(k,l)\in\mathcal{M}}(1-\mu_{il})+\sum_{l:(k,l)\in\mathcal{C}}\mu_{il}\) is the violation cost of placing \(k\) in cluster \(i\), and \(\bar C_k\), \(\bar N_k\) are the \(1/D^2_{jk}\)-weighted means of \(C_{jk}\) and \(N_j\) over the clusters. A cluster that violates fewer constraints than average gains membership; a cluster larger than average gains membership when \(\beta>0\). The corrections can push a membership below zero; as in competitive agglomeration, negative values are clipped and the row renormalized. The agglomeration weight follows Frigui and Krishnapuram’s schedule \(\beta(l)=\eta_0e^{-l/\tau}\sum_{ik}\mu_{ik}^2D_{ik}^2/\sum_iN_i^2\), and clusters with \(N_i<\) min_cardinality are removed. The prototypes are the usual FCM means. New samples carry no constraints and receive FCM memberships.

References

[1]

N. Grira, M. Crucianu, N. Boujemaa, “Fuzzy clustering with pairwise constraints for knowledge-driven image categorization”, IEE Proceedings – Vision, Image and Signal Processing 153(3), 299–304, 2006.

[2]

H. Frigui, R. Krishnapuram, “Clustering by competitive agglomeration”, Pattern Recognition 30(7), 1109–1119, 1997.

Examples

>>> from fuzzyclust.semisupervised import PairwiseConstrainedFCM, make_pairwise_constraints
>>> from fuzzyclust.datasets import make_fuzzy_blobs
>>> X, y = make_fuzzy_blobs(random_state=0)
>>> ml, cl = make_pairwise_constraints(y, n_must_link=20, n_cannot_link=20, random_state=0)
>>> pcca = PairwiseConstrainedFCM(n_clusters=3, random_state=0).fit(X, must_link=ml, cannot_link=cl)
>>> pcca.membership_.shape, pcca.must_link_.shape
((300, 3), (20, 2))
fit(X, y=None, must_link=None, cannot_link=None)[source]#

Cluster X under pairwise constraints.

Parameters:
Xarray_like of shape (n_samples, n_features)
yIgnored
must_linksequence of (int, int), optional

Pairs of sample indices that belong to the same cluster.

cannot_linksequence of (int, int), optional

Pairs of sample indices that belong to different clusters.

Returns:
selfobject

Examples using fuzzyclust.semisupervised.PairwiseConstrainedFCM#

Pairwise constraints: must-link, cannot-link and competitive agglomeration

Pairwise constraints: must-link, cannot-link and competitive agglomeration