ML03.1 · k-means and how it fails
ML03.1 L2 · Intermediate 4 lessons · about 100 min Live · in review
Part of ML03 Unsupervised Learning in the Machine Learning track.
With no labels to check against, a clustering has to be judged by a score. These four lessons build k-means from its score up: the sum of squared distances from points to their centres and the two facts that minimise it, Lloyd’s algorithm that alternates them and its proof of descent, k-means++ and several starts for escaping poor local minima, the elbow for choosing K, and then the data on which even the best score is the wrong answer, with k-medoids as a first repair.
What you need first. Distances, standardising features and k-nearest neighbours (ML01.3), and partial derivatives and the gradient (M04.10). NumPy arrays and broadcasting help for the code.
Lessons
Clustering as an objective ML03.1.1 25 min
With no labels to check against, a clustering has to be judged by a score. Meet the k-means objective J, the sum of squared distances from points to their centres, and derive its two facts: the best centre is the mean, and the best cluster for a point is its nearest centre.
Lloyd’s algorithm ML03.1.2 25 min
Alternate the two facts of k-means: assign every point to its nearest centre, then move every centre to the mean of its points. Prove that J never goes up and that the algorithm stops, trace it by hand on nine points, and see why it can stop at a local minimum.
Starting well and choosing K ML03.1.3 25 min
Where Lloyd’s algorithm starts decides where it stops. Seed it with k-means++, which spreads the first centres out, run it from several starts, read the elbow of J against K for a bend rather than a minimum, and quantise the colours of a photo with MiniBatchKMeans.
How k-means fails ML03.1.4 25 min
k-means looks for round clusters of similar spread and size, because J measures squared distance to a single centre. See each failure beside the right answer, with stretched blobs, moons, unequal spreads, mixed units and an outlier, and meet k-medoids and its PAM swap step.
The interactive
ML03.1-X1 · k-means stepper. Choose a data set and K, place centres by clicking or by random or k-means++ seeding, and run Lloyd’s algorithm half a step at a time, with the Voronoi cells tinted, each centre’s move drawn as an arrow and J printed after every half-step. Drag points to see the cells react. It lives in Lloyd’s algorithm.