Sub topic 15
t-SNE versus UMAP: differences that matter
This chapter assumes you know how t-SNE builds probabilities from distances (with perplexity) and how UMAP builds a weighted neighbour graph (with n_neighbors). Here we compare what the two methods actually do differently, so you can choose between them.
Same goal, different machinery
Both methods keep each point's high-dimensional neighbours close in 2-D. They differ in what they compare and how they optimise.
| Aspect | t-SNE | UMAP |
|---|---|---|
| Input similarities | Gaussian probabilities, with bandwidth set by perplexity | Fuzzy graph weights from k nearest neighbours |
| Low-dimensional kernel | Student-t with one degree of freedom | A curve controlled by `min_dist` |
| Loss | KL divergence | Cross-entropy between graph weights |
| Optimisation | Gradient descent on all pairs, approximated | Attraction along edges, repulsion from sampled negatives |
| Default initialisation | Random in the original paper; PCA in scikit-learn when `init="pca"` | Spectral layout of the graph |
| Embedding new points | Not in scikit-learn | `transform` is available |
What changes in practice
Speed. UMAP usually runs faster on large datasets, because it only touches graph edges plus a few sampled non-neighbours. Barnes-Hut t-SNE scales as roughly N log N. FFT-based implementations such as openTSNE narrow the gap.
Global structure. UMAP plots often look as if they keep more large-scale arrangement. Much of this comes from the spectral initialisation, not from the loss itself. t-SNE with PCA initialisation keeps more global layout than random initialisation does. Treat distances between clusters as unreliable in both methods.
Cluster appearance. t-SNE tends to give round, evenly spread clusters. UMAP gives tighter, more separated clusters, and min_dist controls how tightly it packs points. Tight separation can make weak structure look convincing.
Reuse. UMAP can fit on training data and then place unseen points into the same map. If you need that, scikit-learn's TSNE is not an option.
Reproducibility. Both depend on the seed. In UMAP, setting random_state makes runs repeatable but disables some parallelism, which slows it down.
Comparing both on one dataset
import time
import umap
from sklearn.datasets import load_digits
from sklearn.manifold import TSNE, trustworthiness
X, y = load_digits(return_X_y=True)
models = [
("t-SNE", TSNE(init="pca", learning_rate="auto", random_state=0)),
("UMAP", umap.UMAP(n_neighbors=15, min_dist=0.1, random_state=0)),
]
for name, model in models:
start = time.perf_counter()
Z = model.fit_transform(X)
seconds = time.perf_counter() - start
score = trustworthiness(X, Z, n_neighbors=10)
print(f"{name}: {seconds:.1f}s, trustworthiness = {score:.3f}")On 1,797 points the timing gap is small. Expect it to grow with the number of points. Both trustworthiness scores should be high, and the difference between them is usually small. A score tells you about local neighbourhoods only, so it cannot settle which plot has the "right" global layout.
Exercise
You fit an embedding on last month's customer data and now need to place this month's customers on the same map. Which method fits, and why?
Show answer
UMAP. UMAP.fit followed by transform places new points into the existing embedding. scikit-learn's TSNE has no transform, so you would have to refit on all the data, which produces a new layout.