Fun with Learning Technology
LearnCoursesQuestionsTracksToolsNewsExplorePractice
Fun with Learning Technology

A new problem, explained clearly, every day.

Subscribe
Learn
  • Lessons
  • Topics
  • News
  • Tools
  • Courses
  • Career tracks
  • Everything
Site
  • About
  • Contact
  • Support
  • Privacy
  • Terms
Get the daily one

One email per new problem. No spam.

Request a tutorial

Requests shape what gets made next.

© 2026 Fun with Learning TechnologyRSS
Home›Courses›Machine Learning›t-SNE and UMAP

Unsupervised Learning

t-SNE and UMAP

t-SNE and UMAP are non-linear dimensionality reduction techniques designed to project high-dimensional data into low-dimensional space for visualization. They matter because they preserve local structure better than linear methods like PCA, allowing analysts to identify clusters and patterns in complex datasets. You should reach for these tools when you need to interpret high-dimensional distributions, verify class separability, or perform exploratory data analysis on manifold-structured data.

The Core Philosophy of Manifold Learning

To understand non-linear dimensionality reduction, we must move beyond the assumption that data lies on a flat Euclidean plane. Instead, we view data as existing on a 'manifold,' a complex geometric structure that exists in high-dimensional space but locally resembles lower-dimensional Euclidean space. Traditional methods like Principal Component Analysis (PCA) fail here because they seek to preserve global variance through linear projections, which inherently flattens the manifold and destroys local structure. t-SNE and UMAP take a different approach by focusing on neighbor relationships. By modeling the probability that two points are neighbors in high-dimensional space and attempting to match those probabilities in a lower-dimensional embedding, they 'unroll' the manifold. This preserves the local neighborhood relationships that define clusters, making the resulting visualization significantly more representative of the actual groupings within the complex dataset compared to global linear transformations.

import numpy as np
# Generate synthetic data points on a 3D Swiss Roll manifold
from sklearn.datasets import make_swiss_roll
X, color = make_swiss_roll(n_samples=1000, noise=0.1)
# The manifold is complex; PCA would crush this, but we preserve local connectivity.

t-SNE: Modeling Stochastic Neighbors

The t-distributed Stochastic Neighbor Embedding (t-SNE) algorithm works by transforming the distances between points in high-dimensional space into conditional probabilities that represent similarities. For a given point, the probability of selecting another point as its neighbor is proportional to a Gaussian distribution centered at the first point. In the lower-dimensional target space, t-SNE uses a Student t-distribution with one degree of freedom, which has heavier tails than the Gaussian distribution. This 'heavy tail' is critical: it prevents the 'crowding problem' where clusters would otherwise collapse into a dense center. By minimizing the Kullback-Leibler divergence between these two probability distributions using gradient descent, t-SNE effectively pushes non-neighboring points apart while pulling actual neighbors together. The result is a visualization where local clusters are incredibly clear, though the global distance between separate clusters often loses its metric significance.

from sklearn.manifold import TSNE
# Initialize t-SNE with a high perplexity to capture broader local context
tsne = TSNE(n_components=2, perplexity=30.0, learning_rate=200)
X_embedded = tsne.fit_transform(X)
# The output X_embedded represents the 2D projection maintaining local similarity.

UMAP: Topological Data Analysis

Uniform Manifold Approximation and Projection (UMAP) is rooted in topological data analysis and Riemannian geometry. Unlike t-SNE, which focuses primarily on the probability distribution of neighbors, UMAP constructs a fuzzy topological representation of the high-dimensional data. It assumes that the data is uniformly distributed on a Riemannian manifold and that the metric is locally constant. UMAP creates a weighted directed graph where edges represent the strength of connectivity between points. It then optimizes a low-dimensional layout that is as close as possible to the high-dimensional topological structure in terms of cross-entropy. Because UMAP utilizes a much more efficient optimization approach than the gradient descent used in t-SNE, it is significantly faster and handles larger datasets with ease. Furthermore, UMAP preserves more global structure than t-SNE, making the relative positioning of distant clusters more meaningful for high-level exploratory analysis.

from umap import UMAP
# UMAP is generally faster and preserves more global relationships than t-SNE
umap_model = UMAP(n_neighbors=15, min_dist=0.1, metric='euclidean')
X_umap = umap_model.fit_transform(X)
# The n_neighbors parameter balances the focus between local and global structure.

Choosing Between Algorithms

When deciding between t-SNE and UMAP, you must consider the specific needs of your project. If you are exclusively interested in finding very dense, tight clusters in medium-sized datasets, t-SNE remains an industry standard due to its proven track record in visualizing feature spaces in neural networks. However, if you require a faster runtime, the ability to project new data into an existing mapping, or a better representation of how large clusters relate to one another, UMAP is the superior choice. UMAP treats the data as a graph, which allows it to maintain a stronger grasp on the global topology of the dataset. You should always run both if time permits, as they emphasize different aspects of the underlying data geometry. Remember that both are stochastic, meaning their outputs can vary between runs unless you explicitly set a random seed for reproducible visualization.

import matplotlib.pyplot as plt
# Visualize the projection results using scatter plotting
plt.scatter(X_umap[:, 0], X_umap[:, 1], c=color, cmap='Spectral', s=5)
plt.title('UMAP Projection of the Swiss Roll')
plt.show() # Visualization helps identify manifold unrolling success

Avoiding Common Pitfalls

The most common mistake when using these algorithms is over-interpreting the distance between disparate clusters. Because both t-SNE and UMAP focus on local optimization, the 'empty space' between clusters is often an artifact of the projection process rather than a meaningful metric of high-dimensional distance. Another pitfall is the misuse of hyperparameters; for instance, choosing a perplexity in t-SNE that is too low can result in meaningless fragmented clusters, while a value that is too high can lead to a single blurred blob. Similarly, in UMAP, a low number of neighbors focuses purely on local micro-clusters, whereas a high number forces the algorithm to focus on the global structure, potentially washing out local details. Always normalize your features before running these algorithms, as they are distance-based and sensitive to the scale of input variables, just like K-Nearest Neighbors or Support Vector Machines.

from sklearn.preprocessing import StandardScaler
# Always scale data, as distance metrics are sensitive to feature magnitudes
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Now the projection is robust against feature scale imbalances.

Key points

  • t-SNE preserves local structure by matching probability distributions of neighbors.
  • UMAP relies on fuzzy topological representations to preserve both local and global connectivity.
  • PCA is a linear method, whereas t-SNE and UMAP are specifically designed for non-linear manifold learning.
  • The t-distribution in t-SNE prevents cluster crowding by providing heavier tails than Gaussian distributions.
  • UMAP is generally faster and more scalable than t-SNE for very large datasets.
  • Both t-SNE and UMAP are stochastic, so setting a random seed is necessary for consistency.
  • Distances between far-apart clusters in low-dimensional projections are often unreliable metrics.
  • Feature scaling is a prerequisite because these algorithms depend on distance calculations between data points.

Common mistakes

  • Mistake: Interpreting distance between clusters as meaningful in t-SNE. Why it's wrong: t-SNE focuses on local structure preservation, and global distances between distant clusters are often artifacts of the optimization process. Fix: Use UMAP if global structure is a priority, or be cautious not to draw conclusions about inter-cluster relationships in t-SNE plots.
  • Mistake: Assuming t-SNE and UMAP can be used for out-of-sample transformation. Why it's wrong: These are primarily manifold learning techniques for visualization, not parametric dimensionality reduction models. Fix: Use PCA or autoencoders if you need to project new, unseen data points onto existing manifolds.
  • Mistake: Using default perplexity or n_neighbors without tuning. Why it's wrong: These hyperparameters control the balance between local and global structure; incorrect settings can lead to noise being misinterpreted as structure. Fix: Perform a grid search or use heuristic-based initialization and evaluate results across multiple values.
  • Mistake: Believing that lower dimensions always reflect the original data accurately. Why it's wrong: Both techniques compress high-dimensional data, meaning information loss is inevitable. Fix: Acknowledge that these are visualization tools for cluster detection and not replacements for high-dimensional feature analysis.
  • Mistake: Using t-SNE or UMAP as a direct replacement for PCA for downstream classification. Why it's wrong: These methods are non-linear and stochastic, which can severely warp the feature space in ways that break linear classifiers. Fix: Perform dimensionality reduction for visualization only; use PCA or feature selection for downstream training pipelines.

Interview questions

What is the primary objective of dimensionality reduction techniques like t-SNE and UMAP?

The primary objective of these techniques is to perform non-linear dimensionality reduction, effectively mapping high-dimensional data points into a lower-dimensional space, typically two or three dimensions, for visualization and exploration. Unlike linear methods such as Principal Component Analysis, which focus on preserving global variance, t-SNE and UMAP focus on preserving local structures. This allows machine learning practitioners to identify clusters, detect outliers, and understand the manifold geometry of complex datasets that would otherwise be impossible to interpret visually in their original high-dimensional space.

How does t-SNE represent similarity between data points in high-dimensional and low-dimensional spaces?

t-SNE converts Euclidean distances between data points into conditional probabilities that represent similarities. In the high-dimensional space, it uses a Gaussian distribution to calculate these probabilities. In the low-dimensional space, it uses a Student t-distribution with one degree of freedom. The algorithm then minimizes the Kullback-Leibler divergence between these two distributions using gradient descent. This approach effectively pushes dissimilar points apart while pulling similar points together, which is particularly useful for visualizing distinct clusters in noisy data, though it can be computationally expensive for very large datasets.

What is the foundational concept behind UMAP's construction of a topological representation?

UMAP is grounded in Riemannian geometry and algebraic topology. It assumes that the data is uniformly distributed on a Riemannian manifold, that the metric is locally constant, and that the manifold is locally connected. UMAP constructs a weighted k-neighbor graph representation of the high-dimensional data and then optimizes a low-dimensional graph to have as close a fuzzy topological structure as possible. This approach is significantly faster than t-SNE because it separates the process into a graph construction phase and an optimization phase, allowing it to scale effectively to millions of points.

Compare t-SNE and UMAP in terms of performance and preservation of data structure.

t-SNE is generally better at preserving local relationships and is often preferred for discovering small, tight clusters. However, it is computationally intensive and struggles with global structure preservation. UMAP, by contrast, is much faster and handles larger datasets with greater ease while preserving more of the global structure of the data. For instance, in Python, one might use `umap.UMAP().fit_transform(data)` which typically runs in a fraction of the time required by `sklearn.manifold.TSNE().fit_transform(data)`. UMAP is increasingly favored in production pipelines where both speed and global context are critical for data exploration.

Why is it important to consider the 'perplexity' parameter in t-SNE, and how does it influence the result?

The perplexity parameter in t-SNE effectively acts as a guess for the number of close neighbors each point has. It balances the attention between local and global aspects of the data. A low perplexity value considers only a small number of local neighbors, which can lead to disjointed clusters and noisy visual artifacts. A higher perplexity value considers more neighbors, helping to reveal more global structure but potentially merging distinct small clusters together. Practitioners must experiment with this value, as an improper setting can lead to misleading visualizations that do not accurately represent the underlying data manifold.

How do non-linear dimensionality reduction techniques handle the 'curse of dimensionality' compared to linear methods?

Linear methods like PCA struggle when the data lies on a complex, non-linear manifold, as they force a linear projection that may collapse critical structures. Non-linear techniques like t-SNE and UMAP mitigate the curse of dimensionality by focusing on local neighborhoods rather than global coordinate axes. By modeling the relationships through local connectivity or probability distributions, they allow the algorithm to 'unroll' or 'unfold' the manifold. This preserves the meaningful relationships between points that would otherwise be lost in a linear subspace projection, making these tools indispensable for deep learning feature analysis and high-dimensional clustering workflows.

All Machine Learning interview questions →

Check yourself

1. Which statement best describes the fundamental difference in how t-SNE and UMAP handle global structure?

  • A.t-SNE uses a t-distribution to model similarities, while UMAP uses a Riemannian manifold approximation.
  • B.t-SNE is strictly deterministic, while UMAP is stochastic.
  • C.UMAP creates a fuzzy topological representation of the data, while t-SNE emphasizes only pair-wise local distances.
  • D.t-SNE is computationally cheaper because it uses graph-based optimization, unlike UMAP.
Show answer

C. UMAP creates a fuzzy topological representation of the data, while t-SNE emphasizes only pair-wise local distances.
UMAP is built on a solid theoretical framework of manifold learning and fuzzy simplicial sets that better preserve global structure than t-SNE. The first option is partially correct regarding the distribution, but misses the core point; the second is false (both are stochastic); the fourth is incorrect as UMAP is generally faster than standard t-SNE.

2. If you increase the 'perplexity' hyperparameter in t-SNE, what is the expected outcome?

  • A.The algorithm will focus strictly on the noise, ignoring cluster structure.
  • B.The algorithm will balance local and global structure by considering a larger set of neighboring points.
  • C.The computational complexity will decrease linearly.
  • D.The model will force the data into a strictly linear projection.
Show answer

B. The algorithm will balance local and global structure by considering a larger set of neighboring points.
Perplexity is a measure of the effective number of neighbors; increasing it allows each point to see more of its surroundings, which forces the model to respect broader global structures. The other options are incorrect because perplexity does not influence linear constraints or computational speed linearly.

3. What is the primary reason why t-SNE/UMAP visualizations are often criticized in a rigorous scientific setting?

  • A.They are unable to process data with more than 100 features.
  • B.They provide no mechanism to explain why certain points were clustered together.
  • C.They are sensitive to the random seed and hyperparameter choices, which can create 'phantom' structures.
  • D.They require labeled data to perform the projection.
Show answer

C. They are sensitive to the random seed and hyperparameter choices, which can create 'phantom' structures.
Because these methods are stochastic, running them with different seeds can lead to significantly different visual clusterings, making them prone to 'cherry-picking' results. Other options are false: they handle high dimensions well, they are unsupervised, and they don't explicitly require labels.

4. Why is it generally discouraged to use t-SNE or UMAP as a feature extraction step for training a supervised regression model?

  • A.The output dimensions of these models are not continuous.
  • B.These methods create non-linear transformations that are not easily interpretable or generalizable to test data.
  • C.Regression models only accept data that has been scaled using standard deviation.
  • D.These methods remove all variance from the data, leaving nothing for the regression model to learn.
Show answer

B. These methods create non-linear transformations that are not easily interpretable or generalizable to test data.
Non-linear manifold learning transforms the input space in ways that are specific to the training set; applying these to new test data is not straightforward, and the resulting coordinates are often complex combinations of features. The other options are either false (they produce continuous output) or misunderstanding the purpose of variance.

5. Which of the following scenarios is ideal for choosing UMAP over t-SNE?

  • A.When the dataset contains fewer than 50 samples.
  • B.When you need a deterministic result every time with the same parameters.
  • C.When you want to balance computational efficiency with preservation of global data structure.
  • D.When your goal is to find the principal components of the data.
Show answer

C. When you want to balance computational efficiency with preservation of global data structure.
UMAP is generally faster and better at preserving global structure than t-SNE, making it ideal for larger datasets where global relationships matter. Small datasets don't strictly prefer UMAP, it is not strictly deterministic, and PCA is the correct choice for principal components.

Take the full Machine Learning quiz →

← PreviousDimensionality Reduction — PCANext →Classification Metrics — Accuracy, Precision, Recall, F1

Machine Learning

35 lessons, free to read.

All lessons →

Track your progress

Sign in to mark lessons done, score quizzes and keep notes.

Open in the app