Stop 11 · Unsupervised LearningTopics · 7/10

t-SNE & UMAP for Visualization

Part of the Machine Learning Roadmap

In short

PCA draws straight-line shadows. t-SNE and UMAP bend and fold data to show who sits near whom, which is great for spotting clusters in images, text embeddings or genes.

t-SNE and UMAP squash high-dimensional data into 2D in a way that keeps similar points close, making clusters visible on a plot. They are for exploring and visualising, not for feeding distances into other models.

The mascot seats wedding guests at round tables, with friends grouped together.

Think of it like this

The wedding seating plan

Planning seats for 300 wedding guests, you put close friends and relatives at the same table. Tables of related families sit nearby. Nobody cares if the hall's exact distances match real-life closeness. Only 'who sits with whom' matters. That's what t-SNE and UMAP keep.

Watch it happen

t-SNE vs UMAP

t-SNE

  • Keeps close neighbours together
  • Slower on very large data
  • Key knob: perplexity

UMAP

  • Usually faster on big data
  • Can transform new points
  • Key knob: n_neighbors

Both are mainly for looking at data. Use UMAP for big datasets or when new points must be mapped, t-SNE for small ones. Never read too much into gaps between clusters.

The big ideas

What you need to know

Built for plotting

They squeeze many dimensions into 2 or 3 so you can see clusters. They're mostly not used as model features.

Local beats global

Neighbours stay neighbours. But distances between far-apart clusters, and cluster sizes, are not reliable.

Settings change the picture

Perplexity or n_neighbors controls how local the view is. Try a few values before trusting a pattern.

Runs can differ

Results vary with the random seed. Fix random_state so your plots can be reproduced.

See it in code

Walk through the code

python
1from sklearn.datasets import load_digits2from sklearn.manifold import TSNE3import matplotlib.pyplot as plt4 5X, y = load_digits(return_X_y=True)          # 64 pixel features6emb = TSNE(n_components=2, perplexity=30, random_state=0).fit_transform(X)7 8plt.scatter(emb[:, 0], emb[:, 1], c=y, cmap="tab10", s=5)9plt.colorbar()10plt.show()11# UMAP: pip install umap-learn, then umap.UMAP(n_neighbors=15).fit_transform(X)

1/3Each 8 by 8 image is 64 numbers.

1,797 handwritten digits with 64 pixels each, squeezed to 2D. Each digit usually forms its own island.

Save yourself the pain

Common mistakes

Oops

Saying 'cluster A is twice as far from B as from C' from a t-SNE plot.

Do this instead

Only trust who is near whom. Global distances and cluster sizes in t-SNE plots are not meaningful.

Oops

Feeding t-SNE output into a model as features for new data.

Do this instead

Use PCA, or UMAP's transform, when new points must be mapped the same way later.

Your mission

PCA vs t-SNE on digits

~25 min

Quick check

What can you safely read from a t-SNE plot?

Go deeper

  1. 1Plot the digits dataset in 2D with t-SNE and with UMAP.
  2. 2Change perplexity or n_neighbors and see how the picture changes.
  3. 3Avoid reading meaning into the distances between far-apart clusters.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge