Lesson 44
Spectral Clustering
Connect nearby points into a graph and cut that graph so tightly linked points stay in the same cluster. One example is on this page.
Intermediate12 min
What You Will Learn
- Idea
- Example
- When to use it
Idea
Spectral clustering builds a similarity graph. Points close to each other get an edge. It then uses the graph's structure to embed the points and cluster that embedding. The method can separate shapes that a round k-means center would smear together.
Example
import numpy as np
from sklearn.cluster import SpectralClustering
X = np.array([[0, 0], [0.1, 0.2], [0.2, 0.0], [5, 5], [5.1, 4.9], [4.8, 5.2]], dtype=float)
labels = SpectralClustering(
n_clusters=2, affinity="nearest_neighbors", n_neighbors=2, random_state=0
).fit_predict(X)
print(labels.tolist())Output:
[0, 0, 0, 1, 1, 1]When to use it
- Use it when clusters are connected shapes rather than round blobs.
- You still choose the number of clusters.
- Nearest-neighbor affinity needs enough neighbors to link a real group.
Loading playground…