Interactive · Clustering
k-means splits points into k groups by repeating two moves: assign every point to its nearest centroid, then update each centroid to the mean of the points it caught. Step through it yourself and watch the total inertia fall until nothing moves.
Tip: click empty space to add a centroid, or drag a centroid (the ◆ markers) to a new spot, then press Step.
Each Step alternates between two half-rounds. In ASSIGN, every point is recolored to match the nearest centroid — squared Euclidean distance, which carves the plane into a Voronoi diagram (the faint shaded cells). In UPDATE, each centroid slides to the mean position of its newly assigned points. That's the whole algorithm.
The number that matters is inertia — the sum of squared distances from each point to its
centroid, Σ ‖x − μ‖². Both half-steps can only ever lower it (or leave it
unchanged): assignment picks the closest centroid, and the mean is provably the point that
minimizes squared distance to a set. Because inertia is bounded below and never rises, the
process must converge — usually within a handful of rounds. We stop once an ASSIGN moves
zero points.
k-means finds a local optimum, not a global one. The result depends on where the centroids start. Hit Re-init centroids a few times on the same data and you'll see different (sometimes worse) inertia — this is exactly why real implementations restart from several seeds and keep the best. Use Regenerate data for a fresh-but-reproducible set of Gaussian blobs.