Besley’s Lab
← All labs

Interactive notebook

k-Means Clustering

How a good algorithm still lands in a bad answer when it starts in the wrong place.

Use the arrow keys to move the crosshair. Press Enter or Space to add a point or select the nearest point. Arrow keys move a selected point. Delete removes it; Escape releases it. Hold Shift with an arrow key for a larger move.
iteration 0inertia 0.00points 90

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.