Foundations to Frontier
Tests
D. Classical Machine Learning › D9 Dimensionality Reduction and Manifold Learning

t-SNE

t-distributed stochastic neighbour embedding (van der Maaten and Hinton, 2008) is the most widely used method for visualising high-dimensional data in 2-D. It converts distances into neighbour probabilities, with a Gaussian in the original space whose width per point is set by a target perplexity (an effective number of neighbours), and a heavy-tailed Student-t in the embedding. It then moves the 2-D points to minimise the Kullback–Leibler divergence between the two. The heavy tail solves the crowding problem and produces strikingly separated clusters. The same design means t-SNE preserves who is near whom but not cluster sizes, densities or the distances between clusters, and its output depends on perplexity, initialisation and optimisation settings. This page covers perplexity calibration, the symmetric objective and its gradient, early exaggeration and momentum, the crowding problem, Barnes–Hut and FFT acceleration, and how to read and not misread t-SNE plots. In the exercises you implement t-SNE from scratch and check it against PCA on clustered data; then you measure how it equalises cluster sizes and distances at different perplexities.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

A map that keeps neighbours together

Imagine you must seat hundreds of guests at a flat table so that friends sit near friends. You can’t keep everyone’s exact distances, so you settle for this: whoever was close stays close. t-SNE does this for data points. It works out, for each point, which others are its near neighbours, then arranges points on a 2-D sheet so that the same points end up as neighbours. Groups of similar points become islands.

t-SNE: neighbours kept, sizes and distances lost
iteration 0 (early exaggeration)
PCA
size of C ÷ size of A: truth ≈ 4 · PCA 4.02 · t-SNE 0.00  |  distance A–C ÷ A–B: truth 3 · PCA 3.00 · t-SNE 0.00
Clusters A (blue, tight), B (green) and C (orange, four times wider and three times further from A than B is). t-SNE separates the clusters cleanly, but by the end they are the same size and roughly equally spaced: each point's neighbourhood is calibrated to the same perplexity, which erases density differences, and the heavy-tailed Student-t similarity pushes all well-separated clusters to similar distances. Large perplexity keeps a little more of the global layout. Read t-SNE plots for who is near whom, not for sizes or gaps.
Why engineers care

t-SNE is the standard first look at learned representations: neural-network embeddings, anomaly-detector features, image or telemetry clusters. It shows whether classes or failure modes form separate groups and which points are mixed up. It is equally famous for misleading readers about cluster sizes, gaps and counts, so using it well means knowing exactly what it preserves.

📝 Quick check (Level 0)
0/3 answered
  1. 1. t-SNE mainly preserves…
  2. 2. In the demo, cluster C is four times wider than A. In the t-SNE map it is…
  3. 3. Distances between t-SNE clusters…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Probabilities, perplexity and the t kernel

Neighbour probabilities. For point i, p_(j|i) ∝ exp(−‖xᵢ − xⱼ‖²/2σᵢ²), normalised over j ≠ i. The width σᵢ is chosen per point so that the distribution has a target perplexity, e^H with H the entropy in nats, which acts as an effective number of neighbours.

  • A uniform distribution over 4 neighbours has perplexity 4.
  • (0.5, 0.3, 0.2) has perplexity 2.80.
  • Dense regions get small σᵢ and sparse ones large σᵢ, so every point sees about the same number of neighbours.

Symmetrising. pᵢⱼ = (p_(j|i) + p_(i|j))/(2n), so the pᵢⱼ sum to 1 over all pairs and every point contributes at least 1/(2n). If p_(1|0) = 1 and p_(0|1) = 0.5 with n = 3, then p₀₁ = 1.5/6 = 0.25.

Embedding similarities. qᵢⱼ ∝ (1 + ‖yᵢ − yⱼ‖²)⁻¹, normalised over all pairs: a Student-t with one degree of freedom. At distance 3 the t kernel gives 1/10 = 0.1, while a Gaussian exp(−9/2) gives 0.011, nine times smaller. Moderately far pairs can sit far apart at little cost.

Objective. KL(P‖Q) = Σ pᵢⱼ log(pᵢⱼ/qᵢⱼ). It is 0 only if Q = P. KL([1, 0] ‖ [½, ½]) = ln 2 = 0.693.

The steps in plain English:

  1. Optionally reduce to about 50 dimensions with PCA, to cut noise and cost.
  2. For each point, binary-search σᵢ to hit the perplexity (5–50; 30 is a common default).
  3. Symmetrise to P; initialise Y small, ideally from PCA.
  4. Run gradient descent: about 250 iterations with early exaggeration (P multiplied by 12) and momentum 0.5, then plain P with momentum 0.8.
  5. Inspect neighbourhoods, and compare several perplexities and seeds before reading any structure.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Perplexity is…
  2. 2. t-SNE's embedding similarity uses…
  3. 3. KL(P‖Q) = 0 when…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

The gradient, crowding and what gets distorted

Gradient. ∂KL/∂yᵢ = 4 Σⱼ (pᵢⱼ − qᵢⱼ)(yᵢ − yⱼ)(1 + ‖yᵢ − yⱼ‖²)⁻¹. Each pair is a spring. It attracts when pᵢⱼ > qᵢⱼ (the pair should be closer) and repels otherwise, and the (1 + d²)⁻¹ factor weakens forces between far-apart points. The attractive part comes from P and acts locally; the repulsive part is spread across all pairs through the normalisation of Q.

Crowding. In 10 dimensions a point can have many equidistant neighbours; in 2-D there is not enough room. With a Gaussian Q (the original SNE), moderately distant points are crushed toward the centre. The t kernel’s heavy tail lets them move far out cheaply, which opens the gaps between clusters.

Exercise 1 (6 clusters in 10-D, 150 points, perplexity 20):

  • the binary search hit the target perplexity to 10⁻³ for every point;
  • KL fell from 1.43 to 0.178 over 400 iterations (0.172 in the browser: the optimisation amplifies rounding differences, though the final map is equally good);
  • 5-nearest-neighbour accuracy in 2-D was 0.987 for t-SNE against 0.813 for PCA, which cannot separate six clusters in two linear directions.

What t-SNE throws away.

  • Density and size: per-point σᵢ equalises neighbourhoods, so tight and loose clusters look alike.
  • Between-cluster distances: once clusters separate, the near-zero pᵢⱼ across them exert almost no force, and the repulsion places clusters at roughly similar distances.
  • Global shape: it depends on initialisation and perplexity; a PCA initialisation keeps more of it.
from sklearn.manifold import TSNE
Y = TSNE(n_components=2, perplexity=30, init="pca", learning_rate="auto", random_state=0).fit_transform(X)
🐍 Exercise 1: t-SNE from scratch
Calibrate per-point Gaussian widths to a target perplexity by binary search, symmetrise, and optimise the KL divergence with the Student-t kernel, early exaggeration and momentum; compare 5-NN accuracy of the 2-D map with PCA on six clusters.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Pairs with pᵢⱼ > qᵢⱼ are…
  2. 2. The crowding problem is that…
  3. 3. Initialising t-SNE from PCA tends to…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Early exaggeration is spectral clustering. Multiplying P by a large factor makes the attraction dominate. Linderman and Steinerberger (2019) and Arora et al. (2018) showed that in this phase the gradient flow behaves like power iteration on the graph defined by P, contracting each well-separated cluster toward a point. This is why t-SNE reliably separates clusters that the P-graph separates.

Scaling. Exact gradients cost O(n²) per iteration. Barnes–Hut t-SNE (van der Maaten, 2014) approximates the repulsion with a quadtree, at O(n log n). FIt-SNE (Linderman et al., 2019) interpolates on a grid with the FFT, at about O(n), and handles millions of points. The attractive term uses a sparse k-NN P with k ≈ 3 × perplexity.

Perplexity and the global–local trade-off. Small perplexity focuses on very local structure and can fragment clusters into spurious islands; large perplexity approaches a more global view. Very large perplexity with strong exaggeration makes t-SNE resemble Laplacian eigenmaps (Böhm, Berens and Kobak, 2022), placing t-SNE, UMAP and spectral methods on one attraction–repulsion spectrum.

Interview questions.

  • What can you conclude from a t-SNE plot? Which points and groups are neighbours, and whether separable groups exist. Not cluster sizes, densities, the distances between clusters, or the number of clusters without checks across perplexities and seeds.
  • How should t-SNE be run responsibly? Run PCA first, initialise from PCA, set the learning rate to n/12, use several perplexities and seeds, and verify any claimed cluster in the original space.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Early exaggeration behaves like…
  2. 2. Barnes–Hut t-SNE costs about…
  3. 3. Very small perplexity risks…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

How much of the geometry survives?

History.

  • G. Hinton and S. Roweis (2002) introduced stochastic neighbour embedding (SNE).
  • L. van der Maaten and G. Hinton (2008) introduced t-SNE, with the heavy tail and symmetric P.
  • L. van der Maaten (2014) introduced Barnes–Hut t-SNE, and G. Linderman et al. (2019) FIt-SNE.
  • M. Wattenberg, F. Viégas and I. Johnson (2016) wrote the widely read Distill guide to misreadings.
  • D. Kobak and G. Linderman (2021) showed that initialisation, not the algorithm, explains most differences in global structure.

Exercise 2. Three Gaussian clusters in 10-D, 60 points each, with spreads 0.5, 1 and 2. Cluster B is 10 from A and cluster C is 30 from A; 2 datasets. The table compares the RMS radius of cluster C (and B) to cluster A’s, and the centroid distance A–C to A–B.

map size C/A (truth ≈ 4) size B/A (truth ≈ 2) distance AC/AB (truth 3)
original 10-D 4.28 2.06 3.02
PCA 4.58 2.11 3.02
t-SNE, perplexity 5 0.96† 0.96† 1.06†
t-SNE, perplexity 30 1.00 1.00 1.02
t-SNE, perplexity 60 1.12 1.32† 1.30†

† t-SNE’s optimisation magnifies tiny rounding differences, so details vary by platform: in the browser, perplexity 5 gives 1.01, 1.12 and 1.12, and perplexity 60 gives 1.33 and 1.11 for the marked cells. The conclusions are unchanged.

  • t-SNE made all three clusters the same size, although C is four times as spread out as A, and placed them almost equidistant, although C is three times further away. PCA kept both ratios.
  • At perplexity 60, equal to the cluster size, a little global structure leaked back (ratios 1.1–1.3 on either platform): each point’s neighbourhood then spans its whole cluster, so the cluster’s scale enters the P matrix.
  • Cluster separation itself was perfect in every t-SNE run; only sizes and spacing were erased.
🐍 Exercise 2 (research): what t-SNE erases
Three clusters with spreads 0.5, 1, 2 at distances 10 and 30: compare cluster-size and between-cluster distance ratios in the original space, PCA and t-SNE at perplexities 5, 30 and 60.

t-SNE in AI, robotics and aerospace

  • Inspecting learned features: check whether a perception network’s embeddings separate object classes, or whether anomaly features separate fault types.
  • Telemetry exploration: spot distinct operating regimes or rare events in high-dimensional sensor logs, then confirm them in the original space.
  • Dataset auditing: find mislabelled or out-of-distribution samples sitting inside the wrong island.

References

  1. L. van der Maaten, G. Hinton (2008). “Visualizing data using t-SNE.” Journal of Machine Learning Research 9.
  2. G. E. Hinton, S. T. Roweis (2002). “Stochastic neighbor embedding.” Advances in NeurIPS 15.
  3. L. van der Maaten (2014). “Accelerating t-SNE using tree-based algorithms.” Journal of Machine Learning Research 15.
  4. G. C. Linderman, S. Steinerberger (2019). “Clustering with t-SNE, provably.” SIAM Journal on Mathematics of Data Science 1(2).
  5. D. Kobak, G. C. Linderman (2021). “Initialization is critical for preserving global data structure in both t-SNE and UMAP.” Nature Biotechnology 39.
  6. M. Wattenberg, F. Viégas, I. Johnson (2016). “How to use t-SNE effectively.” Distill.