Plain k-means picks its starting centroids uniformly at random — sometimes two of them land in the same blob by pure bad luck, and the algorithm never fully recovers. And nobody handed you the "right" number of clusters to begin with. Can both problems be fixed with the same idea?
Step through the seeding process. Every point gets a bar proportional to how far it is from every centroid chosen so far — a point already close to an existing centroid barely registers, while a point stuck in an untouched blob towers over the rest. That's the whole trick: distant points get picked more often, without ever being forced to.
k-means++ replaces uniform-random seeding with distance-weighted sampling, and the elbow method replaces a guessed with one read off a curve.
- — the distance from point to the nearest centroid already chosen.
- — the probability of picking as the next centroid: proportional to , normalized over every not-yet-chosen point.
- — the inertia: total squared distance from every point to its assigned centroid, once centroids have converged via Lloyd's algorithm.
- First centroid, then weighted draws
The first centroid is picked arbitrarily (uniformly at random in practice). Every centroid after that is sampled from — walk the cumulative distribution until a random draw lands inside a point's slot.
- Farther points are more likely, never guaranteed
A point ten units from every existing centroid has a much bigger slice of the distribution than one sitting right next to a centroid — but it's still a lottery, not a rule. Any point can still be drawn.
- Run Lloyd's algorithm to convergence, once seeded
With centroids chosen this way, ordinary k-means (assign, recompute, repeat) takes over from here.
- Increase k and watch inertia only ever fall
More centroids can only help — never increases with . The elbow is the whose drop is dramatically larger than the next one: the last point where adding a centroid genuinely helped, before returns collapse to "barely worth it."
At , everything is forced into one centroid at the grand mean — a big inertia, badly fit. At , one centroid per true blob, and inertia bottoms out almost entirely. At , a blob gets needlessly split in two, but the improvement over is tiny compared to every earlier jump.
Three tight blobs of 3 points each, far apart from one another; the very first draw is fixed to land exactly halfway through the cumulative distribution each time:
- D(x)^2 from the first centroid, (0,0)
Squared distance (since the centroid is the origin) to each of the other 8 points:
- :
- :
- :
- :
- :
- :
- :
- :
Summing: .
- The halfway draw lands on the far blob
Half of is . Walking the cumulative sum in point order:
- : cumulative
- : cumulative
- : cumulative
- : cumulative
- : cumulative
- : cumulative — first exceeds here
The second centroid is .
- A third halfway draw finds the last untouched blob
With centroids at and , is now the smaller of the two distances to each point. Every point in the first two blobs is far closer to than to , so their is unchanged from step 1: (for ), while the third blob's points are now essentially at distance from the chosen : . Summing: ; half is . Walking the cumulative sum, it passes at (cumulative ) — the third centroid. All three blobs now have exactly one seed.
- Inertia across k=1..4
is the cleanest to check by hand: with one centroid per blob, each blob's centroid is its own mean, e.g. blob A's mean is , giving squared distances to its three points — summing to . All three blobs have the same shape, so each contributes , and .
The same sum-of-squared-distances recipe, applied to the (worse-fitting) 1- and 2-centroid clusterings and the (needlessly-split) 4-centroid one, gives , , and . The drop from to is ; the drop from to is under . is the elbow.
Pick the value of k where the inertia drop collapses — the elbow of the curve.
k-means++ fixes where k-means starts — weighting the random draw by squared distance so centroids spread themselves across the data instead of clumping by chance. The elbow method fixes how many centroids to use — tracking inertia as grows and stopping at the point where more centroids stop paying for themselves. Neither removes the randomness or the guesswork entirely, but both turn a blind guess into an informed one.