Spectral clustering aims at clustering observations based on their proximity information. It essentially consists of two steps. The first step is a feature embedding, or dimension reduction. We first construct the graph Laplacian (or a normalized version), which represents the proximity information, and perform an eigendecomposition of the matrix. This allows us to use a lower-dimensional matrix to represent the proximity information of the original data. Once we have the low-dimensional data, we can perform a regular clustering algorithm, such as -means, on the new dataset.
An Example
Letβs look at an example where regular -means fails to recover the groups we have in mind. The supplied sample preserves the original lectureβs R simulation with set.seed(1): 200 observations on each of three rings, with radii close to 1, 2, and 3. The same 200 random angles are used for all three rings. Both languages read these same 600 observations.
Since -means minimizes squared Euclidean distances to cluster centers, it favors compact groups around those centers. Here that criterion cuts across the rings. How can we describe closeness along a ring instead?
Adjacency Matrix
Maybe we should use a nonlinear way to describe the distance or closeness between subjects. For example, we can connect each observation to its 10 nearest neighbors, excluding itself, using Euclidean distance in the original two coordinates. We create an adjacency matrix and then make it symmetric.
W <-matrix(0, N, N)nn <- FNN::get.knn(X, k =10)for (i inseq_len(N)) { W[i, nn$nn.index[i, ]] <-1}# Average the two directed neighbor relations.W <-0.5* (W +t(W))# An alternative is W <- pmax(W, t(W)), a different weighting rule.
Show the reproducible code
W = np.zeros((N, N))nn = NearestNeighbors(n_neighbors=10, metric="euclidean").fit(X)nn_index = nn.kneighbors(return_distance=False) # Excludes each point itself.for i inrange(N): W[i, nn_index[i]] =1# Average the two directed neighbor relations.W =0.5* (W + W.T)# An alternative is W = np.maximum(W, W.T), a different weighting rule.
An observation can be a neighbor of another without the reverse being true. Averaging gives for mutual neighbors, for a one-way neighbor relation, and zero otherwise. Thus is symmetric, nonnegative, and has a zero diagonal.
Letβs use an image to display the adjacency information. Please note that our observations are ordered by rings 1, 2, and 3. The ring labels are used to explain the example, not to build the graph.
The three blocks show that the graph has no edges between rings. In this sample each ring is connected internally, so the graph has three connected components. This is the structure that ordinary -means missed.
Laplacian Matrix
The next step is to calculate the Laplacian
where is diagonal with , the degree of observation . Row sums and column sums agree because is symmetric. We will perform an eigendecomposition of to extract underlying features.
Why does this describe the graph? Suppose an embedded feature assigns a value to observation , with . We want and to be close when is large. For a symmetric graph,
Without constraints, a constant feature makes this expression zero and tells us nothing. On a connected graph, minimizing it subject to and gives an eigenvector associated with the second-smallest eigenvalue. Further features can be chosen orthogonal to the previous ones.
Our graph is disconnected. Any feature that is constant within each connected component has zero graph variation, even when the constants differ between components. There are therefore three zero eigenvalues in this example. Small numerical errors may make the computed values slightly positive or negative.
The smallest eigenvalue is always zero because . The gap after the third eigenvalue suggests retaining the first three eigenvectors for this example. Each row of the resulting matrix is a new representation of one observation.
The original example displayed the second and third eigenvectors. We retain those plots below. With repeated zero eigenvalues, numerical routines may return different orthonormal bases for the same space, so the individual curves can differ between R and Python. We use all three vectors for clustering: simply discarding whichever vector happens to be returned first need not remove only the global constant direction. Two suitable coordinates would suffice if that constant direction were explicitly removed.
The table confirms recovery of the three rings, allowing for arbitrary cluster numbers. This happens because the graph separates the rings and the embedding keeps that component information. A different neighbor count or distance measure could create a different graph and a different result.
Another Laplacian
There are other choices of the Laplacian matrix. For example, a normalized graph Laplacian is
This formula requires positive degrees, as in our graph. The normalization accounts for differences in the number and weight of connections. We again retain the three smallest eigenvectors, then normalize each row to length one before applying -means. This is the usual symmetric normalized spectral clustering construction.
For our problem, the normalized Laplacian achieves the same partition. The important change from ordinary -means was the representation of closeness through the graph. In UMAP, we will keep the idea of a neighborhood graph but use a different criterion to construct the embedding.