No labels this time — just a scatter of points. Can an algorithm find the groups on its own?
Drag the two diamond centroids anywhere, then take a step. Every point turns the color of whichever centroid it's closest to; then each centroid jumps to the middle of the points that just turned its color. Do that a few times and the centroids stop moving — they've found the two blobs, starting from nothing but a guess.
This is k-means, and one iteration is just two moves repeated:
- — the cluster that point gets assigned to.
- — the coordinates of point .
- — the centroid (mean position) of cluster .
- — the set of points currently assigned to cluster .
- Assign, then update
Assign every point to its nearest centroid, then move each centroid to the mean of the points now assigned to it.
- Assignment alone stalls
Assignment without updating just repeats the same bad guess forever.
- Updating alone misses reassignments
And updating without reassigning never notices when a point should switch sides.
Try a few very different starting positions for the centroids — both inside one blob, both in empty space, swapped to the wrong side entirely. The path each takes to get there differs, but for two well-separated blobs like these, they all end up in the same place.
Starting both centroids in empty space — one at , the other at :
- First assignment step is messy
Point ends up nearer to (distance ) than to (distance ) — a near-tie that could easily have gone the other way.
- First update already helps a lot
That first assignment sorts 6 points to each centroid: 's group ends up , and 's group gets the other six. Averaging each group's coordinates:
- 's new centroid: ,
- 's new centroid: ,
— close enough to the real blobs that the next assignment step cleanly sorts every point correctly.
- Converges to the true means
With the centroids that close, every point sorts to its own blob this time — no more near-ties. Averaging each true blob's 6 points:
- Blob near : ,
- Blob near : ,
— landing exactly on both blobs' true means, and a further step changes nothing at all.
Starting from this deliberately bad position, take steps until both centroids converge.
K-means needs no labels at all — it only ever looks at distances between points and centroids. That's also its limit: it can only find clusters shaped like blobs around a center. The next chapter's technique looks at the data differently, by asking which directions it actually varies in.