Spectral clustering
Cluster the similarity graph, not the raw space: Laplacians, eigenvectors and normalised cuts.
Animated explanation
The idea
k-means asks "which centre is nearest?". Spectral clustering asks a different question: "which points are connected to each other through chains of close neighbours?". Two concentric rings fail the first test and pass the second easily: every point on the inner ring is linked to its neighbours on the inner ring, and hardly at all to the outer one.
So we build a similarity graph and look for a cut that severs as few, weak edges as possible while keeping the groups balanced. That combinatorial problem is hard, but a relaxed version is solved by the eigenvectors of the graph Laplacian. Those eigenvectors give each point new coordinates in which the clusters are compact blobs, and plain k-means finishes the job.
Building the graph
Choices matter more than the eigen-solver: a fully connected Gaussian graph with width , an ε-neighbourhood graph, or a k-nearest-neighbour graph (often the most robust). (or ) sets what counts as "connected". The eigengap heuristic picks the number of clusters: compute the Laplacian's smallest eigenvalues and look for a jump after the -th.
Cost and relatives
The affinity matrix is and the eigen-decomposition costs up to ; sparse k-NN graphs and Lanczos solvers help. The same Laplacian eigenvectors power Laplacian eigenmaps (dimensionality reduction) and connect to random walks: clusters are regions a random walker rarely leaves.
The algorithm, step by step
- 1Build the affinity matrix (Gaussian or k-NN) and the degree matrix .
- 2Form .
- 3Compute the eigenvectors with the smallest eigenvalues; stack them as columns of .
- 4Normalise each row of to unit length; run k-means on the rows; assign labels back to the points.
The maths
Graph Laplacian
Quadratic form
Normalised cut
Strengths and weaknesses
Strengths
- Finds non-convex clusters defined by connectivity
- Works from any similarity matrix (graphs, images, text)
- Grounded in graph theory (normalised cuts)
Weaknesses
- O(n²) memory and expensive eigen-decomposition
- Sensitive to the graph construction (σ, k)
- Still needs k (eigengap helps)
Reach for it when…
Check your understanding
Lambdian Lab
Spectral clustering of two rings
Implement normalised spectral clustering.
- affinity(X, sigma): Gaussian affinities with a zero diagonal.
- normalized_laplacian(W): .
- spectral_embedding(X, k, sigma): the k eigenvectors with the smallest eigenvalues (np.linalg.eigh), rows normalised to unit length.
- spectral_cluster(X, k, sigma, seed): KMeans(k, n_init=10, random_state=seed) on the embedding.