Core ML2008intermediate9 min read

Visualizing Data Using t-SNE

تصوير البيانات باستخدام t-SNE

van der Maaten, L. · Hinton, G. — JMLR

The problem

High-dimensional data — images, gene expressions, word embeddings — live in spaces with hundreds or thousands of dimensions. Humans can only see two or three. Linear methods like project data along axes of maximum , but they cannot unfold the curved, nonlinear manifolds where real data lives. Clusters that are clearly separated in high dimensions get crushed together in PCA's flat projection.

The contribution

converts pairwise distances in high-dimensional space into conditional probabilities using Gaussian kernels, then defines a matching set of probabilities in 2D using a heavy-tailed . It minimizes the between the two distributions via . The heavy tail solves the "" — moderately distant points are no longer forced into tight clusters — producing maps with clearly separated clusters and preserved local neighborhood structure.

The impact

t-SNE became the default visualization tool for high-dimensional data in machine learning, bioinformatics, and NLP. It revealed structure in word embeddings, single-cell RNA-seq data, and neural network activations that no prior method could show. Its successor improved speed and global structure, but t-SNE established the paradigm: optimize a probabilistic neighborhood objective to produce human-readable maps.

Imagine a concert hall with thousands of people. Everyone knows who their friends are, but the hall has too many dimensions to photograph. PCA tries to compress the crowd by projecting shadows on a wall — friends who stood in different "depths" get flattened together.

t-SNE works differently: it hands everyone a spring connected to each friend and a repelling magnet against strangers, then lets them shuffle onto a flat dance floor. The springs pull friends together, the magnets push strangers apart, and after enough shuffling the floor layout mirrors the friendship network.

The ceiling: linear projections flatten structure

PCA finds the axes of greatest variance and projects data onto them. This is optimal when data lies on a flat plane. But real-world datasets often live on curved, folded surfaces — manifolds — embedded in high-dimensional space. Think of a : the data is really two-dimensional (position along the roll and height), but PCA sees a flat pancake and smashes points together that are far apart on the surface.

Earlier nonlinear methods like and Isomap improved on PCA, but they still struggled to preserve both local and global structure simultaneously. What was needed was a method that makes the neighborhoods in low dimensions match those in high dimensions — even if global distances have to bend.

Open in Lab
Left: PCA flattens the Swiss roll, merging distant surface points. Right: t-SNE unfolds the roll and preserves neighborhoods.
The demo wakes as you arrive…

Core idea: measure neighborhood with probabilities

t-SNE starts with a simple question: for each point xix_i in high-dimensional space, how likely is it that xix_i would pick xjx_j as its neighbor? The is defined as a Gaussian centered on xix_i — nearby points get high probability, distant points get near zero. Each point has its own Gaussian bandwidth σi\sigma_i, set automatically by the parameter (more on that shortly).

Then t-SNE asks the same question in the 2D map: for each low-dimensional point yiy_i, how likely is it that yiy_i picks yjy_j as its neighbor? Here the kernel is a Student-t distribution with one degree of freedom (a Cauchy distribution) — a bell curve with much heavier tails than a Gaussian.

The algorithm's goal: adjust the 2D positions until the low-dimensional probabilities match the high-dimensional ones. The mismatch is measured by KL divergence, and positions are optimized by descent.

Open in Lab
Move the center point and watch how the Gaussian kernel assigns neighborhood probabilities to surrounding points. Closer = higher probability.
The demo wakes as you arrive…

The math: from distances to probabilities

The first step converts every pairwise in the original space into a . Think of it as asking: "If point xix_i chooses a neighbor in proportion to a Gaussian centered on itself, what is the chance it picks xjx_j?"

pj∣i=exp⁡ ⁣(−∥xi−xj∥2/2σi2)∑k≠iexp⁡ ⁣(−∥xi−xk∥2/2σi2)p_{j|i} = \frac{\exp\!\bigl(-\|x_i - x_j\|^2 / 2\sigma_i^2\bigr)} {\sum_{k \neq i} \exp\!\bigl(-\|x_i - x_k\|^2 / 2\sigma_i^2\bigr)}
High-dimensional conditional probability — Each point assigns a Gaussian-weighted probability to every other point. The bandwidth σᵢ adapts per point so dense regions have tight Gaussians and sparse regions have wide ones.

To make the probabilities symmetric (pij=pjip_{ij} = p_{ji}), t-SNE averages the two conditional probabilities: pij=(pj∣i+pi∣j)/2np_{ij} = (p_{j|i} + p_{i|j}) / 2n. This guarantees every point makes a significant contribution, even outliers.

In the 2D map, the between the low-dimensional counterparts yiy_i and yjy_j uses a Student-t distribution with one degree of freedom instead of a Gaussian. The purpose of the heavy tail is critical — it gives moderately distant points room in the map, solving the crowding problem.

qij=(1+∥yi−yj∥2)−1∑k≠l(1+∥yk−yl∥2)−1q_{ij} = \frac{(1 + \|y_i - y_j\|^2)^{-1}} {\sum_{k \neq l} (1 + \|y_k - y_l\|^2)^{-1}}
Low-dimensional similarity (Student-t kernel) — The Student-t kernel decays much more slowly than a Gaussian, so pairs that are moderately far apart in high-D can be placed comfortably far in 2D without much penalty.
Open in Lab
Compare the Gaussian and Student-t kernels. Notice how the Student-t (orange) remains higher at large distances, giving far-apart points more room.
The demo wakes as you arrive…

The cost: KL divergence

t-SNE wants the neighborhood structure in 2D to mirror the one in high-D. It measures the mismatch using KL divergence — a one-way measure of how different two probability distributions are. The goal is simple: make Q (the 2D neighborhoods) as close to P (the high-D neighborhoods) as possible.

C=KL(P∥Q)=∑i≠jpijlog⁡pijqijC = KL(P \| Q) = \sum_{i \neq j} p_{ij} \log \frac{p_{ij}}{q_{ij}}
KL divergence cost function — This is asymmetric: it penalizes placing nearby high-D points far apart in 2D (large p, small q) much more than placing far-apart points close together. This means t-SNE prioritizes preserving local structure — exactly what makes clusters visible.

Perplexity: how many neighbors to care about

Each point's Gaussian bandwidth σi\sigma_i is chosen so that its probability distribution has a fixed perplexity — roughly the effective number of neighbors. Low perplexity (5–10) means each point only cares about its nearest few friends, producing tight local clusters. High perplexity (30–50) considers more neighbors, producing smoother, more globally structured maps.

The typical default is 30. But perplexity is not just a dial to set and forget — it controls the scale of structure you see. Running t-SNE at multiple perplexities is a good practice to separate real structure from artifact.

Open in Lab
Drag the perplexity slider and watch how the 2D map changes. Low perplexity shows tiny local clusters; high perplexity reveals broader groupings.
The demo wakes as you arrive…

The crowding problem: why Gaussians fail in 2D

Consider 11 equidistant points on a 10-dimensional sphere. In 10D there is plenty of room for all of them to be far from a central point. But in 2D there is only a thin ring around the center — far fewer "moderately distant" positions are available. If you use a Gaussian kernel in 2D (as the earlier SNE algorithm did), those moderately distant points get crushed into the center. Everything becomes a single undifferentiated blob.

The Student-t distribution solves this by making the "acceptable distance" in 2D much wider. A point that is moderately similar in high-D can sit comfortably far from its partner in 2D without paying a large KL penalty, because qijq_{ij} stays reasonably high even at that distance. This is the key insight of t-SNE over its predecessor SNE.

Open in Lab
In high dimensions, many neighbors fit comfortably around a point. In 2D with a Gaussian kernel, they crowd together. Switch to Student-t to see them spread.
The demo wakes as you arrive…

Optimization: how points move

The gradient of the KL cost with respect to each map point yiy_i has an elegant physical interpretation. Each pair of points exerts a "force" on each other. If pij>qijp_{ij} > q_{ij} (the points should be closer but aren't), there is an attractive spring pulling them together. If pij<qijp_{ij} < q_{ij} (the points are too close), there is a repulsive force pushing them apart. The (1+∥yi−yj∥2)−1(1 + \|y_i - y_j\|^2)^{-1} factor means nearby points exert stronger forces — distant points are almost ignored.

∂C∂yi=4∑j(pij−qij)(yi−yj)(1+∥yi−yj∥2)−1\frac{\partial C}{\partial y_i} = 4 \sum_{j} (p_{ij} - q_{ij})(y_i - y_j) (1 + \|y_i - y_j\|^2)^{-1}
t-SNE gradient — Attractive force when p > q, repulsive when p < q. The Student-t factor ensures the repulsion is strong between nearby points but vanishes for distant ones — preventing distant clusters from interfering with each other.
Open in Lab
Watch attraction and repulsion forces in real-time as t-SNE iterates. Green arrows show attraction, red arrows show repulsion.
The demo wakes as you arrive…

Tricks of the trade: reading t-SNE maps correctly

t-SNE is a powerful tool, but its outputs can be misleading if you don't know what to trust. Here are the rules experienced practitioners follow:

  • Cluster membership is meaningful — points in the same cluster are genuinely nearby in the original space.
  • Distances between clusters are NOT meaningful — two clusters may look far apart but be close in the original space, or vice versa.
  • Cluster sizes are NOT meaningful — t-SNE can inflate or shrink clusters depending on the local density of the data.
  • Run it multiple times — t-SNE has a random initialization. Different runs can produce different layouts. True structure appears consistently across runs.
  • Try multiple perplexities — structure that only appears at one perplexity may be an artifact.

Step by step: the full t-SNE algorithm

Here is the complete algorithm laid out as a pipeline. Notice that the high-dimensional probabilities PP are computed once upfront, while the low-dimensional probabilities QQ are recomputed every iteration because the map points yiy_i are moving.

Open in Lab
Walk through the t-SNE pipeline step by step: compute P, initialize map, iterate gradient descent, refine.
The demo wakes as you arrive…

t-SNE vs UMAP: the successor

UMAP (2018) builds on t-SNE's insight — optimize a neighborhood-based cost function — but uses a different mathematical framework (fuzzy simplicial sets from topological data analysis). Key differences:

  • Speed: UMAP is much faster, especially on large datasets, because it uses approximate nearest neighbors and .
  • Global structure: UMAP tends to better preserve global relationships — the relative positions of clusters are more meaningful.
  • Embeddings: UMAP can embed new, unseen data points using a learned transformation. t-SNE must re-run the entire optimization.
  • Determinism: UMAP produces more consistent runs with different random seeds.

Both are valuable. t-SNE remains preferred when local cluster quality is paramount and dataset size is manageable. UMAP wins on speed, scale, and when global structure matters.

Impact: what t-SNE revealed

  1. 2008

    t-SNE published

    van der Maaten & Hinton introduce t-SNE in JMLR. First clear visualizations of MNIST digit clusters, Olivetti face neighborhoods, and COIL-20 object rotations.

  2. 2013

    Word embedding visualizations

    Researchers use t-SNE to visualize Word2Vec and GloVe embeddings, revealing semantic clusters and analogies in language models for the first time.

  3. 2014

    Barnes-Hut approximation

    van der Maaten publishes an O(n log n) approximation that makes t-SNE practical for datasets with millions of points.

  4. 2016

    Single-cell RNA-seq revolution

    t-SNE becomes the standard visualization in single-cell biology, enabling researchers to identify cell types from gene expression profiles.

  5. 2018

    UMAP emerges

    McInnes, Healy, & Melville introduce UMAP — faster, better global structure, and capable of out-of-sample embedding. Builds directly on t-SNE's paradigm.

t-SNE's legacy is not just a visualization algorithm — it established the idea that neighborhood-based probabilistic optimization can reveal structure invisible to linear methods. Every time you see a colorful scatter plot of embeddings, cell types, or model activations, you are likely looking at t-SNE's intellectual offspring.

Citationvan der Maaten, Hinton. Visualizing Data Using t-SNE. JMLR, 2008.

Terms in this paper