k-Means Clustering illustration

k-Means Clustering

k-means partitions points into k clusters by repeating two steps: assign every point to its nearest centroid, then move each centroid to the mean of the points assigned to it. Pick a preset — Gaussian blobs, two moons, uniform noise or elongated clusters — or click the canvas to add points of your own, choose k and an initialization (random points or k-means++), and step through the algorithm: points recolour in the assignment phase, centroids glide to their new means with trails in the update phase, and Voronoi shading behind the points shows each centroid’s territory. The inertia (within-cluster sum of squares) is reported every iteration, a restart button re-rolls the initialization so you can catch bad local optima, and an elbow-curve button plots inertia for k = 1–10 to help pick k. Try the moons preset to see where k-means fails.

Runs 100% in your browser — models are trained and computed locally on your device.

Notes

  • Each iteration minimises the inertia ΣₖΣᵢ‖xᵢ − μₖ‖²: the assignment step is optimal for fixed centroids, the update step for fixed assignments, so inertia never increases and the loop converges in finitely many steps.
  • The result is only a local optimum and depends on the initialization; k-means++ seeds centroids far apart (probability proportional to squared distance) and bounds the expected inertia within O(log k) of optimal.
  • One iteration costs O(n·k·d) — cheap — but k-means always carves space into convex Voronoi cells, so it cannot recover non-convex shapes like the two moons no matter how it is initialised.
  • The elbow method picks k where inertia stops dropping sharply; inertia alone always decreases as k grows, so it can never choose k by itself.
  • Runs 100% in your browser — models are trained and computed locally on your device.