Classical machine learningML 11

k-Means Clustering

k-means splits unlabelled data into k groups. It places k centres, called centroids, and then repeats two simple moves: assign every point to its nearest centroid, then update every centroid to the mean of the points assigned to it. When the assignments stop changing, it is done. It is one of the most widely used methods for finding groups, from customer segments to cell types and colour palettes. Step through the moves one at a time, or press play. Click to add points.

the datalive
click: add point · ✕: centroids · shading: nearest centroid

Inertia at each iteration

Elbow plot best inertia for each k · line: the current k

inertia: the total squared distance from each point to its centroid, which no step can raise · lines from points to centroids appear after an assign step (when paused)

Local optima and starting points

No assign or update step can raise the inertia, so k-means always stops, but it stops at the first arrangement it cannot improve, which need not be the best one. With random starting points, two centroids sometimes begin in the same blob and one real blob is left to share a centroid with its neighbour: press “new start” a few times with four blobs to see it happen. k-means++ (Arthur & Vassilvitskii, 2007) picks each new starting centroid with probability proportional to its squared distance from the nearest centroid already chosen, which spreads them out and makes poor starts rare. Software usually also runs k-means several times and keeps the result with the lowest inertia.

Choosing k

Inertia always falls as k grows, reaching zero when every point is its own cluster, so the lowest inertia cannot pick k. The elbow plot shows the best inertia found for each k: on clear data the curve drops steeply until k reaches the true number of groups and then flattens, and that bend, the “elbow”, suggests k (Thorndike, 1953). On the data with no clusters there is no elbow, yet k-means still returns k tidy-looking groups: it always finds clusters, whether or not they are there.

What k-means assumes

Because each point goes to its nearest centroid, k-means draws straight borders halfway between centroids and favours round clusters of similar size. The unequal example shows the cost: the borders cut into the wide cluster to give the tight ones a fair share of space. The two moons are two clear groups, but not round ones, so k-means splits them across the middle. Methods such as Gaussian mixtures, DBSCAN or hierarchical clustering make different assumptions. Stuart Lloyd devised the algorithm at Bell Labs in 1957 for quantising signals; it was published only in 1982. The name “k-means” comes from James MacQueen (1967).

References

  1. Lloyd, S. P. (1982). Least squares quantization in PCM. IEEE Transactions on Information Theory, 28(2), 129–137. (Bell Laboratories manuscript, 1957.) doi.org/10.1109/TIT.1982.1056489
  2. MacQueen, J. (1967). Some methods for classification and analysis of multivariate observations. In Proceedings of the Fifth Berkeley Symposium on Mathematical Statistics and Probability (Vol. 1, pp. 281–297). University of California Press.
  3. Arthur, D., & Vassilvitskii, S. (2007). k-means++: the advantages of careful seeding. In Proceedings of the Eighteenth Annual ACM-SIAM Symposium on Discrete Algorithms (SODA) (pp. 1027–1035). SIAM.
  4. Thorndike, R. L. (1953). Who belongs in the family? Psychometrika, 18(4), 267–276. doi.org/10.1007/BF02289263