Choosing the Number of Clusters
K-means and its relatives need the number of clusters, k, decided before they run — and since more clusters always fit the training data at least as well as fewer, picking k needs a rule that isn't just "lowest training error."
Prerequisites: K-Means Clustering, The Bias-Variance Decomposition
K-means clustering needs the number of clusters, , handed to it before it starts — it has no built-in way to discover the "right" number on its own. And a naive fix, "pick the that minimizes within-cluster distance," fails immediately: within-cluster distance keeps shrinking every time goes up, hitting zero once equals the number of data points, where every point is its own one-member cluster. That's a perfect fit and a useless answer, so choosing needs a rule that penalizes complexity, not one that just rewards fit.
Two workable rules: the elbow method looks for the point where adding another cluster stops buying much improvement, and the silhouette score directly measures whether each point sits comfortably inside its own cluster versus how close it is to a neighboring one — and only the silhouette score gives an actual number you can compare across different .
Two ways to find k
The elbow method plots within-cluster sum of squared distances (inertia) against . Inertia always decreases as grows, but early on each new cluster explains a lot of remaining structure, and past some point it explains very little — the curve bends from a steep decline to a shallow one, and that bend (the "elbow") is read off as the reasonable . It's intuitive but the elbow is often ambiguous — real data rarely bends as cleanly as a textbook picture.
The silhouette score is more precise: for each point , compute , its average distance to other points in its own cluster, and , its average distance to points in the nearest other cluster. The silhouette for that point is . In words: it's the gap between "how far am I from my neighbors" and "how far am I from the nearest rival cluster," scaled to sit between -1 and +1. A point near +1 sits comfortably inside a tight, well-separated cluster; a point near 0 sits right on a boundary between two clusters; a negative value means the point is actually closer to a different cluster than its own — a sign it was probably misassigned. Averaging across all points for a given gives a single comparable score, and the with the highest average silhouette is the pick.
Worked example
Clustering 500 stocks by their trailing 60-day return correlations, inertia drops sharply from to , then only gradually from onward — a visible but not perfectly crisp elbow around . Computing the average silhouette score across through gives: 0.31, 0.38, 0.44, 0.40, 0.35, 0.33, 0.30 — a clear peak at , confirming the elbow's rough read with an actual number. Choosing groups the 500 stocks into four correlation-based clusters that, on inspection, roughly line up with cyclicals, defensives, high-beta growth, and rate-sensitive financials — clusters a portfolio manager can sanity-check against sector intuition.
What this means in practice
In finance, "the right number of clusters" is often as much a business decision as a statistical one — a risk model built for position sizing might want 6-10 clusters to be actionable, even if the silhouette score technically favors 4. Treat the silhouette score and the elbow as evidence to weigh, not an automatic answer, and always look at what the resulting clusters actually contain before committing to a .
The elbow and silhouette methods both assume roughly spherical, similarly-sized clusters, which is exactly what k-means itself assumes. If the true groupings are elongated or highly uneven in size — common with correlation structures during a crisis, where one dominant regime cluster balloons — these diagnostics can point to a that fits the method's assumptions better than it fits the actual data.
Related concepts
Practice in interviews
Further reading
- Rousseeuw, 'Silhouettes: A Graphical Aid to the Interpretation and Validation of Cluster Analysis' (1987)
- Tibshirani, Walther & Hastie, 'Estimating the Number of Clusters via the Gap Statistic' (2001)