AI Hints
Home›Machine Learning Tutorial›Finding Groups
Machine Learning · Lesson 43 of 85

K-Means Clustering

Finding groups without answers.

⏱ 2 min read▶ Video at 1:33:12🎓 Free
Watch this lesson in the Machine Learning courseWatch on YouTube ↗Full Machine Learning course

Your boss drops a new file on your desk. It is the same housing data, but this time there is no target, and no question. Just a request. Are there natural groups of neighbourhoods here? Could we market to each one differently? Nobody can tell you the right answer, because nobody knows it. Welcome to unsupervised learning.

K-Means Clustering
From the video: K-Means Clustering
  • No target, no answers
  • Are there natural groups?
  • Welcome to unsupervised learning

Watching k-means work

K means is beautifully simple. We choose how many groups we want, and drop that many centre points at random. Then we repeat two steps. Assign, where each point joins its nearest centre. And update, where each centre moves to the middle of its points. Watch the diamonds slide across the screen. After only a few rounds, they stop moving, and we are done. No labels were used. The groups emerged from the shape of the data alone.

K-means in code

Here it is in code. We make some data with four hidden blobs. K means with four clusters fits it, and n init of ten means that it restarts ten times from different random positions, and keeps the best result, because a bad start can give a bad answer. Labels tells us the group of each point. And inertia is the total squared distance from the points to their centres, so smaller means tighter groups.

kmeans.pypython
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs

X, truth = make_blobs(n_samples=600, centers=4, cluster_std=0.9, random_state=7)

model = KMeans(n_clusters=4, n_init=10, random_state=0).fit(X)

print("Cluster of the first ten points:", model.labels_[:10])
print("Centres:")
print(model.cluster_centers_.round(1))
print("Inertia:", round(model.inertia_))
Output
Cluster of the first ten points: [2 3 1 2 3 2 1 1 1 3]
Centres:
[[ 9.5  0.7]
 [-8.5  5.6]
 [ 0.2 -8.7]
 [-1.2  4.3]]
Inertia: 913

Summary

Two steps, assign and update, repeated until nothing moves. That is all there is to k means. But we told it to find four groups, because we already knew the answer. In real work, nobody tells you how many groups there are. So how do you choose?

Key takeaways
  • No labels needed
  • Assign, update, repeat
  • Restart to avoid bad starts
Next up: How Many Groups? — the elbow and the silhouette