Skip to content
ML Visualization

t-SNE

Unsupervised & Dim. ReductionAdvanced~9 min

t-SNEEmbed 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
KL divergence vs iteration
10.20Max kl divergence on axis: 10.16

t-SNE controls

Data
Source data

Three well-separated blobs in six dimensions — the case t-SNE handles cleanly.

Shape60 × 6 → 2
Model
18
120
×4.0
80 iters
300
Playback
Step 0 / 38
Speed
  1. Early exaggeration
  2. Relax

Step 0 of 38 — iteration 0 — P is still inflated ×4, so points are being hauled into tight clumps; KL 10.16

Inspect
5

Hover any point: its true nearest neighbours in 6-D are linked. Amber links landed nearby here too; red links are real neighbours the layout flung away.

Break it

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):

KL(PQ)=ijpijlogpijqij\mathrm{KL}(P\,\|\,Q) = \sum_{i \ne j} p_{ij} \log \frac{p_{ij}}{q_{ij}}
pijp_{ij}
high-dimensional neighbor probability (Gaussian, set by perplexity).
qijq_{ij}
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.

  1. Step 1 — Gaussian similarity as seen from x1

    p21=e1/2e1/2+e4/2=0.60650.6065+0.1353=0.8176p_{2|1} = \frac{e^{-1/2}}{e^{-1/2} + e^{-4/2}} = \frac{0.6065}{0.6065 + 0.1353} = 0.8176

    Each point spreads a Gaussian over the others and normalizes — a probability distribution over its neighbors.

  2. Step 2 — symmetrize into the joint distribution P

    p12=p21+p122n=0.8176+0.88086=0.2831p_{12} = \frac{p_{2|1} + p_{1|2}}{2n} = \frac{0.8176 + 0.8808}{6} = 0.2831

    Seen from x2 the same pair looks even closer (its other option is at squared distance 5), so symmetrizing blends both views.

  3. Step 3 — Student-t similarity in the 2-D layout

    q12=(1+y1y22)1kl(1+ykyl2)1=1/52(0.2+0.1+0.5)=0.21.6=0.125q_{12} = \frac{(1 + \lVert y_1 - y_2 \rVert^2)^{-1}}{\sum_{k \ne l} (1 + \lVert y_k - y_l \rVert^2)^{-1}} = \frac{1/5}{2(0.2 + 0.1 + 0.5)} = \frac{0.2}{1.6} = 0.125

    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.

  4. Step 4 — the KL divergence sees the mismatch

    (1,2)p12logp12q12=2×0.2831×log0.28310.1250=2×0.2831×0.81740.463\sum_{(1,2)} p_{12} \log\frac{p_{12}}{q_{12}} = 2 \times 0.2831 \times \log\frac{0.2831}{0.1250} = 2 \times 0.2831 \times 0.8174 \approx 0.463

    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?
Perplexity roughly sets how many neighbors each point considers when t-SNE builds its similarity structure, balancing attention between local and broader relationships. Low values emphasize very local structure and can fragment the data, while high values smooth it out. Typical values fall between five and fifty, and comparing a few is wise.
Why do the distances between clusters in my t-SNE plot look meaningless?
They largely are. t-SNE preserves local neighborhoods, not global geometry, so the gaps between separated clusters carry little information and should not be read as a measure of how different the groups are. Focus on which points sit together, not on how far apart the clusters appear.
Do the sizes of clusters in a t-SNE plot mean anything?
No. t-SNE adapts to local density, effectively expanding sparse regions and compressing dense ones, so a large-looking blob is not necessarily a bigger or more spread-out group. Comparing apparent cluster sizes or densities on a t-SNE map is unreliable.
Why do I get a different plot every time I run t-SNE?
t-SNE optimizes its layout from a random initialization using gradient descent, so different runs settle into different but often equally valid arrangements. Fixing the random seed makes a run reproducible. The relative neighbor structure is usually more stable than the exact positions and orientation.
Can I use t-SNE to reduce dimensions before training a model?
It is generally not recommended. t-SNE is designed for visualization in two or three dimensions and does not produce a reusable mapping that new data can be pushed through consistently. For dimensionality reduction feeding a model, PCA or UMAP are far more appropriate.

Written & reviewed by the ML Visualization team · Last updated .