Skip to content
AI360Xpert
Beta
Core ML

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.

Four numbers arrive with no labels, yet two pairs stare back at you.
Four numbers arrive with no labels, yet two pairs stare back at you.

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

Two guessed centres drop in at 1 and 2, both landing on the left pair.
Two guessed centres drop in at 1 and 2, both landing on the left pair.

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

Points join the nearer centre, then each centre jumps to its points mean of 1 and 7.
Points join the nearer centre, then each centre jumps to its points mean of 1 and 7.

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

The split slides to 4, points switch sides, and the centres settle at 1.5 and 9.5.
The split slides to 4, points switch sides, and the centres settle at 1.5 and 9.5.

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

Total squared spread falls from 113 to 1.0 on the same axis.
Total squared spread falls from 113 to 1.0 on the same axis.

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

Two centres fit these points, while three strands one point all alone.
Two centres fit these points, while three strands one point all alone.

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

On two crescents the straight split cuts each moon in half.
On two crescents the straight split cuts each moon in half.

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.