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,BaseFuzzyClustererHard 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_clusters
int, default=3 Number of clusters \(c\).
- tol
float, default=1e-4 Termination tolerance on the change of the (crisp) partition.
- max_iter
int, default=300 - init{“random”, “kmeans++”}
orndarray, default=”random” - n_init
int, default=1 Number of random restarts; the run with the lowest final \(J\) is kept.
- random_state
int,RandomStateinstanceorNone, default=None - verbose
int, default=0
- n_clusters
- Attributes:
- cluster_centers_
ndarrayofshape(n_clusters,n_features) - membership_
ndarrayofshape(n_samples,n_clusters) Crisp partition (one-hot rows).
- labels_
ndarrayofshape(n_samples,) - distances_
ndarrayofshape(n_samples,n_clusters) - objective_
float Within-cluster sum of squares.
- objective_history_
ndarray - n_iter_
int
- cluster_centers_
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,)