Ultralytics YOLO27: Join the waitlist.
License YOLO
Back to Ultralytics Glossary

t-distributed Stochastic Neighbor Embedding (t-SNE)

t-SNE maps high-dimensional data to 2D or 3D so that neighbors stay neighbors, revealing clusters. How it works, perplexity, pitfalls, and code.

t-distributed stochastic neighbor embedding (t-SNE) is a nonlinear dimensionality reduction method that places high-dimensional data points on a two- or three-dimensional map so that points that are neighbors in the original space stay neighbors on the map. Laurens van der Maaten and Geoffrey Hinton introduced it in a 2008 paper in the Journal of Machine Learning Research. It is a standard tool for data visualization of embeddings, image features, and single-cell gene expression data, where hundreds of dimensions hide groups that a 2D scatter plot makes visible.

How t-SNE Works#

t-SNE turns distances into probabilities in both spaces and then moves the map points until the two sets of probabilities match:

  • High-dimensional similarities: For each point, t-SNE places a Gaussian over the distances to all other points and converts them into probabilities of picking each one as a neighbor. The width of each Gaussian is tuned so that the distribution's perplexity, roughly the effective number of neighbors, matches a value the user sets, which gives dense regions narrow Gaussians and sparse regions wide ones.
  • Low-dimensional similarities: On the map, similarities come from a Student's t-distribution with one degree of freedom, whose heavy tails let moderately distant points sit far apart. This relieves the crowding problem: a 2D map has too little room to hold all the moderately close neighbors that a high-dimensional point can have.
  • Optimization: Gradient descent moves the map points to minimize the Kullback-Leibler divergence between the two distributions. An early exaggeration phase multiplies the high-dimensional similarities, by 12 in scikit-learn, so that natural clusters form tight, well-separated groups early on.

Exact t-SNE compares every pair of points, so its cost grows quadratically with the dataset. The Barnes-Hut approximation, the default in scikit-learn, cuts this to O(N log N) for 2D and 3D maps, and FFT-accelerated implementations such as openTSNE handle millions of points.

Perplexity and Reading a t-SNE Plot#

Perplexity is the main setting. Values between 5 and 50 are typical, scikit-learn's default is 30, and it must be smaller than the number of points. Low perplexity focuses on very local structure and can split one group into fragments, while high perplexity blends neighboring groups and shows more of the global layout. Because the optimization is non-convex, different random seeds can produce different maps.

Three properties make t-SNE plots easy to misread. Cluster sizes on the map say little about the spread of the original clusters, because t-SNE expands dense groups and contracts sparse ones. Distances between clusters are not reliable either, and at low perplexity even random noise can appear to form clusters. Scikit-learn's TSNE also has no transform method, so a fitted map cannot place new points without being recomputed, and t-SNE coordinates are rarely used as input features for other models.

t-SNE vs. PCA and UMAP#

Principal component analysis (PCA) is linear and deterministic, preserves the directions of greatest global variance, and projects new points instantly. t-SNE is nonlinear and stochastic and preserves local neighborhoods. The two are often combined: reducing very high-dimensional data to about 50 dimensions with PCA before running t-SNE suppresses noise and speeds up the computation, and scikit-learn's t-SNE initializes the map from PCA by default, which keeps more of the global arrangement. UMAP, introduced in 2018, also builds a neighbor graph, typically runs faster on large datasets, often keeps more global structure, and can embed new points into an existing map.

This example projects the 64-pixel handwritten digits bundled with scikit-learn to 2D with both methods and measures trustworthiness, the extent to which each point's nearest neighbors on the map were also its neighbors in 64 dimensions:

from sklearn.datasets import load_digits
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE, trustworthiness

# 1,797 handwritten digits, each an 8x8 image flattened to 64 features
X, y = load_digits(return_X_y=True)

# Project to 2D with a linear method and with t-SNE
X_pca = PCA(n_components=2).fit_transform(X)
X_tsne = TSNE(n_components=2, perplexity=30, random_state=0).fit_transform(X)

# Trustworthiness near 1.0 means a point's 2D neighbors were also its 64D neighbors
print(f"PCA trustworthiness:   {trustworthiness(X, X_pca, n_neighbors=10):.3f}")
print(f"t-SNE trustworthiness: {trustworthiness(X, X_tsne, n_neighbors=10):.3f}")

The script prints 0.830 for PCA and 0.993 for t-SNE. Squeezing 64 dimensions onto two linear directions places many points beside digits that were far from them in 64 dimensions, while the t-SNE map keeps nearly all of the original 10-nearest-neighbor relationships.

Real-World Applications#

  • Inspecting learned features: Projecting a network's feature extraction outputs shows whether visually similar classes form separate groups. YOLO26 models return one feature vector per image through model.embed(), taken from the second-to-last layer by default, and a t-SNE map of those vectors shows how a custom-trained model separates its classes.
  • Dataset curation: Images that land inside another class's island are often mislabeled, and tight clumps of nearly identical points flag duplicates. The Clustering panel in Ultralytics Platform projects a dataset's image embeddings into a similar 2D scatter plot for these checks.
  • Word and document embeddings: Maps of word vectors place related words, such as names of countries or numbers, in neighboring groups.
  • Single-cell genomics: Researchers reduce the expression of thousands of genes per cell to 2D to identify cell types, an analysis in which t-SNE was long the standard before many groups moved to UMAP.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning