Learn · k-means Clustering
k-means clustering, visualised
Clustering finds structure in unlabelled data. k-means does it with a beautifully simple loop: assign each point to its nearest centre, then move each centre to the mean of its points — and repeat. Generate some blobs, step through Lloyd's algorithm, or let it run to convergence.
Ringed markers are centroids. Click anywhere on the canvas to add a point.
How many centres to fit. Changing k reseeds the centroids.
Inertia is the sum of squared distances from every point to its assigned centroid — the quantity k-means minimises. It can only stay the same or fall each iteration, and the loop stops when no point changes cluster.
What k-means is doing
k-means partitions points into k groups so that points in each group are close together. It represents each group by a single centre (its centroid) and tries to place those centres so the total squared distance from points to their nearest centre — the inertia — is as small as possible.
The assign / update loop
This is Lloyd's algorithm. Starting from an initial set of centroids (here seeded with k-means++, which spreads the first centres apart), it alternates two steps:
- Assign — give every point to the nearest centroid, using plain Euclidean distance.
- Update — move each centroid to the mean position of the points assigned to it.
Each step can only lower (or hold) the inertia, so the loop always converges — usually within a handful of iterations. It has converged once an assign step reassigns nobody.
Why the starting point matters
k-means only guarantees a local optimum, not the best possible clustering. Different initial centroids can settle on different, sometimes worse, groupings. Press New points a few times and watch the same blobs occasionally split awkwardly. In practice you run it several times from different seeds (k-means++ helps) and keep the result with the lowest inertia.
Caveat: these points are illustrative synthetic blobs in 2-D, and you pick k by hand. Real datasets are high-dimensional, rarely form neat spheres, and choosing k (elbow/silhouette methods) is a problem in itself — but the assign/update maths above is exactly what production k-means runs. More on the Learn shelf.