fuzzyclust.cluster.KMeans#

class fuzzyclust.cluster.KMeans(n_clusters=3, tol=0.0001, max_iter=300, init='random', n_init=1, random_state=None, verbose=0)[source]#

Bases: _EuclideanPrototypeMixin, BaseFuzzyClusterer

Hard c-means (k-means) clustering with the fuzzyclust interface.

The algorithm alternates between assigning every sample to its nearest prototype and recomputing each prototype as the mean of its members.

Parameters:
n_clustersint, default=3

Number of clusters \(c\).

tolfloat, default=1e-4

Termination tolerance on the change of the (crisp) partition.

max_iterint, default=300
init{“random”, “kmeans++”} or ndarray, default=”random”
n_initint, default=1

Number of random restarts; the run with the lowest final \(J\) is kept.

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)

Crisp partition (one-hot rows).

labels_ndarray of shape (n_samples,)
distances_ndarray of shape (n_samples, n_clusters)
objective_float

Within-cluster sum of squares.

objective_history_ndarray
n_iter_int

Notes

Minimizes the within-cluster sum of squares

\[J(\mathbf{X};\mathbf{U},\mathbf{V}) = \sum_{i=1}^{c}\sum_{\mathbf{x}_k\in A_i} \|\mathbf{x}_k-\mathbf{v}_i\|^2, \qquad \mathbf{v}_i = \frac{1}{N_i}\sum_{\mathbf{x}_k\in A_i}\mathbf{x}_k .\]

It is the \(m\to 1\) limit of fuzzy c-means. The membership matrix is an element of the hard partitioning space \(M_{hc}\) (\(\mu_{ik}\in\{0,1\}\), \(\sum_i\mu_{ik}=1\)).

References

[1]

J. MacQueen, “Some methods for classification and analysis of multivariate observations”, Proc. 5th Berkeley Symp., 1967.

[2]

B. Balasko, J. Abonyi, B. Feil, “Fuzzy Clustering and Data Analysis Toolbox for use with MATLAB”, 2005.

Examples

>>> from fuzzyclust.cluster import KMeans
>>> from fuzzyclust.datasets import make_fuzzy_blobs
>>> X, y = make_fuzzy_blobs(n_clusters=3, random_state=0)
>>> km = KMeans(n_clusters=3, random_state=0).fit(X)
>>> km.labels_.shape
(300,)