ML0075 t-SNE vs PCA

What is t-SNE, and when should you use it instead of PCA?

Answer

t-SNE (t-distributed stochastic neighbor embedding) is a non-linear visualization method that turns pairwise distances in high-dimensional space into neighborhood probabilities, then arranges low-dimensional points so their own neighborhood probabilities, computed with a heavy-tailed Student-t kernel, match them by minimizing the KL divergence between the two distributions. Use it instead of PCA when the goal is a 2D map that reveals cluster structure: PCA preserves global variance along linear axes, while t-SNE sacrifices global geometry to keep close neighbors close. Do not use it as a preprocessing step for a downstream model: it is transductive (no transform for new points), stochastic across runs, and its cluster sizes and separations are not quantitative.

(1) What It Optimizes: match high-dimensional affinities p_{ij} (Gaussian, calibrated per point) with low-dimensional affinities q_{ij} (Student-t) via KL divergence; the heavy t-tails let dissimilar points sit far apart without penalty, solving the crowding problem.
(2) Perplexity Is the Knob: it acts as the effective neighbor count per point. Too small fragments clusters, too large merges them; 5 to 50 is the usual range, and results vary with the random seed.
(3) Production Pattern: PCA first, neighbor embedding second. 10x Genomics’ Cell Ranger pipeline runs PCA on gene expression, then computes t-SNE or UMAP projections for its Loupe Browser; UMAP scales better than t-SNE on large cell counts, which is why it has become the default projection at that scale.

Same three-cluster data embedded with PCA on the left showing partially overlapping clusters and with t-SNE on the right showing cleanly separated clusters

Figure 1: The same clustered data two ways: PCA (left) preserves global spread but overlaps clusters that are not linearly separable; t-SNE (right) separates them cleanly. The t-SNE panel’s axes and inter-cluster gaps carry no units and no meaning.

Mathematical Formulation:
p_{j \mid i} \propto \exp\Big(-\frac{\|x_i - x_j\|^2}{2 \sigma_i^2}\Big)
q_{ij} \propto \big(1 + \|z_i - z_j\|^2\big)^{-1}
\mathcal{L} = \sum_{i,j} p_{ij} \log \frac{p_{ij}}{q_{ij}}

Where:

  • p_{j \mid i} is the probability that x_i picks x_j as a neighbor; the per-point \sigma_i is set so the perplexity matches the chosen value, and the conditionals are symmetrized into p_{ij}.
  • q_{ij} is the same affinity computed on the map points z_i with a Student-t kernel (1 degree of freedom), whose heavy tails prevent crowding.
  • \mathcal{L} is the KL divergence between the two affinity matrices, minimized by gradient descent directly on the map coordinates.
Featuret-SNEPCA
TypeNon-linear, neighbor-graph basedLinear orthogonal projection
PreservesLocal neighborhoodsGlobal variance and large-scale structure (projection can only shrink distances)
Output2-3D coordinates only, no transformReusable projection, any k
DeterminismStochastic; reruns differDeterministic up to sign flips
Right UseFinal-step cluster visualizationPreprocessing, compression, de-correlation
Three t-SNE embeddings of the same data at perplexity 5 showing fragmented clusters, perplexity 30 showing clean clusters, and perplexity 100 showing merged structure

Figure 2: Perplexity sweeps the effective neighborhood size: at 5 the clusters fragment into fake sub-clusters, at 30 the structure is clean, at 100 distinct clusters merge. Always check a second perplexity before trusting a t-SNE picture.


Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *