Interactive notebook
k-Means Clustering
How a good algorithm still lands in a bad answer when it starts in the wrong place.
Next: assign each point to its nearest centroid.
Click empty space to add a point, drag one to move it, or alt-click to remove it. Keyboard users can focus the plot and use its arrow, Enter, and Delete controls. The faint lines show which centroid currently owns each point, and inertia is the total squared distance along those lines — the quantity k-means is minimizing. It never increases, which is exactly why the algorithm can get stuck: from a bad start, every step is an improvement right up until it stops at the wrong answer. Try “Bad corner” a few times with re-seed.