Two tools for looking, not for modelling

t-SNE and UMAP exist to turn a high-dimensional dataset into a two-dimensional picture a human can look at. That is the whole job. Both are non-linear, both preserve local neighbourhoods rather than global geometry, and both will happily produce a convincing plot from data that has no structure at all. If you have reached for either expecting a feature transform to feed a classifier, stop: use principal component analysis instead, which is linear, invertible and cheap to apply to new rows.

The difference between them in one line: t-SNE matches pairwise similarity distributions between the original and embedded spaces by minimising a Kullback-Leibler divergence, while UMAP builds a weighted neighbour graph and optimises a low-dimensional layout of that graph. In practice they give similar pictures and differ in runtime, in how much global structure survives, and in how they treat new data.

Here is a support desk’s ticket data: 2,400 tickets, fifty engineered features each, six topics, with two of the six deliberately built almost on top of each other.

import time
import numpy as np, pandas as pd
from sklearn.preprocessing import StandardScaler

rng = np.random.default_rng(7)
n_per, d = 400, 50
centres = rng.normal(0, 2.2, (6, d))
centres[5] = centres[4] + rng.normal(0, 0.35, d)   # topics 5 and 6 nearly identical
X = np.vstack([c + rng.normal(0, 1.0, (n_per, d)) for c in centres])
topic = np.repeat(np.arange(1, 7), n_per)
X = StandardScaler().fit_transform(X)
print(X.shape, len(np.unique(topic)))
# (2400, 50) 6

Scaling matters as much here as anywhere else, since both methods work from distances. The scaling chapter covers why.

What t-SNE and UMAP cost, measured

Pictures are hard to compare in prose, so score them. trustworthiness asks what fraction of each point’s k nearest neighbours in the embedding were also among its nearest neighbours in the original space. One is perfect, 0.5 is roughly chance.

from sklearn.decomposition import PCA
from sklearn.manifold import TSNE, trustworthiness
import umap

t0 = time.perf_counter()
emb_pca = PCA(n_components=2, random_state=0).fit_transform(X)
print(round(time.perf_counter() - t0, 1),
      round(float(trustworthiness(X, emb_pca, n_neighbors=12)), 4))
# 0.0 0.8461

t0 = time.perf_counter()
emb_tsne = TSNE(n_components=2, perplexity=30, init="pca",
                learning_rate="auto", random_state=0).fit_transform(X)
print(round(time.perf_counter() - t0, 1),
      round(float(trustworthiness(X, emb_tsne, n_neighbors=12)), 4))
# 7.2 0.9501

for attempt in (1, 2):
    t0 = time.perf_counter()
    emb_umap = umap.UMAP(n_components=2, n_neighbors=15, min_dist=0.1,
                         random_state=0).fit_transform(X)
    print(attempt, round(time.perf_counter() - t0, 1),
          round(float(trustworthiness(X, emb_umap, n_neighbors=12)), 4))
# 1 14.6 0.9226
# 2 4.1 0.9226
Method Runtime Trustworthiness (k=12) Handles new rows
PCA under 0.1s 0.8461 yes, transform
t-SNE 7.2s 0.9501 no
UMAP 14.6s cold, 4.1s warm 0.9226 yes, transform

Two results worth sitting with. t-SNE beat UMAP on neighbourhood fidelity, 0.9501 against 0.9226, which runs against the usual claim that UMAP is simply better. And UMAP’s first fit took 14.6 seconds against t-SNE’s 7.2, because umap-learn compiles its numerical kernels with Numba on first use. The second fit took 4.1 seconds. Quote the warm number when you compare, and the cold one when you are budgeting a one-off script.

PCA at 0.8461 is the honest baseline nobody reports. It cost nothing, it is reversible, and it already recovered most of the neighbourhood structure. On data this cleanly clustered, run PCA first and only escalate if the picture is genuinely muddled.

The parameters that change the picture

t-SNE’s perplexity is a soft guess at how many neighbours each point has. UMAP’s n_neighbors plays the same role, and min_dist controls how tightly points may pack together.

for p in (5, 30, 100):
    e = TSNE(n_components=2, perplexity=p, init="pca",
             learning_rate="auto", random_state=0).fit_transform(X)
    print(p, round(float(trustworthiness(X, e, n_neighbors=12)), 4),
          round(float(e.std()), 1))
# 5 0.9373 36.9
# 30 0.9501 19.5
# 100 0.9361 14.1

for nn in (5, 15, 50):
    e = umap.UMAP(n_components=2, n_neighbors=nn, min_dist=0.1,
                  random_state=0).fit_transform(X)
    print(nn, round(float(trustworthiness(X, e, n_neighbors=12)), 4))
# 5 0.9232
# 15 0.9226
# 50 0.9225

Perplexity 5 and perplexity 100 both score worse than 30, and the embedding’s overall spread drops from 36.9 to 14.1 as perplexity rises. The axes carry no units, so that spread is an artefact of the optimisation, not a finding about the tickets. UMAP’s n_neighbors barely moved trustworthiness at all here, 0.9232 down to 0.9225 across a tenfold change. On well separated clusters the parameter changes how the plot looks far more than how faithful it is. On tangled data it matters much more, so sweep it rather than trusting a default.

min_dist is the one with no t-SNE equivalent. Push it towards zero and UMAP packs each neighbourhood into a tight knot, which flatters a presentation slide and hides how much the groups overlap. Raise it towards 0.5 and the clouds breathe. It changes only the drawing, never the neighbour graph underneath, so pick it for legibility and say which value you used.

Always set init="pca" for t-SNE. Random initialisation makes the layout depend on the seed and throws away the global arrangement for no benefit.

How to misread one of these plots

Four readings that are not supported by the method:

  • Cluster sizes. Both algorithms expand sparse regions and compress dense ones. A blob twice the width does not hold twice the points.
  • Distances between clusters. Gaps are not proportional to anything in the original space. Two groups drawn far apart may be adjacent in fifty dimensions.
  • Clusters as discoveries. These are visualisations, not clustering algorithms. If you want labelled groups you can defend, run k-means or a density method on the original features and colour the embedding by the result.
  • Structure in noise. Feed t-SNE pure Gaussian noise at low perplexity and it will draw you tidy islands.

Topics 5 and 6, built almost on top of one another, show up in every embedding as a single elongated mass rather than two groups. That is correct behaviour and a useful reminder: the plot cannot invent a separation the features do not contain. The UMAP documentation works through the parameter effects visually, and scikit-learn’s manifold learning guide covers the t-SNE implementation.

Frequently Asked Questions

Is UMAP better than t-SNE?

Not automatically. On this dataset t-SNE scored higher on trustworthiness, 0.9501 against 0.9226. UMAP’s real advantages are a transform method for new rows and better scaling to hundreds of thousands of points. For a single plot of a few thousand rows, either is fine.

Can I use t-SNE output as model features?

No. The embedding has no transform for unseen data, the axes are arbitrary, and the layout shifts with the seed. If you want fewer columns for a model, use PCA or a feature selection method.

Why does my plot change every time I run it?

Both methods optimise a non-convex objective from a random start. Set random_state for reproducibility and init="pca" for t-SNE. A layout that changes character between seeds is telling you the structure is weak.

Key Takeaways

  • Treat both methods as visualisation only, and never feed t-SNE coordinates into a model, since there is no way to transform new rows.
  • Run PCA first as a baseline: it scored 0.8461 trustworthiness here for effectively zero runtime.
  • Measure embeddings with trustworthiness rather than judging them by eye, and sweep perplexity or n_neighbors before trusting one picture.
  • Ignore cluster sizes and inter-cluster gaps in the output, because both are artefacts of the optimisation.
  • Budget UMAP’s first call separately from the rest, since Numba compilation made the cold fit roughly three times the warm one.