Chapter 9
Neighbor-graph embeddings
Since the late 2000s, the most widely used visualization methods start from a weighted nearest-neighbor graph and lay it out in the plane. This chapter presents the spectral embeddings (Laplacian eigenmaps and diffusion maps) and the neighbor embeddings t-SNE and UMAP, explains them in the common language of attraction along graph edges and repulsion between random pairs, and discusses what their maps can and cannot be trusted for. It connects them to the graph-based maps of the previous chapter, whose concern was the preservation of geodesic distances rather than of neighborhoods.
9.1 Problem and idea
Topology-based maps such as Isomap and TRNMap try to reproduce geodesic distances. For data that are not a single smooth manifold—several clusters, branching structures, high intrinsic dimension—no planar map can preserve these distances, and the stress of the solution is spread over all of them. Neighbor-graph embeddings give up on long distances altogether. They keep only the graph of local neighbors and look for a layout in which linked samples are close, while unlinked samples are pushed apart just enough to avoid collapse. The resulting maps show clusters with striking clarity, at the price that distances between clusters, cluster sizes, and densities in the map carry little information [23].
9.2 Theory
9.2.1 Laplacian eigenmaps
Belkin and Niyogi [88] weight the edges of the kNN graph by the heat kernel and look for coordinates that vary slowly over the graph, minimizing with under the scale constraint . The solutions are the eigenvectors of with the smallest non-zero eigenvalues—the smoothest non-constant functions on the graph. The objective is purely attractive; the constraint plays the role of repulsion. The method is deterministic and fast and is the nonlinear counterpart of the locality preserving projections of chapter 7.
9.2.2 Diffusion maps
Coifman and Lafon [89] interpret the normalized kernel matrix as the transition matrix of a random walk on the data. After normalizing the kernel by the estimated density raised to the power (with the walk approximates the Laplace–Beltrami operator of the manifold, independently of how densely it is sampled), the eigenvectors of and its eigenvalues define the embedding . Euclidean distances in this map equal the diffusion distances after steps of the walk, which are robust to noise because they average over all paths between two samples instead of following the single shortest one.
Both spectral methods share a limitation on elongated manifolds: if the manifold is much longer than it is wide, the second non-trivial eigenvector is a higher harmonic of the first rather than a coordinate across the manifold, and the map shows a curve instead of a sheet. The phenomenon appears in fig. 9.2.
9.2.3 t-SNE
t-distributed stochastic neighbor embedding [19] converts the distances from each sample into conditional probabilities , with chosen so that the distribution has a prescribed perplexity (an effective number of neighbors), and symmetrizes them, . Because the Gaussian tails decay fast, is effectively a weighted kNN graph. In the map, similarities follow a heavy-tailed Student-t kernel, , and the layout minimizes the Kullback–Leibler divergence . Its gradient consists of an attractive term along the graph edges and a repulsive term between all pairs, which Barnes–Hut or interpolation schemes approximate for large .
9.2.4 UMAP
UMAP [8] builds the graph differently and optimizes a different loss, but with the same structure (algorithm 9.1). For each sample, the distances to its nearest neighbors are shifted by the distance to the nearest one and scaled by , chosen so that the memberships sum to ; the directed memberships are symmetrized by the probabilistic union . In the map the similarity is with fitted to a minimum distance parameter, and the layout minimizes the fuzzy cross-entropy between the two graphs by stochastic gradient descent: each edge, sampled in proportion to its weight, attracts its end points, and a few random samples per edge are repelled. Starting from a spectral layout, UMAP preserves more of the global arrangement than t-SNE started at random, although Kobak and Linderman [22] showed that this is mostly due to the initialization: t-SNE initialized with PCA behaves similarly.
9.2.5 One family
Böhm, Berens, and Kobak [90] showed that Laplacian eigenmaps, UMAP, and t-SNE lie on a single attraction–repulsion spectrum: increasing the strength of attraction relative to repulsion moves from t-SNE (weak attraction, many small well-separated clusters) through UMAP to Laplacian eigenmaps (strong attraction, continuous structures). Wang et al. [91] reached a similar conclusion empirically and designed PaCMAP to balance local and global structure with mid-range pairs. In this light, the choice among neighbor embeddings is a choice of how strongly the map should emphasize clusters over continuity, not a choice between fundamentally different methods.
In a nutshell: Neighbor-graph embeddings
- All start from a weighted kNN graph; spectral methods solve a quadratic problem, t-SNE and UMAP balance attraction along edges and repulsion between random pairs.
- They preserve local neighborhoods very well (high trustworthiness) and show clusters clearly; distances between clusters and cluster sizes are not reliable.
- Initialization (spectral or PCA) matters for global structure; the graph of UMAP can be clustered directly, for example with Leiden.
- For manifolds whose geometry matters, geodesic maps (Isomap, TRNMap) remain the better choice.
9.3 Using it in graphclust
graphclust.manifold
LaplacianEigenmap(n_components, k=10, t=None); DiffusionMap(n_components, k=10, epsilon=None, alpha=1.0, t=1); UMAPEmbedding(n_components, k=15, min_dist=0.1, spread=1.0, n_epochs=None, init="spectral") with graph_; tsne(X, n_components, perplexity=30, metric="euclidean"), a wrapper of scikit-learn’s Barnes–Hut implementation that also accepts precomputed (e.g. geodesic) distances. The UMAP implementation is written in NumPy and updates all edges of an epoch at once; for data beyond a few ten thousand samples the umap-learn package implements the same algorithm with approximate neighbors.
umap = UMAPEmbedding(k=15, min_dist=0.1, random_state=0).fit(X) Y_tsne = tsne(X, perplexity=30, random_state=0)
9.4 Examples
9.4.1 Spectral embeddings of manifolds
Figure 9.2 applies Laplacian eigenmaps and diffusion maps to the S curve and the swiss roll. Both methods order the samples correctly along the length of the sheets, so the first coordinate is an excellent parametrization of the long direction. The second coordinate, however, is a quadratic function of the first instead of a coordinate across the sheet: the sheets are much longer than wide, and the second eigenvector is the second harmonic along the length. Isomap and TRNMap do not have this problem (chapter 8).
Open in ColabExample plot_01_spectral_embeddings.py in the gallery
9.4.2 Handwritten digits
On 1797 handwritten digits (64 pixels each), PCA and Isomap produce overlapping clouds (fig. 9.3); their trustworthiness at is 0.83–0.84. t-SNE and UMAP separate the ten classes into distinct islands and reach trustworthiness 0.99 and continuity close to 0.99; t-SNE has the highest area (0.54 versus 0.41 for UMAP and 0.25 for Isomap). The relative positions of the islands differ between the two maps, a reminder that inter-cluster distances in neighbor embeddings should not be interpreted.
9.5 Further reading
Belkin and Niyogi [88] and Coifman and Lafon [89] introduce the spectral embeddings; van der Maaten and Hinton [19] t-SNE; McInnes, Healy, and Melville [8] UMAP. Böhm, Berens, and Kobak [90] unify them, Kobak and Linderman [22] discuss initialization, and Chari and Pachter [23] warn against over-interpreting such maps.