UMAP (Uniform Manifold Approximation and Projection) › Example
Michael BrenndoerferJuly 24, 20253 min read
Machine Learning from ScratchAll chapters Part 6 of 9Explainability
UMAP (Uniform Manifold Approximation and Projection) › ExampleOne section · Sections in this chapter4 / 8 ↓
Sections in this chapter8
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 work through a concrete example using a simple 2D dataset to demonstrate how UMAP works step by step. We'll use a dataset with two distinct clusters to make the process clear.
In[9]:
Code# Create a simple 2D dataset with two clusters
np.random.seed(42)
cluster1 = np.random.normal([2, 2], 0.5, (50, 2))
cluster2 = np.random.normal([-2, -2], 0.5, (50, 2))
X = np.vstack([cluster1, cluster2])
y = np.hstack([np.zeros(50), np.ones(50)])
print(f"Dataset shape: {X.shape}")
print(f"Cluster 1 center: {cluster1.mean(axis=0)}")
print(f"Cluster 2 center: {cluster2.mean(axis=0)}")
print(
f"Distance between cluster centers: {np.linalg.norm(cluster1.mean(axis=0) - cluster2.mean(axis=0)):.2f}"
)Out[10]:
Visualization
Now let's apply UMAP and examine the results:
In[11]:
Code# Apply UMAP
reducer = UMAP(n_components=2, n_neighbors=15, min_dist=0.1, random_state=42)
X_umap = reducer.fit_transform(X)
print(f"UMAP embedding shape: {X_umap.shape}")
print(
f"UMAP embedding range: X1=[{X_umap[:, 0].min():.2f}, {X_umap[:, 0].max():.2f}], X2=[{X_umap[:, 1].min():.2f}, {X_umap[:, 1].max():.2f}]"
)Out[12]:
Visualization
Let's also examine the local neighborhood structure that UMAP preserves:
In[13]:
Code# Examine local neighborhoods
from sklearn.neighbors import NearestNeighbors
# Find nearest neighbors in original space
nbrs_original = NearestNeighbors(n_neighbors=5).fit(X)
distances_original, indices_original = nbrs_original.kneighbors(X)
# Find nearest neighbors in UMAP space
nbrs_umap = NearestNeighbors(n_neighbors=5).fit(X_umap)
distances_umap, indices_umap = nbrs_umap.kneighbors(X_umap)
# Check how well neighborhoods are preserved
neighborhood_preservation = []
for i in range(len(X)):
original_neighbors = set(indices_original[i])
umap_neighbors = set(indices_umap[i])
intersection = len(original_neighbors.intersection(umap_neighbors))
preservation = intersection / 5.0 # 5 neighbors
neighborhood_preservation.append(preservation)
print(
f"Average neighborhood preservation: {np.mean(neighborhood_preservation):.3f}"
)
print(
f"Neighborhood preservation range: [{np.min(neighborhood_preservation):.3f}, {np.max(neighborhood_preservation):.3f}]"
)