← capítulo

K-means clustering

No labels this time. Just points on a plane.

Find the groups that are already there — without being told what a group is.

The first unsupervised chapter.

The whole algorithm

Guess k centers. Then repeat two moves until nothing changes:

The centers chase the density and park in the middle of each group.

The data

150 points, four blobs from make_blobs, no labels. Gray on purpose.

Your eye finds four groups instantly. Now make the algorithm find them.

What it minimizes

Inertia — the within-cluster sum of squares:

J=i=1Nxiμci2J = \sum_{i=1}^{N} \bigl\lVert x_i - \mu_{c_i} \bigr\rVert^{2}

Assign step picks the nearest centroid. Update step moves each centroid to its cluster's mean. Both only lower JJ, so the loop has to stop — at a local minimum that depends on the seed.

Watch the centroids move

Diamonds are the centroids. Rings reach to each cluster's farthest point. Lower panel: inertia falling. Every frame is one real iteration.

Inertia craters from ~7,081 to 342 in six iterations. Most of it in step one.

Picking k: the elbow

Inertia always falls as k grows. Don't minimize it — find the bend.

Cliff to k=4 (342.2), then flat. The orange dot is the elbow.

Scratch vs. library

Our k-means++ vs sklearn.cluster.KMeans. Both hit inertia 342.2. Both score adjusted Rand index 1.0 against the hidden true blobs — perfect recovery.

Takeaways