How to select K in K-Means?
Answer
To select the optimal number of clusters in K-means, combine visual tools (the elbow method), quantitative metrics (the silhouette score), and statistical methods (the gap statistic), balancing model fit against generalization without overfitting. Domain knowledge and interpretability should have the final word.
(1) Elbow Method: Plot within-cluster sum of squares (WCSS) against and pick the “elbow” where the rate of improvement sharply slows.
(2) Silhouette Score: Compute the average silhouette coefficient for each candidate and pick the highest: it rewards tight, well-separated clusters.
(3) Gap Statistic: Compare the observed WCSS against that of a random reference distribution; pick the maximizing the gap.

Figure 1: The elbow method: WCSS always decreases as K grows, but the marginal gain collapses after the true cluster count (the marked elbow at K = 3). Beyond the elbow you pay model complexity for noise.
Mathematical Formulation:
Where:
is cluster
and
its centroid; WCSS is the total within-cluster squared distance that the elbow method plots against
.
is point
‘s mean distance to its own cluster (intra-cluster);
its mean distance to the nearest other cluster.
: near 1 means well clustered, near 0 means on a boundary, negative means likely misassigned; average it over all points for each
.
Leave a Reply