← All animations

๐ŸŽฏ K-Means Clustering

You have a cloud of points and no labels โ€” nobody told you which group each one belongs to. K-means finds the groups by itself. Drop k markers, colour every point by the nearest marker, then slide each marker to the middle of its colour, and repeat. Two tiny steps โ€” assign, then move โ€” chased until nothing changes. Scroll through and watch structure fall out of raw geometry.

1. The loop โ€” assign, then move, until it settles

Each round has exactly two steps. Assign: colour every point with the nearest centroid (the big โœ•). Move: slide each centroid to the mean of its points. Press Step to do one half-step at a time, or Auto-run to watch it converge โ€” the iteration counter stops climbing once nothing moves. Click the canvas to add your own points.

k = 3 iteration 0 ยท assign next

2. The catches โ€” random starts & choosing k

K-means isn't magic. It only finds a local optimum, so a different random start can give a different answer โ€” press New seed a few times and watch the clustering (and its total within-cluster distance) jump around. And k is your choice: this data has a clear number of blobs. Slide k and watch the elbow chart on the right โ€” the spot where adding another cluster stops buying much improvement is usually the right k.

k = 2 inertia: โ€”

3. What's really going on

One algorithm, a handful of ideas: