← Master Index
Vol. 05 Module 5.3 Lecture

UMAP

Unsupervised Learning

How This Lesson Fits the Module

UMAP (Uniform Manifold Approximation and Projection) is the capstone for Module 5.3 because it connects clustering, dimensionality reduction, and practical visualization. It often gives cleaner 2D maps than t-SNE, runs faster on large datasets, and can transform new points after fitting.

The full workflow is now visible: scale features, optionally compress with PCA, cluster with K-Means/DBSCAN/hierarchical methods, then use UMAP to inspect whether labels and metadata make sense.

Learning Objectives

By the end of this capstone lesson, students should be able to:

  • Explain UMAP as a nonlinear manifold-learning method for neighborhood-preserving embeddings.
  • Tune n_neighbors, min_dist, metric, and random_state.
  • Use UMAP for visualization while avoiding overclaims about exact distances.
  • Compare UMAP with PCA and t-SNE in practical ML workflows.
  • Apply a fitted UMAP model to new data when the library supports transform.
  • Design an end-to-end unsupervised learning workflow for a real dataset.

How UMAP Works (Intuition)

UMAP assumes high-dimensional data lies on a lower-dimensional manifold. It builds a graph of local neighbor relationships, then optimizes a low-dimensional layout that preserves those relationships. Compared with t-SNE, UMAP often keeps more medium-range structure, which can make broad trends easier to interpret.

The axes in a UMAP plot are still arbitrary. The useful question is not “what does UMAP-1 mean?” but “do nearby points share meaningful features, labels, or outcomes?”

HyperparameterWhat it controlsStarting guidance
n_neighborsLocal vs broader structure15–50; lower = more local detail
min_distHow tightly points can pack0.0–0.5; lower = denser islands
metricDistance definition in input space"euclidean" for scaled numeric, "cosine" for embeddings
random_stateReproducible layoutSet for teaching, reports, and reviews

Implementation with umap-learn

The common Python package is umap-learn. Install it separately from sklearn, then use PCA first when input dimensions are very high.

# pip install umap-learn import matplotlib.pyplot as plt import umap from sklearn.decomposition import PCA from sklearn.preprocessing import StandardScaler scaler = StandardScaler() pca = PCA(n_components=min(50, X.shape[1]), random_state=42) X_scaled = scaler.fit_transform(X) X_pca = pca.fit_transform(X_scaled) reducer = umap.UMAP( n_components=2, n_neighbors=30, min_dist=0.1, metric="euclidean", random_state=42, ) coords = reducer.fit_transform(X_pca) plt.scatter(coords[:, 0], coords[:, 1], c=labels, s=8, cmap="tab10") plt.title("UMAP projection colored by cluster label") plt.xlabel("UMAP-1") plt.ylabel("UMAP-2") plt.show() # Later, transform new rows with the same preprocessing and reducer new_scaled = scaler.transform(new_X) new_pca = pca.transform(new_scaled) new_coords = reducer.transform(new_pca)
Engineering Habit — Compare Multiple Colorings

A capstone UMAP plot should be colored several ways: cluster label, known class, time period, geography, spend band, or model error. Meaningful structure should survive more than one convenient story.

UMAP vs PCA vs t-SNE

MethodBest roleStrengthRisk
PCAPipeline preprocessingFast, linear, reproducible transformMisses nonlinear structure
t-SNELocal visual explorationExcellent neighborhood separationSlow; global distances unreliable
UMAPExploration and reusable embeddingsFast, nonlinear, supports transformStill sensitive to parameters and randomness

Capstone Workflow

Use the following sequence as a production-minded unsupervised learning checklist:

  1. Define the unit of analysis: customer, document, image, transaction, or time window.
  2. Prepare features: clean missing values, encode categories, scale numeric columns, and document leakage risks.
  3. Reduce dimensions: use PCA when features are high-dimensional or strongly correlated.
  4. Cluster: compare K-Means, DBSCAN, and hierarchical clustering against the business question.
  5. Visualize: use t-SNE or UMAP to inspect local neighborhoods and color by labels or metadata.
  6. Validate: profile segments, check stability, review outliers, and avoid deploying a plot as the model.

Good UMAP Uses

  • Visualizing embeddings or image features
  • Auditing cluster labels qualitatively
  • Finding drift, cohorts, or mislabeled groups
  • Creating coordinates for exploratory dashboards

Use Caution

  • Do not read axis values literally
  • Do not assume every island is a real segment
  • Do not compare plots with different parameters casually
  • Do not skip scaling and metric selection
Critical Mistake — Turning Pretty Islands into Policy

UMAP can make continuous gradients look like separated islands when parameters are aggressive. Before changing product, pricing, or risk policy from a plot, validate clusters in original features and check whether the pattern is stable across seeds, samples, and time.

Knowledge Check

  1. Short Answer: What does n_neighbors influence? Answer: The balance between local neighborhood detail and broader structure.
  2. True/False: UMAP axes have direct business meaning. Answer: False—the layout axes are arbitrary.
  3. Multiple Choice: Best metric for normalized text embeddings is often: (a) cosine, (b) Manhattan by default, (c) accuracy. Answer: (a).
  4. Short Answer: Why use PCA before UMAP on very high-dimensional data? Answer: To reduce noise and speed neighbor graph construction.
  5. Short Answer: Name one validation step before using clusters. Answer: Profile cluster feature means, check stability, or review domain usefulness.
  6. True/False: min_dist controls how tightly UMAP packs points. Answer: True.
  7. Multiple Choice: Compared with t-SNE, UMAP often: (a) is slower always, (b) better preserves more global structure and can transform new points, (c) has labeled axes. Answer: (b).
  8. Short Answer: Why set random_state? Answer: Reproducible embeddings for comparisons and reviews.
  9. True/False: A pretty UMAP plot proves clusters are the right business segments. Answer: False—validate with profiles and stability.
  10. Multiple Choice: After UMAP, Module 5.4 starts with: (a) model optimization (underfitting), (b) RNNs, (c) data collection. Answer: (a).

Key Takeaways

  • UMAP is a fast nonlinear embedding method for visualizing neighborhood structure.
  • Parameter choices shape the plot; compare seeds and settings before making claims.
  • Use UMAP to inspect clusters, not as proof that clusters are real.
  • The complete unsupervised workflow combines scaling, PCA, clustering, visualization, and validation.
  • Next module: Module 5.4 Model Optimization.
Trainer’s Guide

Hands-on capstone: Use one dataset end to end. Students build scaled features, compare K-Means and DBSCAN labels, project with PCA/t-SNE/UMAP, then present which segmentation they trust and why.

Discussion prompt: A UMAP plot shows five islands, but silhouette is weak and cluster profiles overlap. What evidence would you require before naming the segments?

Recap: UMAP is a fast manifold embedding for inspecting neighborhoods—use it to audit clusters, not as the model. Continue to Module 5.4 Model Optimization.