← All AI/ML articles

Clustering, visualised.

Nobody gave these points labels. Clustering has to find the groups on its own. Drop in some centres, watch them tug the points toward themselves, and see when the answer is right and when it is confidently wrong.

1. Assign and update2. Choose how many3. Know the limits
Click to add a point · Shift-click (or right-click) a point to remove it · Squares are the cluster centres (centroids).
Iteration 0 = starting centres. Iteration t = assign points to the current centres, then move the centres, then re-assign.
Starting centres
Inertia (WCSS)0
Silhouette (shown clustering)0
Points0
Behind the scenes · 1

One iteration, up close

k-means repeats two simple moves. Assign: every point joins its nearest centre. Update: every centre jumps to the average position of its points. The squared distance from points to their centres (the inertia) can never go up during these steps, so the loop always settles. Iteration t below is the same iteration t as in the controls above: hollow squares are the centres before it, filled ones the centres after it. The run has converged when re-assigning the points after the update changes nobody’s cluster.


hollow = old centrefilled = new centre (arrow shows the move)
What the computer does
Behind the scenes · 2

How many clusters?

For the same dataset, the best achievable inertia cannot increase when you add more clusters. Inertia alone therefore cannot determine the best k: it generally rewards adding clusters, even when the extra clusters are not useful (with k equal to the number of points it is zero). Two common guides are the elbow, where the curve stops dropping quickly, and the silhouette. For a point i, s(i) = (b(i) − a(i)) / max(a(i), b(i)), where a(i) is its average distance to the other points in its own cluster and b(i) is its lowest average distance to any other cluster. The score is the mean of s(i) over all points: near 1 means points sit much closer to their own cluster, near 0 means they sit on borders, and a negative value means a point is on average closer to another cluster than to its own. That compares relative distances; it does not prove a point is truly misplaced. Each k below was run 6 times with k-means++ and the run with the lowest inertia was kept, and the silhouette is computed from that same kept clustering.


Elbow: best-of-6 inertia vs k
Silhouette vs k (same best-of-6 runs)
Behind the scenes · 3

When k-means is confidently wrong

k-means draws straight borders halfway between centres, so it prefers round, similar-sized blobs. DBSCAN works differently: a point is a core point if at least minPts points lie within distance ε of it, counting the point itself. With minPts = 4, that means at least three other points plus itself. Clusters grow by chaining core points together, and points that are not reachable are left as noise. This lets DBSCAN follow clusters of arbitrary shape, although not always successfully: the wrong ε or minPts can merge or shatter clusters. Try Moons or Circles above, then compare. The score is the Adjusted Rand Index against the true grouping of the generated points: 1 is perfect, 0 is about chance (DBSCAN noise counts as one extra group).


k-means (your k)
DBSCAN
grey = noise (DBSCAN only)

DBSCAN is not simply better: it needs ε and minPts, it struggles when clusters have very different densities, and it has no centres to summarise a group. Match the method to the shape of the data.

Experiment

Break it on purpose

Unlucky start

Random starting centres can trap k-means in a poor local optimum. Find the worst start out of 30 and compare it with k-means++.

Moons

Two crescent moons, k = 2. k-means slices them straight down the middle instead of following the curves.

Too many clusters

Three real blobs but k = 7. Inertia looks better, the silhouette says otherwise.

Check yourself

Three quick questions

Key takeaways

  • k-means alternates two steps: assign points to the nearest centre, then move each centre to the mean of its points.
  • It always converges, but possibly to a poor local optimum, so the starting centres matter (k-means++ spreads them out).
  • Choosing k is a judgement: the best achievable inertia never increases as k grows, so inertia alone rewards extra clusters. Use the elbow and the silhouette as guides, not proof.
  • k-means assumes round blobs. Curved or very uneven shapes break it.
  • DBSCAN clusters by density, can follow arbitrary shapes and flags noise, at the cost of choosing ε and minPts (the point itself counts toward minPts).
← Back to all articles