All concepts
K-Means Clustering
Alternate between assigning points to the nearest centroid and moving each centroid to its cluster's mean.
Classical ML · Beginner · ~8 min
In plain English
Drop k pins on a map, send every house to its nearest pin, then move each pin to the middle of the houses that chose it. Repeat until the pins stop moving.
Why it's worth your time
It's the fastest way to find structure in unlabelled data — customer segments, image palettes, a first look at any embedding space.
If you remember three things
- You must choose k; the algorithm won't tell you
- It finds round, similarly-sized clusters — that's an assumption, not a detail
- Scale your features or the largest-range column defines the clusters
Overview
K-means partitions data into k clusters by minimizing within-cluster variance. It repeats two steps — assign each point to the closest centroid, then recompute centroids as cluster means — until assignments stop changing. Simple, fast, and everywhere, but you must choose k and it assumes round clusters.
How it works
- The data Unlabeled points. We suspect there are k natural groups and want to find them.
- Initialize centroids Place k centroids (k-means++ picks spread-out starting points to avoid bad local minima).
- Assign step Assign every point to its nearest centroid by Euclidean distance. This partitions the space.
- Update step Move each centroid to the mean of the points assigned to it.
- Reassign Points near a moved centroid switch clusters. Assign and update alternate — this is EM-style coordinate descent.
- Converge When assignments stop changing, within-cluster variance is at a local minimum. Done.
In an interview
K-means clusters data by alternating assign (nearest centroid) and update (centroid = cluster mean) to minimize within-cluster variance. It's fast and simple but you must pick k, it finds only local optima, and it assumes round, similarly-sized clusters.
Production defaults
- Init
- k-means++ always. Random init is how you get a bad local optimum
- Restarts
- n_init=10 and keep the best inertia
- Choosing k
- elbow on inertia plus silhouette score — and a human check that the clusters mean something
What breaks
- One giant cluster and several tiny ones — Unscaled features, or the data genuinely isn't spherical. Try DBSCAN or Gaussian mixtures.
- Different clusters every run — Bad initialization or k is wrong. Fix the seed for reproducibility, but treat instability as a signal that k is off.