t-SNE & UMAP for Visualization
Project high-D embeddings for exploration while respecting neighbor preservation caveats.
In plain words
t-SNE and UMAP squash data with many columns into a 2D picture so that similar items land near each other, which is great for looking but not for measuring.
Think of it like…
Flattening a globe onto a map: cities that are close stay close, but the distances between continents and the sizes of regions get distorted, and a different projection tells a slightly different story.
Where you’ve seen it
The perplexity of t-SNE is 2^H, where H is the entropy (in bits) of a point's neighbour distribution: if a point spreads its attention evenly over 30 neighbours, H = log₂ 30 = 4.907 bits and the perplexity is 2^4.907 = 30, so perplexity 30 means about 30 effective neighbours. For 70,000 images with 784 pixels each, the team first reduces to 50 dimensions with PCA (to denoise and speed up), then runs t-SNE with perplexity 30 (a few minutes on CPU) and UMAP with n_neighbors = 15, min_dist = 0.1 (well under a minute). Both maps show ten clusters, but the relative distances between clusters differ between the two plots and between perplexities 5, 30 and 100, which warns against reading distances. Inspecting a small island of the "7" cluster inside the "1" cluster reveals 41 mislabelled images, a concrete use. They also project 20,000 text-embedding vectors coloured by customer segment to confirm that support tickets about billing form a distinct region, then validate the claim with a classifier instead of the picture alone.
Words you’ll hear
- Perplexity
- Effective number of neighbours in t-SNE, 2^entropy; typical values 5–50.
- n_neighbors and min_dist
- UMAP parameters controlling local versus global structure and how tightly points pack.
- Neighbourhood preservation
- Keeping points that are near in high dimensions near in the map.
- Trustworthiness
- A score of how many map neighbours are true neighbours in the original space.
Visual diagram
Theory & deep dive
How they work and how to set them
t-SNE converts distances to conditional probabilities: for each point it sets a Gaussian bandwidth so that the entropy of its neighbour distribution matches the chosen perplexity (2^H: a uniform distribution over 30 neighbours has H = log₂ 30 = 4.907 bits and perplexity 30). In the low-dimensional map it uses a heavy-tailed Student-t distribution (which lets clusters separate and relieves crowding) and minimises the KL divergence between the high- and low-dimensional similarity distributions by gradient descent, with early exaggeration to form clusters first. Use perplexity 5 to 50 (smaller emphasises very local structure, larger smooths), at least 1,000 iterations, learning_rate="auto" and PCA initialisation for more stable layouts; Barnes-Hut or FFT approximations give roughly O(n log n) time.
UMAP builds a weighted k-nearest-neighbour graph (n_neighbors, default 15: low values focus on local detail, high values on broader structure), converts distances to fuzzy memberships and optimises a low-dimensional layout so the graph is preserved. min_dist (default 0.1) sets how tightly points can pack (small values give dense clumps, larger values spread them). UMAP is typically faster, scales to millions of points, keeps more global structure than t-SNE in practice (though not guaranteed) and can transform new points into an existing map (t-SNE cannot), which makes it usable in pipelines. For both: pre-reduce with PCA to about 50 dimensions, standardise or normalise inputs, choose the metric (cosine for text embeddings) and set random seeds.
A point spreads its neighbour probability evenly over 30 neighbours: H = log₂ 30 = 4.907 bits, perplexity = 2^4.907 = 30. If it concentrated on 8 neighbours, perplexity would be 8. The bandwidth is tuned per point so every point attends to about the same number of neighbours.
Reading the map correctly
What you can trust: local neighbourhoods (points close in the map are usually near neighbours in the original space) and the existence of well-separated groups that persist across seeds and parameters. What you can't: distances between clusters (two clusters drawn far apart may be close in the original space and vice versa), cluster sizes and densities (t-SNE equalises density, UMAP compresses it), axes and orientation (arbitrary) and shapes (apparent loops or tendrils may be artefacts). Perplexity 5, 30 and 100 can produce visibly different pictures from the same data; random seeds change layouts. So always run several settings and seeds, and treat features that appear in all of them as real.
Use them for diagnostics: checking that class labels form coherent regions, spotting mislabelled or outlying examples (an island of 41 mislabelled "7"s inside the "1" cluster), detecting batch or domain effects (clusters by source rather than by label) and understanding what a model's embeddings capture. Quantify what you see: trustworthiness and continuity scores (sklearn.manifold.trustworthiness), kNN classifier accuracy in the original space, and silhouette or purity in the original space rather than on the map. Don't feed t-SNE coordinates into a clustering algorithm or downstream model without validation, since they distort geometry; if you need low-dimensional features for modelling, UMAP (with care) or PCA is more appropriate. For large datasets, subsample for t-SNE (10,000 to 50,000 points) or use UMAP, and colour points by metadata to read the story.
Perplexity 5 splits one digit class into several small clumps; 30 shows ten clean clusters; 100 pulls two similar classes closer. The consistent finding across settings (ten groups, one confusing island) is the reliable part; the exact spacing is not.
How it works, step by step
Step 1 of 41. Prepare the data
Normalise or standardise inputs, choose a metric and reduce to about 50 dimensions with PCA.
- Cosine
- for text embeddings
When to use it & trade-offs
What it is
t-SNE and UMAP are nonlinear dimensionality-reduction methods that map high-dimensional points (embeddings, features) to 2D or 3D so you can see neighbourhood structure. Both build a graph of who is near whom in the original space and then arrange points in low dimensions so that neighbours stay neighbours. t-SNE minimises the divergence between pairwise-similarity distributions (using a heavy-tailed Student-t in the map), controlled by perplexity, roughly the number of neighbours each point attends to; UMAP builds a fuzzy topological graph and optimises its layout, controlled by n_neighbors and min_dist. They are for exploration and diagnostics: cluster sizes, between-cluster distances and axes in the picture are not reliable measurements.
Why it matters: Embeddings from language models, images or user behaviour have hundreds of dimensions that no one can plot. A 2D map quickly shows whether classes separate, whether a model has learned structure, where mislabelled or anomalous examples sit and whether batch effects dominate. But these plots are easy to over-interpret, so you need to know what they preserve (local neighbourhoods), what they distort (global distances and densities) and how parameters change the picture.
When to reach for it
- Exploring embeddings from text, images and users
- Spotting mislabelled examples and batch effects
- Explaining what a model has learned to stakeholders
When to avoid or be careful
- Interpreting distances between clusters
- Reading cluster size as population size
- Clustering or modelling directly on t-SNE coordinates
Visualisation methods
Strengths: Very good at revealing local groups.
Watch out: Slow on big data; no transform; global structure distorted.
Strengths: Speed; supports new points; often better global layout.
Watch out: Parameter sensitive; still distorts distances.
Strengths: Deterministic; preserves global variance.
Watch out: Misses nonlinear structure.
Interview signals & numbers to know
When this topic comes up
- “Visualise the embeddings”
- “What does perplexity do?”
- UMAP n_neighbors and min_dist
- Neighbourhood preservation
- Trustworthiness score
Benchmarks & rules of thumb
- 2^H
- perplexity from neighbour entropy; 2^4.907 = 30
- 5 – 50
- typical t-SNE perplexity range
- 15 / 0.1
- UMAP defaults for n_neighbors and min_dist
Common mistakes & follow-ups
Common interview mistakes
- Presenting one run as proof of structure.
- Running t-SNE on 500 raw dimensions without PCA.
- Believing a gap between clusters means the classes are far apart.
Likely interviewer follow-ups
- What does perplexity control in t-SNE?
- Why can't you trust distances between clusters in a t-SNE plot?
- When would you choose UMAP over t-SNE?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply t-SNE & UMAP for Visualization with data science interview Q&A and hands-on practice.