Skip to content
Lambdian

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 σ\sigma, an ε-neighbourhood graph, or a k-nearest-neighbour graph (often the most robust). σ\sigma (or kk) 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 kk-th.

Cost and relatives

The affinity matrix is n×nn\times n and the eigen-decomposition costs up to O(n3)O(n^3); 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

  1. 1
    Build the affinity matrix WW (Gaussian or k-NN) and the degree matrix D=diag⁡(W1)D = \operatorname{diag}(W\mathbf{1}).
  2. 2
    Form Lsym=I−D−1/2WD−1/2L_{\text{sym}} = I - D^{-1/2}WD^{-1/2}.
  3. 3
    Compute the kk eigenvectors with the smallest eigenvalues; stack them as columns of UU.
  4. 4
    Normalise each row of UU to unit length; run k-means on the rows; assign labels back to the points.

The maths

Graph Laplacian

L=D−W,Lsym=I−D−1/2WD−1/2L = D - W,\qquad L_{\text{sym}} = I - D^{-1/2}WD^{-1/2}

Quadratic form

f⊤Lf=12∑ijWij(fi−fj)2\mathbf{f}^\top L\mathbf{f} = \tfrac12\sum_{ij}W_{ij}(f_i - f_j)^2

Normalised cut

NCut⁡(A1..Ak)=∑ccut⁡(Ac,Aˉc)vol⁡(Ac)\operatorname{NCut}(A_1..A_k) = \sum_{c}\frac{\operatorname{cut}(A_c, \bar A_c)}{\operatorname{vol}(A_c)}

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…

Clusters are defined by connectivity rather than compactness, or you start from a similarity graph: image segmentation, social and citation networks.

Check your understanding

Question 1

Q1Why does spectral clustering separate two concentric rings when k-means cannot?

Question 2

Q2Which eigenvectors of the normalised Laplacian are used?

Question 3

Q3What is the main scalability bottleneck?

Lambdian Lab

Spectral clustering of two rings

Implement normalised spectral clustering.

  • affinity(X, sigma): Gaussian affinities with a zero diagonal.
  • normalized_laplacian(W): I−D−1/2WD−1/2I - D^{-1/2}WD^{-1/2}.
  • 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.
Open the labPython + NumPy + scikit-learn runs in your browser. Sign-in keeps your code and attempts.