Skip to content

Clustering, density estimation, and anomaly detection · Graph and community clustering · UCL027 · Algorithm

Spectral clustering

Partition a similarity graph by embedding vertices in leading graph-Laplacian eigenspaces, then clustering that embedding.

Runs in CodeSplash Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Runs in CodeSplash
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through discover.spectral. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Partition a similarity graph by embedding vertices in leading graph-Laplacian eigenspaces, then clustering that embedding. It is useful for nonconvex groups when local similarity links are more meaningful than global Euclidean centroids.

Question / hypotheses

Which partition or latent block structure best explains the supplied graph relationships? This entry defines an estimation or optimization objective, not an intrinsic null/alternative hypothesis test.

Architecture / computational structure

Affinity normalization, Laplacian eigenvectors, and a final embedding-space partition.

Assumptions

Disconnected graphs can create trivial components. Negative similarities and zero-degree vertices need explicit handling. Graph construction can dominate both the result and computational cost.

Formula / computational definition

With symmetric nonnegative affinity matrix WW, degree matrix Dii=∑jWijD_{ii}=\sum_jW_{ij}, the normalized Laplacian is L=I−D−1/2WD−1/2L=I-D^{-1/2}WD^{-1/2}. Small-eigenvalue eigenvectors relax graph-cut objectives; rows of an appropriate eigenvector matrix are clustered, often after normalization.

Parameters and notation

Learned or fitted quantities: Graph eigenvectors used for embedding and resulting labels. User-set hyperparameters/design choices: Set KK, graph construction, neighbor count or RBF bandwidth, eigensolver, and final label assignment. Tune these against the stated objective and stability/validation criteria; do not select settings on the final evaluation set.

Fitting, fine-tuning, and adaptation

Neural fine-tuning is not applicable. Refit after material edge or weight changes and compare several initializations. Warm-starting a partition can help computation but may preserve an obsolete solution; use the same graph construction when comparing versions.

How to use and calibrate

Inspect connectivity, eigenvalue gaps, cut quality, stability under graph perturbations, and substantive group interpretation. An eigengap is a heuristic clue rather than an automatic significance test.

Example application

Partition a curved similarity structure using its neighborhood graph. Define the reference population, measurement units, and success criterion before fitting; investigate whether discovered groups or alerts reflect the intended phenomenon.

Interpretation / pitfalls

Watch out

The reported result is conditional on the representation and objective. A stable partition can reflect nuisance structure such as batch, collection site, or missingness. Confirm the interpretation in original variables and an independent sample.

Python and R code examples

Python supplies sklearn.cluster.SpectralClustering. R can construct the normalized Laplacian, use eigen for small matrices, and cluster normalized rows with kmeans; a custom pipeline must document which spectral formulation it implements.

Component: Normalized graph Laplacian eigenvalues. Python: executed successfully; R: not executed (Rscript unavailable). These small fixtures establish the arithmetic operation, not full-method accuracy or reproduction of paper results.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — Normalized graph Laplacian eigenvalues. · needs numpy

import numpy as np
W=np.array([[0.,1.,.1],[1.,0.,.1],[.1,.1,0.]])
d=W.sum(1); L=np.eye(3)-W/np.sqrt(d[:,None]*d)
result=np.linalg.eigvalsh(L)
print(result)
R

Component demo, not a trained model — Normalized graph Laplacian eigenvalues.

W=rbind(c(0,1,.1),c(1,0,.1),c(.1,.1,0)); d=rowSums(W)
L=diag(3)-W/sqrt(outer(d,d)); result=sort(eigen(L,symmetric=TRUE)$values)
print(result)

Sources

From the CodeSplash reference, entry UCL027.