fuzzyclust.cluster.KMedoids#
- class fuzzyclust.cluster.KMedoids(n_clusters=3, medoid='exact', tol=0.0001, max_iter=300, init='random', n_init=1, random_state=None, verbose=0)[source]#
Bases:
BaseFuzzyClustererHard k-medoids clustering (prototypes are data points).
Useful when the mean of a cluster is not a meaningful object, e.g. the samples are discrete states of a system. Two medoid rules are available: the exact medoid of every cluster (the member with minimal total Euclidean distance to the other members, the PAM criterion) or, as in the MATLAB toolbox, the member closest to the cluster mean.
- Parameters:
- n_clusters
int, default=3 - medoid{“exact”, “nearest_mean”}, default=”exact”
Rule used to select the medoid of each cluster.
- tol, max_iter, init, n_init, random_state, verbose
See
BaseFuzzyClusterer.
- n_clusters
- Attributes:
- cluster_centers_
ndarrayofshape(n_clusters,n_features) The medoids (rows of
X).- medoid_indices_
ndarrayofshape(n_clusters,) Row indices of the medoids in the training data.
- membership_, labels_, distances_, objective_, objective_history_, n_iter_
See
BaseFuzzyClusterer.
- cluster_centers_
Notes
Every sample is assigned to its nearest medoid, and the medoids are constrained to the data, \(\mathbf{v}_i\in\mathbf{X}\). The two rules minimize different criteria over the members of each cluster \(A_i\):
"exact"minimizes the sum of (unsquared) distances, \(\sum_{\mathbf{x}_k\in A_i}\|\mathbf{x}_k-\mathbf{v}_i\|\);"nearest_mean"minimizes the sum of squared distances, \(\sum_{\mathbf{x}_k\in A_i}\|\mathbf{x}_k-\mathbf{v}_i\|^2\), because this sum equals \(|A_i|\,\|\mathbf{v}_i-\bar{\mathbf{x}}_{A_i}\|^2\) plus a constant.
In both cases
objective_andobjective_history_report the within-cluster sum of squared distances to the medoids, \(J=\sum_i\sum_{\mathbf{x}_k\in A_i}\|\mathbf{x}_k-\mathbf{v}_i\|^2\).References
[1]Kaufman, P. J. Rousseeuw, “Finding Groups in Data”, Wiley, 1990.
Examples
>>> from fuzzyclust.cluster import KMedoids >>> from fuzzyclust.datasets import make_fuzzy_blobs >>> X, _ = make_fuzzy_blobs(random_state=0) >>> kmed = KMedoids(n_clusters=3, random_state=0).fit(X) >>> kmed.medoid_indices_.shape (3,)