← All AI/ML articles

K-means, visualised.

Before letting the algorithm run, try being the algorithm. Drag the cluster centres wherever you like and watch the inertia change, then press Step and see how one simple rule beats your best guess.

1. Place the centres2. Let Lloyd fix them3. Learn what can go wrong
square = centre (drag it!)dashed line = best inertia found in 200 runs (100 random + 100 k-means++ starts)
Drag a square to move a centre · Click empty space to add a point · Shift-click (or right-click) a point to remove it. “Best found” is the lowest inertia among those 200 runs; it is not proven to be the global optimum.
Starting centres (for Reset)
Inertia now0
Best found0
Gap above best found0
Behind the scenes · 1

Why the mean? It is the exact minimum

Hold the assignment fixed: take the set of points that currently belong to one cluster. The total squared distance from those points to a candidate centre c has a single lowest point, and it is exactly their average position. Every step away adds n times the squared step. The map shows that total for every possible c: drag the white dot and compare. This is why the update step of k-means is not a guess. The identity only applies while the assignment stays fixed; once centres move, points may switch clusters, which is what the next assign step handles.


bright = low total distancewhite dot = your trial centrestar = the mean
The identity behind the step
Behind the scenes · 2

Where it gets stuck: local minima

Each Lloyd iteration is designed not to increase inertia, but k-means only finds the nearest good answer, not necessarily the best one. So the result depends on the starting centres. Here is the final inertia from 100 runs with random starts (pink) and 100 with k-means++ starts (cyan), on your current data and k. The left-most bars are the best answers found; anything to the right is a trap. “Best found” means the lowest of these 200 runs, which is a practical benchmark rather than a proof of the global optimum.


random startsk-means++ startsdashed = best inertia found

k-means++ picks each new starting centre with probability proportional to its squared distance from the nearest one already chosen, so the starting centres are spread out. The standard fix on top of that is simply to restart several times and keep the lowest inertia.

Behind the scenes · 3

Units decide the clusters

k-means measures plain distance, so a feature with large numbers dominates. Below, two real groups sit one above the other. Stretch the x axis (as if x were measured in millimetres instead of metres) and k-means stops seeing the groups and starts slicing along x. Standardising each feature (subtract its mean, divide by its spread) removes the units, so the answer no longer depends on the stretch. Both panels are drawn in the same original coordinates.


Raw units
After standardising
cluster 1cluster 2square = centre

The score is the Adjusted Rand Index against the true two groups: 1 means a perfect match and 0 means no better than chance.

Experiment

Break it on purpose

Be the algorithm

Three real blobs, but all three centres start crammed in one corner. Press Step and watch them spread out.

Worst-case start

Find the worst of 100 random starts for these blobs and run k-means from there. How far above the best found does it stay?

Wrong units

Stretch x by 10. The two clean groups vanish for raw k-means while the standardised version is untouched.

Check yourself

Three quick questions

Key takeaways

  • The objective is inertia: the sum of squared distances from each point to its centre. Each Lloyd iteration is designed not to increase inertia: assigning points to their nearest centres cannot increase the total squared distance, and moving each centre to the mean of its assigned points minimises that distance. The algorithm can converge to a local optimum rather than the global best, and once it converges inertia simply stays unchanged.
  • Assign, then update: nearest centre for every point, then each centre to the exact mean of its points.
  • The mean is optimal for a fixed assignment: SSE(c) = SSE(mean) + n·‖c − mean‖².
  • Local minima are real. Use k-means++ and several restarts, and keep the lowest inertia. That is the best found, not a guarantee of the global optimum.
  • Scale your features: distance is not unit-free, so standardise before clustering.
← Back to all articles