Contents
04Choosing K, and where K-means fails
4.1The elbow, the silhouette and the gap statistic
Since J always decreases with K, we look at how fast it decreases. If the data contain K^\ast well-separated groups, each new centre up to K^\ast separates two groups and J drops sharply; beyond K^\ast new centres only split a compact group and J drops a little. The elbow of the curve of J against K marks K^\ast. In Figure 2(a), for 400 points in four groups, J falls 3045.9, 1388.0 and 1212.9 for the first three additional clusters, then only 68.7 when going from 4 to 5 clusters: a clear elbow at K=4. Real data rarely give such a clear kink; for the microarray data of Hastie et al. (2009, Figure 14.8) there is “no clear indication”.
The silhouette (Rousseeuw, 1987) scores every point. Let a(i) be the average distance from point i to the other points of its own cluster, and b(i) the smallest average distance from i to the points of another cluster (the “neighbouring” cluster). Then
A value near 1 means the point is much closer to its own cluster than to the next one, near 0 means it lies between two clusters, and a negative value means it is probably in the wrong cluster. The mean silhouette over all points measures the whole clustering, and we choose the K that maximizes it: 0.676 at K=4 in Figure 2(b), against 0.546 for K=3 and 0.589 for K=5.
Worked example 4. Five points on a line in two clusters, A=\{0,1,2\} and B=\{6,7\}. For the point 2: a=(2+1)/2=1.5 and b=(4+5)/2=4.5, so s=(4.5-1.5)/4.5=0.667. The other points give 0.769 (point 0), 0.818 (point 1), 0.800 (point 6) and 0.833 (point 7), and the mean silhouette is 0.777. The point 2, nearest the other cluster, has the lowest score.
The gap statistic (Tibshirani et al., 2001) compares \log J_K with its expected value for data spread uniformly over a box containing the data, estimated by simulation, and picks the K where the gap between them is largest (formally, the smallest K whose gap is within one standard error of the gap at K+1). Unlike the elbow and the silhouette, it can answer K=1, “there are no clusters” (Hastie et al., 2009, Section 14.3.11). For mixture models, the Bayesian information criterion BIC =-2\ln\hat L+p\ln n, with p the number of parameters, is the usual choice (Schwarz, 1978; Fraley and Raftery, 2002); with this sign, as in scikit-learn, lower is better (Fraley and Raftery, 2002 use the opposite sign, so for them higher is better).
4.2Where K-means fails
K-means measures squared distance to a centre and cuts space with straight lines, so it implicitly assumes clusters that are roughly round, of similar size and spread, and convex. Figure 3 shows what happens otherwise. On three elongated, parallel clusters, K-means prefers to cut across them, because chopping a long cluster into shorter pieces lowers the squared distances more than respecting its shape; its agreement with the true groups is only \mathrm{ARI}=0.56 (the adjusted Rand index of Hubert and Arabie, 1985: 1 for a perfect match, about 0 for a random one). On two interleaved moons, no straight line separates the groups, and K-means simply cuts them in half (\mathrm{ARI}=0.23). A Gaussian mixture with full covariances, which can stretch its clusters, recovers the elongated groups perfectly (Section 5); the moons need density, linkage or graph methods (Sections 7–9). K-means is also sensitive to outliers, since a single far point pulls a mean a long way, and to unequal cluster sizes, since the boundary between two centres always lies halfway between them.
Pause and think. Would standardizing the features fix K-means on the elongated clusters of Figure 3(a)?
Show the answer
No. Standardizing rescales each axis separately, but these clusters are stretched along a diagonal direction, so they stay elongated (the comparison in Figure 9 uses standardized data, and K-means still scores 0.51). What would help is whitening with the within-cluster covariance, which is unknown before clustering; the Gaussian mixture estimates it as it goes.