labs / unsupervised
K-Means Clustering
Group points into k clusters by alternating between assigning points and recentering.
K-means groups unlabeled points into k clusters without ever being told what the clusters are. It does this by alternating two simple steps until nothing moves anymore.
The two steps
Assign. Every point joins whichever centroid is currently closest to it (straight-line distance).
Update. Each centroid jumps to the average position — the mean — of the points now assigned to it. That's the "means" in k-means.
assign: cluster(p) = argmin_k distance(p, centroid_k)
update: centroid_k = mean of all points with cluster(p) = k
Neither step alone knows the "right" answer, but alternating them is a hill-climbing process: each round can only decrease the total distance from points to their centroid, never increase it. It's guaranteed to settle down (converge) — just not always on the best possible grouping, which is why the starting positions matter.
This run deliberately starts the three centroids in bad spots — the corners of the plot, far from any real cluster — so you can watch them race toward the actual groups over the first few iterations.
Press play, or step through one assign-and-update round at a time.