Part VI — Unsupervised Learning, Clustering, Dimensionality & Time Series · Chapter 5

Non-linear dimensionality reduction: t-SNE

Hook

PCA only ever draws straight lines through your data — it can't bend to follow a curve. If two points are genuinely close in some high-dimensional, twisty sense but far apart along every straight axis, PCA has no way to notice. What kind of algorithm can?

Intuition
P1P2P3P4
P1↔P2 = 0.21, P3↔P4 = 0.21, P1↔P3 = 0.11

Two true pairs, P1P_1/P2P_2 and P3P_3/P4P_4, start jumbled together at random. After training, the pairs pull apart from each other and tighten internally — nothing here promises where each pair ends up, only that points that were neighbors stay neighbors.

Formalize

t-SNE converts distances into probabilities in both spaces, then makes the low-dimensional probabilities match the high-dimensional ones as closely as possible.

pijexp ⁣(xixj22σ2),qij11+yiyj2p_{ij} \propto \exp\!\left(-\frac{\lVert x_i - x_j \rVert^2}{2\sigma^2}\right), \qquad q_{ij} \propto \frac{1}{1 + \lVert y_i - y_j \rVert^2}
  • pijp_{ij} — how similar points i,ji,j are in the original high-dimensional space, from a Gaussian centered on each point.
  • qijq_{ij} — how similar their images yi,yjy_i, y_j are in the low-dimensional embedding, from a heavier-tailed Student-t distribution.
  • σ\sigma — the Gaussian's bandwidth (fixed here; real t-SNE solves for it per point to hit a target "perplexity").
  1. A heavier-tailed kernel in low dimensions, on purpose

    Squeezing high-dimensional neighborhoods into 2D leaves less room for everyone — the Student-t kernel's heavy tails let moderately-distant points spread out further in the embedding without being punished, easing that crowding.

  2. Minimize how much Q disagrees with P

    Gradient descent adjusts every embedded point yiy_i to make qijq_{ij} track pijp_{ij} as closely as possible — the KL divergence between the two distributions is what's actually being minimized.

  3. Only local structure is preserved

    Nothing in this objective cares about the absolute distance between two far-apart points, or which direction a cluster lands in — only whether true neighbors stay neighbors survives the compression.

Play
P1P2P3P4
KL divergence = 1.097

Drag the steps slider. KL divergence doesn't fall smoothly — early on it can briefly get worse before the optimizer finds a better arrangement, the same way loss curves elsewhere in this course occasionally tick upward before a bigger drop. Given enough steps, it settles low and stays there.

Worked example

Two tight pairs, (0,0)(0,0)(1,0)(1,0) and (20,0)(20,0)(21,0)(21,0), twenty units apart from each other:

  1. Within-pair affinity

    With σ=1\sigma=1, P1P_1's unnormalized Gaussian weights on P2P_2, P3P_3, P4P_4 are e12/20.607e^{-1^2/2}\approx0.607, e202/20e^{-20^2/2}\approx0, e212/20e^{-21^2/2}\approx0 — the far points contribute nothing measurable. Normalizing: p21=0.607/0.6071p_{2\mid1} = 0.607/0.607 \approx 1, and the same reasoning from P2P_2's side gives p121p_{1\mid2}\approx1. Symmetrizing over n=4n=4 points:

    p12=p21+p122n1+12×4=0.25p_{12} = \frac{p_{2\mid1}+p_{1\mid2}}{2n} \approx \frac{1+1}{2\times4} = 0.25

    and symmetrically p340.25p_{34} \approx 0.25.

  2. Cross-pair affinity is essentially zero

    p13p_{13}, between the two far-apart pairs, comes out indistinguishable from 00 — high-dimensional space has already declared these pairs unrelated.

  3. Training pulls each true pair together, blind to the original coordinates

    Neither P1P_1 nor P2P_2's training process ever sees the numbers 00, 11, 2020, or 2121 again once pijp_{ij} is computed — only the affinities. Yet gradient descent still finds an embedding where each pair ends up close and the two pairs end up apart, purely by matching qijq_{ij} to pijp_{ij}.

Checkpoint

Drag the training-steps slider until KL divergence drops below 0.1.

P1P2P3P4
KL divergence = 1.097
Drag steps to try it
Summary
pijexp ⁣(xixj22σ2),qij11+yiyj2p_{ij} \propto \exp\!\left(-\frac{\lVert x_i - x_j \rVert^2}{2\sigma^2}\right), \qquad q_{ij} \propto \frac{1}{1 + \lVert y_i - y_j \rVert^2}

t-SNE gives up everything PCA guarantees — no straight-line projection, no preserved absolute distances, no consistent orientation between runs — in exchange for one thing PCA can't do at all: following genuinely curved structure by preserving who's a neighbor of whom. That tradeoff is exactly why t-SNE plots are for looking, not measuring — the axes and distances between separated clusters carry no meaning, only the clustering itself does.