fuzzyclust.relational.FuzzyCMedoids#
- class fuzzyclust.relational.FuzzyCMedoids(n_clusters=3, metric='euclidean', n_candidates=None, trim=0.0, m=2.0, tol=0.0001, max_iter=100, init='kmeans++', n_init=1, random_state=None, verbose=0)[source]#
Bases:
BaseFuzzyClustererFuzzy c-medoids clustering of relational data (Krishnapuram et al., 2001).
The prototypes are medoids – actual objects – and the objective is the membership-weighted sum of the dissimilarities of the objects to their medoids. Because only dissimilarities between objects enter, the algorithm works on any dissimilarity matrix (dynamic-time-warping, Gower, edit distances, web-session dissimilarities of the original paper) without the Euclidean assumption of relational c-means, and the result is a set of representative objects that can be shown and interpreted directly. Two variants are available: the linear FCMdd of the paper, which searches the new medoid of a cluster only among the
n_candidatesobjects with the highest memberships in it, and a trimmed variant (FCTMdd) in the spirit of the paper’s robust RFCMdd, which ignores, for every candidate medoid, the sharetrimof the objects that fit it worst, to make the medoids robust to outliers.- Parameters:
- n_clusters
int, default=3 Number of clusters \(c\).
- metric
str, default=”euclidean” "precomputed":fit(R)receives the \(N\times N\) dissimilarity matrix (symmetric, zero diagonal, non-negative). Any other value is a metric name ofscipy.spatial.distance.pdist()applied to the rows ofX; the dissimilarities are used as they are (not squared), since the algorithm does not need squared distances.- n_candidates
intorNone, default=None None: the medoid of a cluster is searched among all \(N\) objects (\(O(cN^2)\) per iteration). An integer \(p\) restricts the search to the \(p\) objects with the highest memberships in the cluster – the linear-complexity FCMdd of the paper, \(O(cpN)\) per iteration. Small values speed the algorithm up at the risk of a worse local optimum.- trim
float, default=0.0 Share of the objects (
0 <= trim < 1) that are trimmed when the medoids are updated: for every candidate medoid only the \(K=\lceil(1-\text{trim})N\rceil\) smallest weighted dissimilarities \(\mu_{ik}^m r(\mathbf{x}_k,\mathbf{x}_q)\) are summed (fuzzy c-trimmed medoids, FCTMdd).0is plain FCMdd. This is a simpler rule than the RFCMdd of the paper, which ranks the objects by a harmonic-mean-type dissimilarity to all medoids, keeps the best-fitting ones and computes memberships and medoids from them only; here the memberships of all objects are kept.- m
float, default=2.0 Fuzziness exponent \(m>1\).
- tol
float, default=1e-4 - max_iter
int, default=100 - init{“kmeans++”, “random”}
orarray_likeofint, default=”kmeans++” "random"picks \(c\) distinct random objects as initial medoids (the paper’s start),"kmeans++"seeds them by \(D^2\)-weighted sampling on the dissimilarities; an integer array gives the initial medoid indices.- n_init
int, default=1 Random restarts; the run with the lowest \(J\) is kept. The paper recommends several restarts, the result depending on the start.
- random_state
int,RandomStateinstanceorNone, default=None - verbose
int, default=0
- n_clusters
- Attributes:
- medoid_indices_
ndarrayofshape(n_clusters,) Row indices of the medoids among the training objects.
- cluster_centers_
ndarrayofshape(n_clusters,n_features)or(n_clusters,n_samples) The medoids:
X[medoid_indices_]with a feature-based metric, or the rows of the dissimilarity matrix of the medoids (their dissimilarities to all objects) withmetric="precomputed".- dissimilarity_matrix_
ndarrayofshape(n_samples,n_samples) The matrix \(\mathbf{R}\) the algorithm ran on.
- membership_
ndarrayofshape(n_samples,n_clusters) - labels_
ndarrayofshape(n_samples,) - distances_
ndarrayofshape(n_samples,n_clusters) Dissimilarities \(r(\mathbf{x}_k,\mathbf{v}_i)\) of the objects to the medoids (not square-rooted: the dissimilarity itself plays the role of \(D^2_{ik}\);
transform()returns the same).- objective_, objective_history_, n_iter_
See
BaseFuzzyClusterer.
- medoid_indices_
Notes
The objective is
\[J=\sum_{i=1}^{c}\sum_{k=1}^{N}\mu_{ik}^m\,r(\mathbf{x}_k,\mathbf{v}_i),\qquad \mathbf{v}_i\in\{\mathbf{x}_1,\dots,\mathbf{x}_N\},\]minimized by alternating the membership update (the FCM formula with the dissimilarity in place of the squared distance; \(\mu_{ik}=1\) when \(\mathbf{x}_k=\mathbf{v}_i\))
\[\mu_{ik}=\frac{\big(1/r(\mathbf{x}_k,\mathbf{v}_i)\big)^{1/(m-1)}} {\sum_{j=1}^{c}\big(1/r(\mathbf{x}_k,\mathbf{v}_j)\big)^{1/(m-1)}}\]and the medoid update
\[\mathbf{v}_i=\mathbf{x}_q,\qquad q=\argmin_{j\in C_i}\sum_{k=1}^{N}\mu_{ik}^m\,r(\mathbf{x}_k,\mathbf{x}_j),\]where the candidate set \(C_i\) is all objects (full search) or the
n_candidatesobjects with the largest \(\mu_{ik}\) (linear FCMdd). The iteration stops when the medoids no longer change (the partition is then fixed too) or aftermax_iteriterations. If two clusters would select the same object, the second takes its best unused candidate, so that the medoids stay distinct. Withtrim > 0the sum in the medoid update runs over the \(K\) smallest terms only; the objective reported is the same trimmed sum. New objects are assigned from their dissimilarities to the training objects ((n_new, n_samples)matrix withmetric="precomputed", feature vectors otherwise): only the columns of the medoids are used.References
[1]R. Krishnapuram, A. Joshi, O. Nasraoui, L. Yi, “Low-complexity fuzzy relational clustering algorithms for web mining”, IEEE Transactions on Fuzzy Systems 9(4), 595–607, 2001.
[2]L. Kaufman, P. J. Rousseeuw, “Finding Groups in Data: An Introduction to Cluster Analysis”, Wiley, 1990.
Examples
>>> from fuzzyclust.datasets import make_fuzzy_blobs >>> from fuzzyclust.relational import FuzzyCMedoids, pairwise_dissimilarity >>> X, y = make_fuzzy_blobs(random_state=0) >>> R = pairwise_dissimilarity(X) >>> fcmdd = FuzzyCMedoids(n_clusters=3, metric="precomputed", random_state=0).fit(R) >>> fcmdd.medoid_indices_.shape, fcmdd.membership_.shape ((3,), (300, 3))
- fit(X, y=None)[source]#
Cluster the objects.
- Parameters:
- Xarray_like
ofshape(n_samples,n_samples)or(n_samples,n_features) The dissimilarity matrix when
metric="precomputed", otherwise feature vectors.- y
Ignored
- Xarray_like
- Returns:
- self
object
- self
- predict_proba(X)[source]#
Memberships of (new) objects, from their dissimilarities to the medoids.
- Parameters:
- Xarray_like
ofshape(n_new,n_samples)or(n_new,n_features) With
metric="precomputed"the dissimilarities of the new objects to alln_samplestraining objects, otherwise feature vectors.
- Xarray_like
- Returns:
- U
ndarrayofshape(n_new,n_clusters)
- U
Examples using fuzzyclust.relational.FuzzyCMedoids#
Fuzzy c-medoids on dynamic-time-warping dissimilarities of time series
Gower’s dissimilarity versus one-hot encoding for mixed data
Autocorrelation-based fuzzy clustering of time series