Spectral Clustering: Partitioning Non-Convex Data in AI
Spectral clustering is a powerful machine learning technique used to group complex, non-convex data distributions that traditional algorithms like K-means fail to separate. By transforming spatial data into a graph representation, this method focuses on the connectivity between data points rather than their geometric distance from a central point. This article explains the mechanics of spectral clustering, specifically how it constructs a similarity matrix, computes the Graph Laplacian, and extracts eigenvectors to map non-linear data into a lower-dimensional space where it can be easily partitioned.
The Limitation of Traditional Clustering on Non-Convex Data Traditional clustering algorithms, such as K-means, rely on Euclidean distance to group data points around centroids. This approach assumes that clusters are convex—meaning they are roughly spherical or blob-shaped. However, in many artificial intelligence applications, data forms non-convex shapes like concentric circles, interlocking spirals, or crescent moons. In these scenarios, two points belong to the same cluster not because they are close to a central point, but because they are connected by a continuous chain of nearby points. K-means fails here because it cuts directly through the shapes based on distance from a center. Spectral clustering solves this by redefining clustering as a graph-partitioning problem.
Step 1: Constructing the Similarity Matrix The first step in spectral clustering is to represent the dataset as an undirected graph, where each data point is a node (vertex) and the relationships between points are edges. This graph is mathematically represented by a Similarity Matrix (or Affinity Matrix), denoted as \(W\).
For a dataset with \(N\) points, the similarity matrix is an \(N \times N\) grid. Each entry \(W_{ij}\) measures how close or similar point \(i\) is to point \(j\). A common way to calculate this is using the Gaussian Radial Basis Function (RBF) kernel, which assigns a high value (close to 1) to points that are very close together, and a value approaching 0 to points that are far apart. The result is a matrix that captures the local neighborhood structure of the data, effectively tracing the shape of the non-convex distribution.
Step 2: Deriving the Graph Laplacian Once the similarity matrix is built, the algorithm calculates the Degree Matrix, \(D\). This is a diagonal matrix where each diagonal entry \(D_{ii}\) is the sum of all similarities connected to point \(i\). It represents how well-connected a specific point is to the rest of the dataset.
Using the Similarity Matrix (\(W\)) and the Degree Matrix (\(D\)), the algorithm constructs the Graph Laplacian Matrix, \(L\). In its simplest unnormalized form, it is calculated as:
\(L = D - W\)
The Graph Laplacian is crucial because its mathematical properties directly reflect the connectivity of the graph. It holds the key to finding the "bottlenecks" in the data—the sparse areas between dense, non-convex clusters.
Step 3: Computing the Eigenvectors To partition the graph, the algorithm seeks to cut the edges in a way that minimizes the weights of the cut edges (separating different clusters) while maximizing the weights within the remaining groups (keeping similar points together). Solving this directly is computationally difficult, so spectral clustering uses linear algebra to relax the problem.
The algorithm calculates the eigenvalues and eigenvectors of the Graph Laplacian \(L\). In spectral graph theory, the smallest eigenvalues and their corresponding eigenvectors contain structural information about the graph's connected components:
- An eigenvalue of 0 indicates a completely disconnected component (an isolated cluster).
- The second smallest eigenvalue (known as the Fiedler value) and its corresponding eigenvector (the Fiedler vector) provide the optimal approximation for cutting the graph into two distinct parts.
To find \(k\) clusters, the algorithm extracts the eigenvectors corresponding to the \(k\) smallest eigenvalues.
Step 4: Dimensionality Reduction and Final Partitioning This extraction of eigenvectors is the core mechanism that untangles non-convex data. By taking the \(k\) selected eigenvectors and placing them side-by-side as columns, the algorithm forms a new \(N \times k\) matrix.
Each row of this new matrix represents an original data point, now mapped into a new \(k\)-dimensional space (often called the spectral embedding). Because the eigenvectors are derived from the connectivity of the graph rather than raw spatial coordinates, points that were connected along a complex curve in the original space are pulled tightly together in this new space.
What was once a non-convex shape—like two interlocking moons—is transformed into distinct, tightly packed, and linearly separable blobs in the eigenvector space. Finally, a standard K-means algorithm is applied to the rows of this new matrix. Because the data is now convex in this transformed space, K-means easily and accurately partitions the points, and these cluster labels are mapped back to the original non-convex dataset.