KMeans
- Machine Learning course
- Intermediate
- Lesson 48/112
- 5 min
KMeans is a popular clustering algorithm that divides data into k clusters.
KMeans
KMeans assigns each example to the nearest cluster centre and moves the centres to the mean of the assigned examples; these two steps repeat until the centres stop changing. The goal is to minimise the sum of squared distances of examples to their centres.
km = KMeans(n_clusters=3, n_init=10, random_state=0).fit(X)
print(km.cluster_centers_) # 3 centres
print(km.predict([[0, 0]])) # the cluster of a new pointpredict assigns new examples to the nearest centre; that is why KMeans can be applied to new data.
KMeans's assumptions
KMeans assumes clusters are round, of similar size and similar density. It fails on moon-shaped or nested ring clusters:
from sklearn.datasets import make_moons
Xm, ym = make_moons(200, noise=0.05, random_state=0)
print(adjusted_rand_score(ym, KMeans(2, n_init=10).fit_predict(Xm))) # lowThe number of clusters n_clusters must be given beforehand; you'll see how to choose it in the elbow method lesson.
Practice Find the centres
Train a KMeans(n_clusters=3, n_init=10, random_state=0) on small data with three clear groups. Sort the centres by their first coordinate (c[np.argsort(c[:, 0])]) and print them with np.round(..., 2). Then print that the cluster of [[5, 5]] equals that of [[5.2, 4.9]].
Starter code
import numpy as np
from sklearn.cluster import KMeans
X = np.array([[0, 0], [0, 1], [1, 0], [5, 5], [5, 6], [6, 5], [10, 0], [10, 1], [11, 0]])
# buraya yazSample solution
import numpy as np
from sklearn.cluster import KMeans
X = np.array([[0, 0], [0, 1], [1, 0], [5, 5], [5, 6], [6, 5], [10, 0], [10, 1], [11, 0]])
km = KMeans(n_clusters=3, n_init=10, random_state=0).fit(X)
c = km.cluster_centers_
print(np.round(c[np.argsort(c[:, 0])], 2))
print(km.predict([[5, 5]])[0] == km.predict([[5.2, 4.9]])[0])Expected output
[[ 0.33 0.33] [ 5.33 5.33] [10.33 0.33]] True
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Where is a centre moved in the KMeans update step?
- To the mean of the assigned examples Correct answer
- To the farthest example
- To a random point
- To the origin
Explanation: The point minimising the sum of squared distances is the mean.
2 Train and get the labels in one step.
etiket = KMeans(n_clusters=3, n_init=10).____(X)
etiket = KMeans(n_clusters=3, n_init=10).fit_predict(X)
Explanation: fit_predict is the same as fit + labels_.
3 Match the steps.
| Term | Meaning |
|---|---|
assignment |
To the nearest centre |
update |
Move to the mean |
cluster_centers_ |
Centres |
predict |
Assign a new example |