UMAP (Uniform Manifold Approximation and Projection) › Visualizing UMAP

Michael BrenndoerferJuly 24, 20257 min read
Machine Learning from ScratchAll chapters
Part 6 of 9Explainability
Chapter 50UMAP (Uniform Manifold Approximation and Projection) › Visualizing UMAP
Reading section 3 of 8 · Visualizing UMAP · 7 minSection 3 of 8 · 7 min

Book contents

Section 3 of 8 · 7 min read

UMAP (Uniform Manifold Approximation and Projection) › Visualizing UMAPOne section · Sections in this chapter3 / 8 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's create visualizations that demonstrate how UMAP works and how it compares to other dimensionality reduction methods. We'll start with a simple example using synthetic data to illustrate the key concepts.

In[3]:
Code
import warnings

import numpy as np
from sklearn.datasets import make_circles, make_moons, make_swiss_roll
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from umap import UMAP

# Suppress UMAP warnings about n_jobs with random_state
warnings.filterwarnings("ignore", message="n_jobs value .* overridden")

# Set random seed for reproducibility
np.random.seed(42)

# Create synthetic datasets
swiss_roll, swiss_roll_color = make_swiss_roll(
    n_samples=1000, noise=0.1, random_state=42
)
circles, circles_color = make_circles(
    n_samples=500, noise=0.1, factor=0.3, random_state=42
)
moons, moons_color = make_moons(n_samples=500, noise=0.1, random_state=42)

# Apply different dimensionality reduction methods
pca_swiss = PCA(n_components=2, random_state=42).fit_transform(swiss_roll)
tsne_swiss = TSNE(n_components=2, random_state=42, perplexity=30).fit_transform(
    swiss_roll
)
umap_swiss = UMAP(
    n_components=2, random_state=42, n_neighbors=15, min_dist=0.1
).fit_transform(swiss_roll)

pca_circles = PCA(n_components=2, random_state=42).fit_transform(circles)
tsne_circles = TSNE(
    n_components=2, random_state=42, perplexity=30
).fit_transform(circles)
umap_circles = UMAP(
    n_components=2, random_state=42, n_neighbors=15, min_dist=0.1
).fit_transform(circles)

pca_moons = PCA(n_components=2, random_state=42).fit_transform(moons)
tsne_moons = TSNE(n_components=2, random_state=42, perplexity=30).fit_transform(
    moons
)
umap_moons = UMAP(
    n_components=2, random_state=42, n_neighbors=15, min_dist=0.1
).fit_transform(moons)

Now let's create visualizations comparing the different methods:

Out[4]:
Visualization
PCA scatter plot of the Swiss roll colored by position along the roll.
PCA projection of the Swiss roll. The continuous color progression remains folded together because PCA captures only a linear projection.
t-SNE scatter plot of the Swiss roll colored by position along the roll.
t-SNE projection of the Swiss roll. Local neighborhoods are preserved, but distances between separated regions do not represent the original global geometry.
UMAP scatter plot of the Swiss roll colored by position along the roll.
UMAP projection of the Swiss roll. Local neighborhoods and the broad continuous progression are both visible in the embedding.
Out[5]:
Visualization
PCA scatter plot of concentric circles, with class zero as blue circles and class one as coral triangles.
PCA projection of concentric circles. The two nonlinear rings overlap in the linear projection.
t-SNE scatter plot of concentric circles, with class zero as blue circles and class one as coral triangles.
t-SNE projection of concentric circles. The two classes separate while retaining their ring-like local structure.
UMAP scatter plot of concentric circles, with class zero as blue circles and class one as coral triangles.
UMAP projection of concentric circles. The two classes form clearly separated curved manifolds.
Out[6]:
Visualization
PCA scatter plot of two moons, with class zero as blue circles and class one as coral triangles.
PCA projection of two moons. The nonlinear classes remain interleaved in the linear coordinates.
t-SNE scatter plot of two moons, with class zero as blue circles and class one as coral triangles.
t-SNE projection of two moons. The two local manifolds separate clearly, while their global spacing is arbitrary.
UMAP scatter plot of two moons, with class zero as blue circles and class one as coral triangles.
UMAP projection of two moons. Both classes separate into distinct curved manifolds with their local shape preserved.

Let's also demonstrate how UMAP parameters affect the results:

Out[7]:
Visualization
UMAP scatter plot of concentric circles with n_neighbors set to 5.
UMAP embedding with n_neighbors = 5. The small neighborhood emphasizes fine local structure and fragments both concentric-circle manifolds.
UMAP scatter plot of concentric circles with n_neighbors set to 15.
UMAP embedding with n_neighbors = 15. The default-scale neighborhood balances local detail with the broader two-ring structure.
UMAP scatter plot of concentric circles with n_neighbors set to 50.
UMAP embedding with n_neighbors = 50. The larger neighborhood emphasizes the broader global separation between the two classes.
Out[8]:
Visualization
UMAP scatter plot of concentric circles with min_dist set to 0.01.
UMAP embedding with min_dist = 0.01. Points can pack tightly, producing compact class manifolds with detailed local structure.
UMAP scatter plot of concentric circles with min_dist set to 0.1.
UMAP embedding with min_dist = 0.1. The default minimum distance retains compact neighborhoods while leaving visible within-class spacing.
UMAP scatter plot of concentric circles with min_dist set to 0.5.
UMAP embedding with min_dist = 0.5. The larger minimum distance spreads observations across broader manifolds.