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.
Core idea: measure neighborhood with probabilities
t-SNE starts with a simple question: for each point in high-dimensional space, how likely is it that would pick as its neighbor? The is defined as a Gaussian centered on — nearby points get high probability, distant points get near zero. Each point has its own Gaussian bandwidth , 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 , how likely is it that picks 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.
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 chooses a neighbor in proportion to a Gaussian centered on itself, what is the chance it picks ?"
To make the probabilities symmetric (), t-SNE averages the two conditional probabilities: . This guarantees every point makes a significant contribution, even outliers.
In the 2D map, the between the low-dimensional counterparts and 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.
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.
Perplexity: how many neighbors to care about
Each point's Gaussian bandwidth 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.
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 stays reasonably high even at that distance. This is the key insight of t-SNE over its predecessor SNE.
Optimization: how points move
The gradient of the KL cost with respect to each map point has an elegant physical interpretation. Each pair of points exerts a "force" on each other. If (the points should be closer but aren't), there is an attractive spring pulling them together. If (the points are too close), there is a repulsive force pushing them apart. The factor means nearby points exert stronger forces — distant points are almost ignored.
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 are computed once upfront, while the low-dimensional probabilities are recomputed every iteration because the map points are moving.
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
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.
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.
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.
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.
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
- t-SNEتضمين الجوار العشوائي الموزع
- Stochastic Neighbor Embeddingتضمين الجوار العشوائي
- Crowding Problemمشكلة الازدحام
- Perplexityمعيار الحيرة الاحتمالية
- Student-t Distributionتوزيع ستيودنت-t
- Barnes-Hut Approximationتقريب بارنز-هت