t-SNE

Also known as: t-distributed stochastic neighbor embedding

On this page 7

t-SNE (t-distributed stochastic neighbor embedding) is a nonlinear dimensionality reduction method that places high-dimensional data points in a two- or three-dimensional map so that points which are close neighbors in the original space stay close in the map. In machine learning and the wider field of artificial intelligence, it is used mainly as a visualization tool for exploring embeddings, clusters and class structure, not as a general-purpose feature transformation.

The method was introduced by Laurens van der Maaten and Geoffrey Hinton in 2008 as an improvement on stochastic neighbor embedding. Its defining idea is to compare probability distributions over neighbors rather than raw distances, and to use a heavy-tailed Student t-distribution in the low-dimensional map to prevent points from collapsing into a dense center.

How t-SNE builds a low-dimensional map

t-SNE works by converting distances into neighbor probabilities in both spaces and then moving the map points until the two sets of probabilities match as closely as possible. The algorithm runs in three stages.

  1. For each point, a Gaussian kernel centered on that point turns Euclidean distances into conditional probabilities that another point is its neighbor. The kernel width is chosen per point so that its distribution reaches a target perplexity.
  2. The conditional probabilities are symmetrized into joint probabilities, which makes every pair contribute in both directions and stabilizes the result for outliers.
  3. In the map, similarities are computed with a Student t-distribution with one degree of freedom, and the positions are optimized by gradient descent on the Kullback-Leibler divergence between the high-dimensional and low-dimensional distributions.

The heavy tails of the t-distribution solve the crowding problem: a two-dimensional plane has far less room for moderately distant points than a space with hundreds of dimensions. Heavy tails let dissimilar points sit far apart in the map without a large penalty, so separate clusters appear clearly separated. Because the Kullback-Leibler divergence punishes placing true neighbors far apart much more than placing non-neighbors close, t-SNE is biased toward preserving local structure.

The components of the algorithm can be summarized as follows.

ComponentRolePractical effect
High-dimensional kernelGaussian similarities per pointAdapts to local density
Low-dimensional kernelStudent t with one degree of freedomReduces crowding of clusters
Cost functionKullback-Leibler divergenceFavors local over global fidelity
OptimizerGradient descent with momentumResult depends on initialization
ApproximationBarnes-Hut or FFT-based interpolationMakes large datasets feasible
Main components of the t-SNE algorithm

Key t-SNE parameters and their effects

The output of t-SNE depends strongly on a handful of hyperparameters, and perplexity is the most influential of them. Perplexity can be read as the effective number of neighbors each point considers; typical values range from 5 to 50, and larger datasets usually tolerate higher values.

ParameterTypical rangeWhat it controls
Perplexity5 to 50Balance between local and broader structure
Learning rateAbout n/12, often 200 to 1,000Step size; too low causes compressed clouds
Early exaggeration4 to 12 for the first 250 iterationsForms tight, well-separated clusters early
Iterations1,000 or moreConvergence of the layout
InitializationPCA or randomReproducibility and global arrangement
Output dimensions2 or 3Plotting; higher values are rarely useful
Common t-SNE parameters with typical values

Two settings deserve particular care. Early exaggeration multiplies the high-dimensional probabilities during the first phase so that clusters form before fine adjustment begins. PCA initialization generally preserves more of the global arrangement than random initialization and gives repeatable layouts, which is why recent implementations use it by default.

t-SNE compared with PCA and UMAP

t-SNE is one of several dimensionality reduction methods, and the right choice depends on whether the goal is visualization, compression or downstream modeling. Principal component analysis is linear and preserves global variance, while UMAP is a newer nonlinear method that is usually faster and keeps somewhat more global structure.

Propertyt-SNEPCAUMAP
Mapping typeNonlinearLinearNonlinear
Preserved structureMainly local neighborhoodsGlobal varianceLocal with partial global
Typical runtime on 100,000 pointsMinutes with approximationsSecondsFaster than t-SNE
Transforms new dataNot nativelyYesYes
DeterministicNo, unless seededYesNo, unless seeded
Main useVisual explorationCompression and preprocessingVisualization and feature reduction
Comparison of three dimensionality reduction methods

A common workflow combines the methods: PCA first reduces the data to about 50 dimensions to remove noise and speed up computation, and t-SNE then produces the two-dimensional map. Standard t-SNE has no parametric mapping, so it cannot place new points without rerunning the optimization; parametric variants that train a neural network exist but are less common.

Reading a t-SNE plot correctly

A t-SNE map is reliable mainly about which points are neighbors, and several visual features of the plot carry little or no meaning. Misreading these features is the most frequent source of wrong conclusions.

Visual featureMeaningfulReason
Membership of a tight groupUsually yesLocal neighborhoods are preserved
Cluster sizeNoDensity is equalized by the per-point kernels
Distance between clustersWeakly at bestGlobal distances are not optimized
Apparent clusters in noiseNoLow perplexity can create spurious groups
Axis valuesNoCoordinates have no units or direction
What a t-SNE map does and does not show

Responsible practice is to run several perplexity values and several random seeds and to trust only the patterns that persist across runs. Visual groupings should be confirmed with a clustering algorithm such as k-means or DBSCAN applied to the original or PCA-reduced features, not to the t-SNE coordinates themselves.

Limitations and computational cost

Exact t-SNE has quadratic time and memory cost in the number of points, which limits it to a few thousand samples. The Barnes-Hut approximation reduces the cost to about O(n log n), and FFT-accelerated interpolation, as in FIt-SNE and openTSNE, scales to millions of points on ordinary hardware.

  • Non-convex optimization: different seeds can give visibly different layouts.
  • No out-of-sample mapping in the standard formulation.
  • Unsuitable as model input: the coordinates distort distances and should not feed classifiers.
  • Sensitivity to preprocessing: feature scaling and the choice of distance metric change the result.
  • Limited global faithfulness: the relative placement of distant clusters is often arbitrary.

Explained without jargon

Imagine a huge spreadsheet where every row describes something, such as a photo or a customer, with hundreds of numbers. Nobody can picture hundreds of dimensions. t-SNE draws a flat map of those rows where similar items end up next to each other.

It works like seating guests at a large party. Each guest tells the host who their closest friends are, and the host arranges the tables so that friends sit together. The map is good at showing who belongs with whom, but the size of each table and the distance between tables do not mean much.

A typical use case

A data science team at an online retailer trains an image model on 60,000 product photos and wants to check whether the learned embeddings separate product categories. Each photo is represented by a 512-dimensional vector. The team first applies PCA to reduce the vectors to 50 dimensions and then runs an FFT-accelerated t-SNE with perplexity 30, PCA initialization and 1,000 iterations.

The resulting map shows distinct groups for shoes, bags and watches, but sneakers and running shoes overlap in a single mixed region. Inspecting that region reveals about 2,000 mislabeled training images. The team corrects the labels, retrains the model and confirms the improvement with standard accuracy metrics rather than with the picture alone. The outcome is a cleaner dataset and a measurable gain in classification accuracy.