liveBook · Manning

Spectral clustering

As explained in Data Without Labels

Spectral clustering is a clustering method that partitions data using eigenvectors of a graph-based affinity representation rather than only geometric location.

Spectral clustering groups data points by their affinity—how strongly they are connected—rather than by location alone. It is useful when clusters have complicated shapes. For example, the inner and outer rings of a circular dataset form two distinct groups even though a distance-based split may mix them. section 5.3

This differs from k-means, which groups points around cluster centers. In spectral clustering, connectivity drives the grouping: points may belong together because they are linked through a pattern, not because they occupy a compact region around a centroid. The method does not require clusters to have a spherical shape. section 5.3 section 5.3.2

Figure 5.3 We can have a complex representation of data points that need to be clustered. Observe the doughnut shape (left). An explanation can be that the dots in a square are a part of the same cluster as what would be based on the distance only, but clearly, they are not part of the same cluster (middle). We have two circles here. The points in the inner circle belong to one cluster, whereas the outer points belong to another cluster (right).
figure

The concentric-circle diagrams show why nearby points are not necessarily in the same cluster: the inner ring and outer ring are separate groups. section 5.3

The graph-based process moves from relationships between points to a space where they can be clustered:

  • Construct adjacency and degree matrices for the dataset, then use them to calculate a Laplacian matrix. section 5.3.2
  • Find the k eigenvectors corresponding to the k smallest eigenvalues of the Laplacian. section 5.3.2
  • Use the matrix formed by those eigenvectors to cluster the points in k-dimensional space. In a simple two-cluster illustration, positive and negative values of the resulting Fielder vector distinguish the groups. section 5.3.2

These steps explain what the algorithm uses, but a practical Python workflow need not calculate every matrix by hand. The worked comparison uses scikit-learn on 2,000 points generated with `make_circles`, fits both k-means and spectral clustering, and plots their labels on the same data. section 5.3.2 section 5.4

spectral = SpectralClustering(n_clusters=2, affinity='nearest_neighbors', random_state=5)
sc_clustering = spectral.fit(data)
plt.scatter(data[:,0], data[:,1], c=sc_clustering.labels_, cmap='prism', alpha=0.5, edgecolors='g')

The Python listing fits `SpectralClustering` with two clusters and `affinity='nearest_neighbors'`, then colors a scatter plot by the fitted labels. section 5.4

Check the shape of the result

On the circular dataset, the k-means labels overlap the two rings, while spectral clustering separates them. Plot both outputs rather than judging a clustering solely from its labels; then change the generated data and compare again. Spectral clustering is also a candidate for image segmentation, speech analysis, text analytics, and entity resolution, but its matrix operations can require substantial computation, making dataset size a practical consideration. section 5.4 section 5.3.2 section 5.9

How Data Without Labels covers it

How deep it goes
Works through graph matrices, Laplacian eigenvectors, and a two-cluster illustration, then uses a library rather than implementing the algorithm from scratch.
Examples
Compares k-means and spectral clustering on 2,000 `make_circles` samples using plotted cluster labels.
What it assumes
Builds on basic clustering and k-means while explaining the graph-matrix process without requiring advanced mathematics.
What you can do after
Apply it via scikit-learn’s `SpectralClustering` and compare its plotted labels with k-means on circular data.
How it connects
Connects clustering to graph theory, affinity matrices, Laplacian matrices, eigenvectors, and k-means.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Without Labels. The section links point to the parts of the book it draws on.