Spectral Clustering
Spectral clustering turns data points into a graph of who is connected to whom, then uses the eigenvectors of that graph to re-plot the points somewhere ordinary k-means can finally separate them cleanly.
Prerequisites: K-Means Clustering
K-means clustering assumes clusters look like round blobs centred somewhere, which fails badly on data shaped like two interlocking spirals or two concentric rings — no straight-line distance-to-a-centre rule can separate them. Spectral clustering solves this not by inventing a fancier distance rule but by first re-describing the data in a completely different coordinate system, one where the awkward shapes straighten out and plain k-means suddenly works.
The analogy: untangling a knotted necklace by its connections
Picture a tangled necklace where nearby beads are connected by thread, and you want to identify separate strands without being fooled by how the whole thing is coiled up in a box. Looking only at raw 3D position is useless — a bead from one strand can be physically closer to a bead from a different strand than to its own neighbour, because the necklace is coiled. But if you instead ask "which beads are directly connected by thread," and use that connectivity to figure out layout, the true strands become obvious. Spectral clustering builds exactly this connectivity graph between data points, then re-lays them out using the graph's structure rather than their raw coordinates.
The maths: graph, Laplacian, eigenvectors
First build a similarity graph: connect each point to its nearby neighbours, weighted by how similar they are (often via an RBF-style kernel). This gives an adjacency matrix and a degree matrix (each diagonal entry is how connected that point is overall). The graph Laplacian is
In words: encodes how much each point differs from its neighbourhood average — it is small where connections are strong and consistent, and it is the object whose structure reveals natural groupings. The key move is computing the eigenvectors of corresponding to its smallest eigenvalues, and using the first of them as new coordinates for each point — each original data point gets re-plotted using these eigenvector values instead of its original features. Ordinary k-means is then run on these new coordinates.
Here is an eigenvector and its eigenvalue. In words: eigenvectors of with small eigenvalues correspond to directions along which the graph varies smoothly within a cluster but sharply between clusters — exactly the axis you'd want to cluster along, and one that raw coordinates never provided.
Worked example 1: two rings, why raw distance fails
Two concentric rings of points, inner ring radius 1, outer ring radius 5. A point at the top of the inner ring and a point at the top of the outer ring are only distance apart — closer than that inner-ring point is to a point on the opposite side of its own ring, at , distance ... actually closer, but consider a point at on the inner ring versus on the outer: distance , while to , both inner ring, is . K-means mostly survives on this simple case, but shrink the ring gap and it fails: raw Euclidean distance simply doesn't track "which ring." Spectral clustering instead connects each point only to its handful of nearest neighbours — a point on the inner ring connects only to other nearby inner-ring points, never to the outer ring, however close in raw distance a fluke pairing might be. The resulting graph has two nearly disconnected pieces, one per ring, and the Laplacian's eigenvectors reflect that split cleanly.
Worked example 2: a tiny graph by hand
Four points, two clearly-connected pairs: – (similarity 1) and – (similarity 1), with only a faint cross-link – (similarity 0.05). Degree matrix diagonal: . The Laplacian has mostly-zero off-diagonal entries except the strong – and – links and the tiny – one. Its second-smallest eigenvector (the one used for 2-cluster splits) will assign nearly opposite-signed values to versus , because that's the split that requires cutting only the weak link rather than a strong -weight one — the eigenvector is, in effect, finding the cheapest possible cut through the graph.
The explorer above shows eigenvectors as the directions a linear transformation doesn't rotate — spectral clustering uses the Laplacian's eigenvectors the same way, as the natural axes along which the graph's structure is best described.
Compare a rigid boundary to a flexible one in the explorer above: spectral clustering's whole purpose is producing boundaries as flexible as the ring or spiral example, which a distance-based method like plain k-means cannot draw.
Spectral clustering re-describes points using the eigenvectors of a similarity graph's Laplacian, turning clusters that are tangled in raw coordinates into ones that are cleanly separable — then hands the easy part off to ordinary k-means.
What this means in practice
Spectral clustering suits data with non-convex, intertwined, or graph-structured cluster shapes — grouping assets by a correlation network rather than raw return distance, or clustering entities connected through a transaction graph. It costs more than k-means: building the similarity graph and its eigenvectors scales poorly with the number of points, typically limiting practical use to thousands rather than millions of points without approximations.
The classic mistake is applying spectral clustering and then judging results by eye in the original feature space, where the whole point was that clusters don't look separable there — the separation lives in eigenvector space. Also, the choice of similarity graph (how many neighbours, what kernel bandwidth) matters enormously; too generous a connectivity and the graph becomes one blob with no natural cut at all.
Practice in interviews
Further reading
- von Luxburg, A Tutorial on Spectral Clustering (2007)
- Hastie, Tibshirani & Friedman, The Elements of Statistical Learning, ch. 14.5