UMAP (Uniform Manifold Approximation and Projection, McInnes, Healy, and Melville, 2018) is a popular feature embedding or dimension reduction algorithm. If you have finished the spectral clustering section, then these concepts shouldn’t be new. In fact, PCA is also an embedding approach, but its new features are linear combinations of the original features.
Two related methods are worth mentioning here: t-SNE (van der Maaten and Hinton, 2008) and spectral embedding, the embedding step in spectral clustering. These methods use pairwise similarities to learn coordinates that retain some of the original neighborhood information. In spectral clustering, our example used a nearest-neighbor graph. UMAP also starts from local neighborhoods, but uses weighted similarities. In t-SNE, similarities in the original space are based on Gaussian kernels, while similarities in the embedding use a heavy-tailed t-distribution.
The difference among these methods is mainly how they define and preserve similarity. UMAP uses a cross-entropy objective between weighted graphs. Spectral embedding uses eigenvectors that minimize variation across graph edges. t-SNE uses a Kullback-Leibler divergence between pairwise similarity distributions. None aims to preserve every original distance exactly.
An Example
Let’s consider our example from the spectral clustering lecture. We use the same supplied observations, with 200 observations on each of three rings. The ring labels are used only to color the plots; UMAP is fitted using the two coordinates alone.
We can perform UMAP using its usual default tuning: 15 neighbors, Euclidean distance, two embedding coordinates, and min_dist = 0.1. We use the native R implementation in the umap package and umap-learn in Python. To align the comparison, both use spectral initialization, 200 optimization epochs, spread 1, learning rate 1, repulsion strength 1, and five negative samples. In Python, 200 epochs is specified explicitly because its automatic epoch choice can differ from R’s default. Both fitting and transformation seeds are set to 1.
The implementations need not produce identical coordinates, even with these settings. Randomness, graph construction, and optimization differ. Compare which observations remain neighbors and how the groups appear, rather than comparing coordinate values or the orientation of a plot.
UMAP learns new features that place many nearby observations together. The colors help us inspect whether ring neighborhoods have been retained. Separated colored groups in a plot do not guarantee that any clustering algorithm will recover the original rings, and the gaps between groups should not be read as original distances.
Tuning
UMAP involves several tuning parameters. One of the most significant concerns how we create the nearest-neighbor graph in the first step. You can see the available settings below.
To change the neighbor count, we copy our settings and set n_neighbors = 5. This leaves the previous settings intact and changes only the neighborhood size.
With fewer neighbors, UMAP concentrates on more local structure. A ring can break into several pieces in the plot when the graph has too few connections to link its distant parts. Inspect the comparison: do the extra pieces correspond to new kinds of observations, or to a change in tuning?
Another parameter, n_components, controls the number of embedding coordinates. Two or three are useful for visualization, but embeddings with more coordinates can also be used for later analysis. The parameter min_dist controls how tightly nearby points can be packed in the embedding; smaller values can make the plot look more clustered. Neither parameter selects the true number of groups. The Python parameter guide and R configuration reference describe these choices.
Another Example
Let’s use UMAP on a larger dataset, the handwritten ZIP digits. We fit the embedding using just the 256 pixels for all 7,291 training images. All pixel values are already on the same scale, from to 1, so we keep that scale and use Euclidean distance. The first column contains the digit label and is excluded from the fit.
We can also predict embedding coordinates for future observations. After fitting on the training pixels, we transform all 2,007 test images into that fitted coordinate system. We do not fit a new embedding on the test images or use their digit labels to position them.
The training embedding and test observations transformed into the same fitted coordinate system. Colors show digit labels that were not used in fitting.
The training embedding and test observations transformed into the same fitted coordinate system. Colors show digit labels that were not used in fitting.
The colors let us compare the digit labels with the neighborhoods in the learned features. Some digits can overlap, and one digit can occupy several regions because handwriting varies. Transforming the test set shows how new images are placed relative to the fitted training embedding. This is a dimension reduction example, not a fitted classifier or a measurement of classification accuracy. A useful-looking plot is a starting point for checking structure, rather than proof of the true groups.