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: BaseFuzzyClusterer

Fuzzy 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_candidates objects 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 share trim of the objects that fit it worst, to make the medoids robust to outliers.

Parameters:
n_clustersint, default=3

Number of clusters \(c\).

metricstr, default=”euclidean”

"precomputed": fit(R) receives the \(N\times N\) dissimilarity matrix (symmetric, zero diagonal, non-negative). Any other value is a metric name of scipy.spatial.distance.pdist() applied to the rows of X; the dissimilarities are used as they are (not squared), since the algorithm does not need squared distances.

n_candidatesint or None, 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.

trimfloat, 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). 0 is 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.

mfloat, default=2.0

Fuzziness exponent \(m>1\).

tolfloat, default=1e-4
max_iterint, default=100
init{“kmeans++”, “random”} or array_like of int, 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_initint, default=1

Random restarts; the run with the lowest \(J\) is kept. The paper recommends several restarts, the result depending on the start.

random_stateint, RandomState instance or None, default=None
verboseint, default=0
Attributes:
medoid_indices_ndarray of shape (n_clusters,)

Row indices of the medoids among the training objects.

cluster_centers_ndarray of shape (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) with metric="precomputed".

dissimilarity_matrix_ndarray of shape (n_samples, n_samples)

The matrix \(\mathbf{R}\) the algorithm ran on.

membership_ndarray of shape (n_samples, n_clusters)
labels_ndarray of shape (n_samples,)
distances_ndarray of shape (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.

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_candidates objects with the largest \(\mu_{ik}\) (linear FCMdd). The iteration stops when the medoids no longer change (the partition is then fixed too) or after max_iter iterations. If two clusters would select the same object, the second takes its best unused candidate, so that the medoids stay distinct. With trim > 0 the 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 with metric="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 of shape (n_samples, n_samples) or (n_samples, n_features)

The dissimilarity matrix when metric="precomputed", otherwise feature vectors.

yIgnored
Returns:
selfobject
predict_proba(X)[source]#

Memberships of (new) objects, from their dissimilarities to the medoids.

Parameters:
Xarray_like of shape (n_new, n_samples) or (n_new, n_features)

With metric="precomputed" the dissimilarities of the new objects to all n_samples training objects, otherwise feature vectors.

Returns:
Undarray of shape (n_new, n_clusters)
transform(X)[source]#

Dissimilarities \(r(\mathbf{x}_k,\mathbf{v}_i)\) of (new) objects to the medoids.

Examples using fuzzyclust.relational.FuzzyCMedoids#

Fuzzy c-medoids on dynamic-time-warping dissimilarities of time series

Fuzzy c-medoids on dynamic-time-warping dissimilarities of time series

Gower’s dissimilarity versus one-hot encoding for mixed data

Gower's dissimilarity versus one-hot encoding for mixed data

Autocorrelation-based fuzzy clustering of time series

Autocorrelation-based fuzzy clustering of time series