Visual explainer
K-Means Clustering
How Lloyd's algorithm discovers groups in unlabelled data through a two-step dance of assigning points and moving centroids.
Your data shows up with coordinates and nothing else. Four numbers sit on a line: 1, 2, 9, 10. Your eyes already see two pairs with a gap of 7 between them. That gap is the whole job.
Guessed Centres
K-means starts by guessing. You drop two centres onto the line, here at 1 and 2, and both land on the left pair. These are placeholders with no authority yet. The right pair starts orphaned.
Assign Then Average
Then the two-step dance begins. Every point joins its nearer centre, drawing the split at 1.5, and each centre jumps to its points average. The right centre leaps from 2 all the way to 7.
Sides Switch
Because the centres moved, loyalties change. Point 2 now sits nearer to 1 than to 7, so it switches sides and the split slides to 4. Fresh averages land at 1.5 and 9.5, and nothing moves after that.
Proof It Worked
Measure the win on one axis. Total squared spread starts at 113 under the first guesses and ends at 1.0 once settled. Same points, same ruler, nearly one hundred times tighter. The dance earns its keep.
Name Your K
You must name k upfront, and the count shapes everything. Here k = 2 gives every centre a real pair, while k = 3 strands one centre on a single dot. If a split looks lonely, lower k and retry.
Where It Breaks
Distance from a centre is all k-means trusts, so it dreams in balls. Feed it two crescents and the straight split cuts each moon in half, pairing wrong halves across the gap. Use density methods there instead.
The Quick Version
- Points arrive with positions only.
- Drop centres as opening guesses.
- Points join the nearer centre.
- Centres jump to their means.
- Spread falls from 113 to 1.0.
- Lonely splits mean lower k.
- Crescents break the round assumption.