t-SNE
t-SNE — Embed high-dimensional data in 2D preserving local neighborhoods.
t-SNE squeezes high-dimensional data down to 2D for visualization by keeping nearby points nearby. It reveals clusters beautifully — but its knobs are easy to misread.
- Cluster 0
- Cluster 1
- Cluster 2
- True neighbour kept close
- True neighbour flung away
t-SNE controls
The idea in plain words
t-SNE squeezes high-dimensional data down to 2-D for visualization by keeping nearby points nearby. Watch separated clusters emerge from a random blob, frame by frame — the archetypal “impressive” ML animation, computed live.
It reveals clusters beautifully but its knobs mislead: the perplexity sets the effective neighborhood size, and the distances and sizes between clusters are not meaningful. Unlike PCA, it’s nonlinear and non-deterministic.
Now, the math
t-SNE minimizes the KL divergence between neighbor distributions P (high-D) and Q (2-D):
- high-dimensional neighbor probability (Gaussian, set by perplexity).
- low-dimensional neighbor probability (heavy-tailed Student-t).
▸ Show the derivation
The heavy-tailed Student-t in 2-D lets moderately distant points spread out, avoiding crowding. Gradient descent on the KL pulls together points that are neighbors in high-D and pushes apart the rest. Too-low perplexity focuses on tiny neighborhoods and shatters real clusters into fake blobs. (This build is a simplified, teaching-scale t-SNE.)
Trace it by hand
Three points whose high-dimensional squared distances are d12 = 1, d13 = 4, d23 = 5, with a fixed Gaussian width sigma = 1 for every point (the real algorithm tunes sigma per point to hit a target perplexity). Values rounded to 4 decimal places.
Step 1 — Gaussian similarity as seen from x1
Each point spreads a Gaussian over the others and normalizes — a probability distribution over its neighbors.
Step 2 — symmetrize into the joint distribution P
Seen from x2 the same pair looks even closer (its other option is at squared distance 5), so symmetrizing blends both views.
Step 3 — Student-t similarity in the 2-D layout
Layout used: y1 = 0, y2 = 2, y3 = 3 on a line, so squared gaps are 4, 9 and 1. The kernel 1 over 1 plus squared distance has heavy tails.
Step 4 — the KL divergence sees the mismatch
p12 is more than double q12: the pair is closer in high-D than the layout shows, so the gradient will pull y1 and y2 together.
What just happened: One pair told the whole story: p12 = 0.2831 in high-D but only q12 = 0.125 in the layout, so this pair alone contributes 0.463 to the KL cost — gradient descent reduces it by dragging the two points together, which is all t-SNE ever does.
Now Break It
Try this: Wrong perplexity fractures real clusters or invents fake ones; distances between clusters are meaningless.
Control: Perplexity slider (set very low)
What happens: Misleading embedding! Low perplexity shatters clusters into fake sub-blobs — cluster sizes and gaps are not real.
Where t-sne is used
t-SNE is a nonlinear technique built specifically to visualize high-dimensional data in two or three dimensions by preserving which points are neighbors. It has become the default for peering inside complex datasets: single-cell RNA sequencing studies use it to reveal distinct cell types as separated islands, machine learning practitioners plot the learned embeddings of images or words to see whether similar items cluster, and it helps inspect the internal representations of neural networks. Its strength is local fidelity; by converting distances into neighbor probabilities and matching them in the low-dimensional map, t-SNE keeps genuinely similar points close together, producing the visually crisp, well-separated blobs that make patterns in otherwise inscrutable data immediately apparent to the human eye.
The most important caution is that t-SNE plots must be read carefully. Distances between well-separated clusters are not meaningful, so two groups drawn far apart are not necessarily more different than two drawn close, and cluster sizes and densities in the plot do not reflect true sizes because the method equalizes local density. The perplexity setting strongly changes the picture, and small values can manufacture apparent clusters that are artifacts, so multiple perplexities should be compared. t-SNE is also stochastic, giving different layouts across runs, and it is primarily a visualization tool, not a general-purpose feature transformer, since it does not provide a stable mapping for new points.
Frequently asked questions
What does the perplexity parameter control?
Why do the distances between clusters in my t-SNE plot look meaningless?
Do the sizes of clusters in a t-SNE plot mean anything?
Why do I get a different plot every time I run t-SNE?
Can I use t-SNE to reduce dimensions before training a model?
Written & reviewed by the ML Visualization team · Last updated .