Use UMAP-learn for nonlinear dimensionality reduction, 2D/3D embeddings, clustering preprocessing, supervised or semi-supervised UMAP, DensMAP, AlignedUMAP, and Parametric UMAP workflows.
Permissions
Files
SKILL.md
umap-learn
Use UMAP-learn for nonlinear dimensionality reduction, 2D/3D embeddings, clustering preprocessing, supervised or semi-supervised UMAP, DensMAP, AlignedUMAP, and Parametric UMAP workflows.
license
BSD-3-Clause license
metadata.version
1.1
metadata.skill-author
K-Dense Inc.
UMAP-Learn
Overview
UMAP (Uniform Manifold Approximation and Projection) is a dimensionality reduction technique for visualization and general non-linear dimensionality reduction. Apply this skill for fast, scalable embeddings that preserve local and global structure, supervised learning, and clustering preprocessing.
Current stable release: umap-learn 0.5.12 (released April 2026). Requires Python 3.9+ and depends on scikit-learn>=1.6, numba, pynndescent, numpy, and scipy. Pin to a verified release:
uv pip install umap-learn==0.5.12
Basic Usage
UMAP follows scikit-learn conventions and can be used as a drop-in replacement for t-SNE or PCA.
import umap
from sklearn.preprocessing import StandardScaler
# Prepare data (standardization is essential)
scaled_data = StandardScaler().fit_transform(data)
# Method 1: Single step (fit and transform)
embedding = umap.UMAP().fit_transform(scaled_data)
# Method 2: Separate steps (for reusing trained model)
reducer = umap.UMAP(random_state=42)
reducer.fit(scaled_data)
embedding = reducer.embedding_ # Access the trained embedding
Preprocessing requirement: Match preprocessing to the metric. For numeric Euclidean-style metrics, scale features before fitting so high-variance columns do not dominate. For cosine, binary, precomputed-distance, or mixed-feature workflows, choose preprocessing that matches the metric instead of blindly standardizing every column.
Custom metrics: User-defined distance functions via Numba
Recommendation: Use euclidean for numeric data, cosine for text/document vectors, hamming for binary data.
Parameter Tuning Example
# For visualization with emphasis on local structure
umap.UMAP(n_neighbors=15, min_dist=0.1, n_components=2, metric='euclidean')
# For clustering preprocessing
umap.UMAP(n_neighbors=30, min_dist=0.0, n_components=10, metric='euclidean')
# For document embeddings
umap.UMAP(n_neighbors=15, min_dist=0.1, n_components=2, metric='cosine')
# For preserving global structure
umap.UMAP(n_neighbors=100, min_dist=0.5, n_components=2, metric='euclidean')
Supervised and Semi-Supervised Dimension Reduction
UMAP supports incorporating label information to guide the embedding process, enabling class separation while preserving internal structure.
Supervised UMAP
Pass target labels via the y parameter when fitting:
# Create 2D embedding for visualization (separate from clustering)
vis_reducer = umap.UMAP(n_neighbors=15, min_dist=0.1, n_components=2, random_state=42)
vis_embedding = vis_reducer.fit_transform(scaled_data)
# Plot with cluster labelsimport matplotlib.pyplot as plt
plt.scatter(vis_embedding[:, 0], vis_embedding[:, 1], c=labels, cmap='Spectral', s=5)
plt.colorbar()
plt.title('UMAP Visualization with HDBSCAN Clusters')
plt.show()
Important caveat: UMAP does not completely preserve density and can create artificial cluster divisions. Always validate and explore resulting clusters.
Transforming New Data
UMAP enables preprocessing of new data through its transform() method, allowing trained models to project unseen data into the learned embedding space.
Basic Transform Usage
# Train on training data
trans = umap.UMAP(n_neighbors=15, random_state=42).fit(X_train)
# Transform test data
test_embedding = trans.transform(X_test)
Data consistency: The transform method assumes the overall distribution in the higher-dimensional space is consistent between training and test data. When this assumption fails, consider using Parametric UMAP instead.
Performance: Transform operations are efficient (typically <1 second), though initial calls may be slower due to Numba JIT compilation.
Scikit-learn compatibility: UMAP follows standard sklearn conventions and works in pipelines. Recent 0.5.x releases also improved feature-name support and compatibility with current scikit-learn validation APIs:
Recent 0.5.12 fixes include Parametric UMAP retraining stability improvements and metric-gradient fixes, so prefer the pinned current release for neural-network workflows.
When to use Parametric UMAP:
Need efficient transformation of new data after training
Working with complex data types (images, sequences) benefiting from specialized architectures
Inverse Transforms
Inverse transforms enable reconstruction of high-dimensional data from low-dimensional embeddings.
Basic usage:
reducer = umap.UMAP()
embedding = reducer.fit_transform(data)
# Reconstruct high-dimensional data from embedding coordinates
reconstructed = reducer.inverse_transform(embedding)
Important limitations:
Computationally expensive operation
Works poorly outside the convex hull of the embedding
Accuracy decreases in regions with gaps between clusters
Example: Exploring embedding space:
import numpy as np
# Create grid of points in embedding space
x = np.linspace(embedding[:, 0].min(), embedding[:, 0].max(), 10)
y = np.linspace(embedding[:, 1].min(), embedding[:, 1].max(), 10)
xx, yy = np.meshgrid(x, y)
grid_points = np.c_[xx.ravel(), yy.ravel()]
# Reconstruct samples from grid
reconstructed_samples = reducer.inverse_transform(grid_points)
AlignedUMAP
For analyzing temporal or related datasets (e.g., time-series experiments, batch data):
from umap import AlignedUMAP
# List of related datasets
datasets = [day1_data, day2_data, day3_data]
# Relations map matching sample indices between consecutive datasets.
relations = [
{day1_idx: day2_idx for day1_idx, day2_idx in matched_day1_to_day2},
{day2_idx: day3_idx for day2_idx, day3_idx in matched_day2_to_day3},
]
# Create aligned embeddings
mapper = AlignedUMAP().fit(datasets, relations=relations)
aligned_embeddings = mapper.embeddings_ # List of embeddings
When to use: Comparing embeddings across related datasets while maintaining consistent coordinate systems. relations is required for meaningful alignment; each dictionary describes how samples in one dataset correspond to samples in the next.
Reproducibility
To ensure reproducible results, always set the random_state parameter:
reducer = umap.UMAP(random_state=42)
UMAP uses stochastic optimization, so results will vary slightly between runs without a fixed random state.
Setting random_state prioritizes deterministic output. Leave it unset when throughput matters more than exact repeatability, because UMAP can use more parallelism without a fixed seed.
Common Issues and Solutions
Issue: Disconnected components or fragmented clusters
Solution: Increase n_neighbors to emphasize more global structure
Issue: Clusters too spread out or not well separated
Solution: Decrease min_dist to allow tighter packing
Issue: Poor clustering results
Solution: Use clustering-specific parameters (n_neighbors=30, min_dist=0.0, n_components=5-10)
Issue: Transform results differ significantly from training
Solution: Ensure test data distribution matches training, or use Parametric UMAP
Issue: Slow performance on large datasets
Solution: Set low_memory=True (default), or consider dimensionality reduction with PCA first
Issue: NaN or inf values in input data
Solution: Impute or drop invalid rows before fitting. Current UMAP uses scikit-learn-style finite-value checks (ensure_all_finite) in fit() and update(), so clean numeric input is the safest default
Issue: All points collapsed to single cluster
Solution: Check data preprocessing (ensure proper scaling), increase min_dist
Issue: Imports resolve to a local file instead of the real package
Solution: Do not keep project files named umap.py, sklearn.py, hdbscan.py, or tensorflow.py beside notebooks or scripts. Those names can shadow installed packages and break or poison examples.