Contents
09Spectral clustering: a cluster is a well-connected piece of a graph
9.1From points to a graph
Spectral clustering forgets the coordinates and keeps only who is near whom. Build a similarity graph: each point is a node, and similar points are joined by edges with weights w_{ij}\ge0. Common choices are the k-nearest-neighbour graph (join i and j if either is among the other’s k nearest neighbours) and the fully connected Gaussian graph w_{ij}=\exp(-\|x_i-x_j\|^2/2s^2) with a scale s. A good clustering cuts few and weak edges between clusters and keeps many strong edges inside them. Social networks give the intuition: communities are groups of people with many friendships inside and few across.
Let W=(w_{ij}) be the weight matrix, d_i=\sum_jw_{ij} the degree of node i and D=\mathrm{diag}(d_1,\dots,d_n). The graph Laplacian is L=D-W. For any vector f\in\mathbb{R}^n, one value per node,
(Expand the right-hand side and use \sum_jw_{ij}=d_i.) So f^\top Lf is small when strongly connected nodes get similar values: it measures how much f varies across edges. In particular L is positive semidefinite and L\mathbf 1=0: the constant vector has eigenvalue 0.
Proposition 2 (von Luxburg, 2007, Proposition 2). The multiplicity of the eigenvalue 0 of L equals the number of connected components of the graph, and its eigenspace is spanned by the indicator vectors of the components.
If Lf=0 then f^\top Lf=0, so by (15) f_i=f_j whenever w_{ij}>0: f is constant on every connected component. Conversely the indicator vector of any component satisfies Lf=0, because its rows sum the weights of edges that stay inside the component. Indicator vectors of different components are orthogonal, so the eigenspace has dimension equal to the number of components.
If the clusters were perfectly separate, the eigenvectors with eigenvalue 0 would be their indicators, and clustering would be trivial. Real clusters are joined by a few weak edges, so the eigenvalues are only close to 0 and the eigenvectors are only approximately constant on each cluster. That is still good enough: in the coordinates given by the first few eigenvectors, the clusters become tight, easily separated groups.
9.2The algorithm, and why it is a relaxed graph cut
The method of Ng et al. (2002), used by scikit-learn: build the graph; compute the K eigenvectors of the normalized Laplacian L_{\mathrm{sym}}=I-D^{-1/2}WD^{-1/2} with the smallest eigenvalues; stack them as the columns of an n\times K matrix and normalize its rows to unit length; cluster the rows with K-means. Each point is thus mapped to a new K-dimensional “spectral embedding” and clustered there.
Why eigenvectors? Cutting the graph into two parts A and \bar A with few edges between them can be written with an indicator vector: if f_i=1 on A and -1 on \bar A, then by (15) f^\top Lf=4\,\mathrm{cut}(A,\bar A), four times the total weight of the edges that are cut. Minimizing the cut alone favours slicing off a single point, so balanced criteria divide it by the sizes or volumes of the parts: ratio cut, and the normalized cut of Shi and Malik (2000). These discrete problems are NP-hard, but if we relax f to take any real values, subject to being orthogonal to the constant vector and of fixed length, the minimum of f^\top Lf is the eigenvector of the second-smallest eigenvalue, the Fiedler vector (Fiedler, 1973), and its signs give the cut. Normalized cuts lead in the same way to L_{\mathrm{sym}} (von Luxburg, 2007).
Worked example 8. Two triangles, nodes \{1,2,3\} and \{4,5,6\}, with unit-weight edges inside each triangle and one weak edge of weight 0.1 between nodes 3 and 4. Without the weak edge, L has eigenvalues 0,0,3,3,3,3: two components, two zeros. With it, the eigenvalues are 0, 0.064, 3, 3, 3 and 3.136; the small second eigenvalue signals two nearly separate pieces. The Fiedler vector is (0.417,0.417,0.390,-0.390,-0.417,-0.417): its signs split the graph into the two triangles, and the bridge nodes 3 and 4 get the smallest values. For the \pm1 indicator of this split, f^\top Lf=0.4=4\times0.1, four times the weight of the cut edge.
The video’s example. With an 8-nearest-neighbour graph on 170 points from two moons (Figure 8), the second eigenvalue of L_{\mathrm{sym}} is 0.0008, almost zero: the graph is one component, but only barely, since one edge joins the moons. In the spectral embedding the two moons become two separate groups, and K-means on the embedding recovers them perfectly (\mathrm{ARI}=1.00), where K-means on the original coordinates scored 0.23.
The graph is part of the model. Spectral clustering is only as good as its graph. In the comparison of Figure 9, the moons are standardized and 14 scattered points are added. These points create bridges in the 10-nearest-neighbour graph, and spectral clustering cuts one moon instead of separating them (\mathrm{ARI}=0.36); other neighbour counts do not fix it (0.01 for 5 neighbours, 0.51 for 30). A Gaussian graph with w_{ij}=e^{-\gamma\|x_i-x_j\|^2} succeeds for \gamma=10 (0.99) and \gamma=20 (1.00) but fails again for \gamma=50 (0.00), when the graph falls apart into small pieces. von Luxburg (2007, Section 10) warns that spectral clustering “can be quite unstable” under different choices of the neighbourhood graph. The number of clusters is often read from the eigenvalues as well: choose K so that the first K eigenvalues are small and the next one is clearly larger (the eigengap), although, as Figure 8(b) shows, the gap is not always sharp.
Pause and think. Spectral clustering ends with K-means. Why does it succeed where K-means alone fails?
Show the answer
Because K-means runs in a different space. The eigenvectors map each point to coordinates that are nearly constant on each well-connected piece of the graph, so curved clusters that no straight line could separate in the original space become compact, separated blobs, which is exactly the situation K-means handles well.