The idea
Clustering asks: do these unlabeled points form groups? k-means makes that concrete by choosing centroids to minimize within-cluster squared distance (inertia):
Lloyd's algorithm solves it by alternating two moves, each of which can only lower :
- Assign each point to its nearest centroid.
- Update each centroid to the mean of its points (the mean is the unique minimizer of squared distances — that is why it is k-means).
Finitely many partitions + monotone descent ⇒ guaranteed convergence, but to a local minimum: initialization decides which one. Standard practice is k-means++ seeding (new seeds picked with probability proportional to squared distance from existing seeds, spreading them out) plus a few restarts, keeping the lowest-inertia solution. Cost per iteration is — this is the clustering algorithm that scales.
The objective silently assumes: Euclidean geometry (so scale your features), convex roundish clusters of similar size and density, and a known . Each broken assumption has a designated successor:
- Elliptical, overlapping, different-size clusters → GMM: fit Gaussians with EM, get soft memberships and per-cluster covariances. k-means is exactly the degenerate GMM with shared spherical covariance as — hard assignments re-emerge in the limit.
- No known , or the hierarchy is the point → agglomerative clustering: start with every point as a cluster, greedily merge the closest pair (per single/complete/average/Ward linkage), read any granularity off the dendrogram. Price: distances.
- Non-convex shapes, noise, unknown k → DBSCAN: density-based, finds rings and moons, labels outliers as noise — but struggles with varying density.
Choosing k (for the methods that need it): the inertia-vs- curve only ever decreases, so look for the elbow where the marginal drop collapses; cross-check with the silhouette
( = mean distance to own cluster, = mean distance to nearest other cluster; average near 1 is tight-and-separated, near 0 is mush) — and then let the domain veto: a segmentation nobody can act on has the wrong regardless of the curves.
Worked example
Six 1-D points, : , initial centroids , .
Iteration 1 — assign: distances to : point 3 is vs → cluster 1; point 10 is vs → cluster 2. Result: , .
Iteration 1 — update: , .
Iteration 2 — assign: no point changes side → converged.
Silhouette of point 3 (the boundary-most point of cluster 1):
Even the worst-placed point scores 0.81 — this is what genuinely separated clusters look like. Contrast on the same data: the best it can do is split one tight triple, say into and . Inertia falls to — lower than 4, as inertia always is with more clusters — but the silhouette collapses where the split happened: point 2 now has and (distance to the singleton cluster), so , and the singleton gets by convention. Inertia rewards ; silhouette exposes it. That disagreement is the everyday argument for never choosing from the elbow curve alone.
Cheat sheet
k-means objective: ; Lloyd: assign-to-nearest / recompute-means; converges to local min; per iter; k-means++ + restarts.
Silhouette: ; average it to score a clustering; near 0 ⇒ overlapping/boundary.
| Method | Assignments | Shape assumption | Needs k? | Scales? | Reach for when |
|---|---|---|---|---|---|
| k-means | hard | spherical, similar size | yes | great | large n, fast segmentation |
| GMM (EM) | soft probabilities | elliptical (per-cluster covariance) | yes | okay | overlap, membership probabilities, density model |
| Hierarchical (Ward etc.) | nested dendrogram | linkage-dependent | no — cut later | , small n | taxonomy, unknown k, custom distances |
| DBSCAN | hard + noise label | arbitrary shape, uniform density | no (, minPts instead) | good | non-convex shapes, outlier discovery |
- Scale features before any distance-based clustering; log-transform skewed ones first.
- Inertia ALWAYS decreases in — never compare raw inertia across without the elbow/gap-statistic framing.
- Choosing k: elbow + silhouette + stability under resampling + business actionability; agreement metrics (adjusted Rand) across bootstraps separate structure from noise.
- k-means = GMM limit (shared , ); use GMM when clusters are stretched or overlapping.
- k-means traps: outliers drag means (→ k-medoids), empty clusters on bad init, chain-shaped data sliced into balls.
- EM/GMM full covariance costs parameters per cluster — restrict to diagonal when is large.
- Validate without labels: silhouette, Davies-Bouldin, stability; with side labels: adjusted Rand / NMI.