Spectral Clustering
When distance-to-centroid stops making sense
K-Means partitions space using Euclidean distance to centroids, which implicitly assumes clusters are compact, roughly spherical blobs. Two concentric rings, two interleaving spirals, or a crescent wrapped around a blob will all defeat it — the correct "cluster" isn't a region near any single point, it's a connected shape with no sensible center.
Spectral clustering sidesteps the centroid assumption entirely. Instead of asking "which centroid is this point closest to," it asks "which points form a tightly connected neighborhood when you think of the whole dataset as a graph." This graph-based framing is what lets it capture shapes k-means structurally cannot.
The algorithm, step by step
1. Build a similarity graph. Treat every data point as a node. Connect nodes with edge weights based on similarity — commonly a Gaussian (RBF) kernel on distance, or a k-nearest-neighbors graph where only the closest few points per node get an edge.
2. Construct the graph Laplacian. Compute L = D − W, where W is the weighted adjacency (similarity) matrix and D is the diagonal degree matrix (row sums of W). A normalized version, L_sym or L_rw, is typically used in practice for better-behaved results.
3. Compute the eigenvectors. Find the k eigenvectors of L corresponding to its k smallest eigenvalues. These eigenvectors embed each original data point into a new, low-dimensional space.
4. Cluster in the new space. Run ordinary k-means on the rows of this eigenvector matrix. Because the graph structure has already been encoded into the embedding, points belonging to the same non-convex shape end up close together in this new space even though they weren't close in the original feature space.
Note
Contrast with the k-means visualization
The K-Means visualization above shows the algorithm converging by repeatedly moving centroids and reassigning points to the nearest one — it will always produce convex, Voronoi-shaped regions no matter how you tune k or the initialization. Spectral clustering never computes a centroid at all; it clusters in an entirely different, graph-derived coordinate space first. If you mentally run the two crescents from the code sample above through the k-means visualization, you'd see straight-line boundaries slice straight through both crescents — exactly the failure mode spectral clustering is built to avoid.
📊 K-Means Clustering Simulator
InteractiveK-means vs. spectral clustering
| Property | K-Means | Spectral clustering |
|---|---|---|
| Cluster shape assumption | Convex, roughly spherical | Arbitrary, including non-convex |
| Underlying mechanism | Distance to centroids | Graph connectivity via eigenvectors |
| Computational cost | O(n · k · iterations), very fast | O(n³) for eigendecomposition (or O(n²) approximated) |
| Scalability | Scales to millions of points | Expensive beyond tens of thousands of points |
| Needs k specified upfront | Yes | Yes |
Note
What's next
Spectral clustering still requires you to specify k in advance, just like k-means. Mean Shift Clustering removes that requirement entirely — it finds the number of clusters automatically by seeking the modes (peaks) of the underlying data density.
I build these systems professionally.
Whether it's a RAG pipeline, analytics migration, or AI workflow — let's talk.