mb

k-means

k-means gets points without labels and a number k. it drops k centres, then repeats two moves: assign every point to its nearest centre, and update every centre to the middle of its points. inertia, the sum of squared distances from points to their centres, can only go down, so it always stops.

32 points

big marks: the centres. thin lines: each point to its centre. dashed: how far each centre just moved

arrow keys move the crosshair, shift for finer steps. tap to place. keys: arrows, enter, backspace to erase

4
step
0
next
assign
inertia
—
seed
1

4 centres placed with a random start. step to assign the points

centrepointsxy
circle—0.590.46
square—0.420.71
triangle—0.58-0.70
diamond—0.55-0.55

inertia by step

seeds 1 to 100, each run to the end

startstuck
random33 of 100
k-means++6 of 100

stuck: an inertia over 1.5 × the best any run found, 0.65.

try this

  1. press reseed and run, a few times over. the points never change, yet at seed 4 the answer does: one centre takes two groups and two centres split another.
  2. seed 4 stops at an inertia of 4.72, about seven times the 0.65 of the right groups. no single step makes it better, so it stays: a local optimum.
  3. switch the start to k-means++ and run seed 4 again. it spreads the first centres out and finds all four groups. under more, it gets stuck in 6 of 100 seeds, random starts in 33.