Skip to content
AI.info

Unsupervised learning

t-SNE: Local Neighborhood Visualization

Understand t-SNE probabilities, perplexity, optimization, interpretation limits, and a disciplined workflow for exploratory visualization.

By the end you can

A published atlas of 2,058,652 cells, redrawn

One of the largest t-SNE figures in single-cell biology was drawn again in November 2019, and it fell apart. The dataset was a mouse-embryo atlas of 2,058,652 cells from Cao and colleagues, published in Nature that year. Its authors had annotated every cluster and split them into ten developmental trajectories. Reproduced with standard settings, the map misbehaved in every direction at once. “Whereas it showed a lot of structure, it visibly suffered from all the problems mentioned above: some clusters were fragmented into parts (e.g. clusters 13 and 15), there was little separation between distinct cell types, and global structure was grossly misrepresented,” write Dmitry Kobak and Philipp Berens of the reproduced embedding, in Nature Communications.

The reconstruction was not a caricature built to be knocked down. The authors say so directly: “this is not a strawman: this embedding is qualitatively similar to the ones shown in the literature”. The same paper lists the company that figure keeps — 23,822 adult mouse cortex cells that Tasic and colleagues split into 133 clusters, and a 10x Genomics set of 1,306,127 cells. Islands were named, counted, coloured and published. Part of what was being named was the behaviour of the optimiser.

t-SNE is an exploratory neighborhood map, not a census of natural groups.

Key idea

Perplexity is not the number of clusters

Perplexity is not a count of anything the reader can see on the finished plot. The original t-SNE paper, from van der Maaten and Hinton in 2008, defines it as Perp(Pi) = 2^H(Pi): two raised to the Shannon entropy of the conditional distribution Pi, measured in bits. The algorithm then runs a binary search for the Gaussian bandwidth sigma_i that yields a Pi carrying the perplexity you asked for. The paper calls the quantity “a smooth measure of the effective number of neighbors” and reports that “typical values are between 5 and 50”.

That number reaches most users as a library default rather than as a decision. scikit-learn ships perplexity = 30.0 in sklearn.manifold.TSNE and repeats the range in the parameter text: “Consider selecting a value between 5 and 50. Different values can result in significantly different results.” Its User Guide describes k as “effectively the number of nearest neighbors t-SNE considers when generating the conditional probabilities”, and states which way the trade runs: “Larger perplexities lead to more nearest neighbors and less sensitive to small structure. Conversely a lower perplexity considers a smaller number of neighbors, and thus ignores more global information in favour of the local neighborhood.”

So perplexity changes the neighborhood scale used to construct high-dimensional affinities. It does not request a target number of islands. No single value is correct for every sample size or density pattern. Kobak and Berens note that implementations default to 30 or 50, and suggest taking roughly one percent of the sample size as a large perplexity instead. Run a plausible range, record initialization and optimization settings, and inspect whether important neighborhoods persist. A conclusion that exists at one setting only should be reported as fragile.

Treat perplexity as a sensitivity parameter, not a cluster dial.

Comparison

What the map can suggest and what it cannot establish

Interpretation improves when every visual claim is matched to the property t-SNE actually optimizes — and each of the unmatched claims below has been measured failing.

Cluster size is the first casualty. A Gaussian cluster ten times as dispersed as another comes out roughly the same size in the map. Martin Wattenberg and two colleagues showed it with controlled synthetic tests, published in Distill in 2016: “The bottom line, however, is that you cannot see relative sizes of clusters in a t-SNE plot.” Distance goes the same way. Three Gaussians of 50 points each, one pair placed five times further apart than the other, appear equidistant at low perplexity. “The basic message is that distances between well-separated clusters in a t-SNE plot may mean nothing,” they write. Real data does it too: Chari and Pachter measured 4- to 200-fold increases in the ratio of maximum to minimum distance when single-cell data are reduced to 2D, whether compared against the relevant PCA space or the ambient space.

The split between the local column and the global column has been scored on data whose ground truth is known. Kobak and Berens generated 15,500 points from fifteen 50-dimensional Gaussians and applied three metrics. KNN is the fraction of 10 nearest neighbours preserved. KNC is the fraction of 4 nearest class means preserved. CPD is the Spearman correlation of pairwise distances over all 499,500 pairs among 1,000 sampled points. Their result: “t-SNE is much better than PCA in preserving the local structure (KNN 0.13 vs. 0.00) but much worse in preserving the global structure (KNC 0.23 vs. 1.00 and CPD 0.51 vs. 0.85)”. Even the column t-SNE wins reads 0.13.

The gaps between islands are an optimiser output too. Belkina and colleagues, working on cytometry datasets of at least 1 million events, traced fragmentation to hard-coded defaults: an early-exaggeration phase scaled to 25% of a fixed 1,000-iteration budget, and a learning rate hard-coded to 200. “When the EE is too short, the cell clusters continue to be resolved simultaneously with the local structure of each cluster being unfolded, leading to fragmented, overlapped or deformed islands in the resulting map,” they report in Nature Communications. Their opt-SNE method ends the exaggeration phase at the local maximum of the per-iteration rate of Kullback–Leibler-divergence change, instead of at a fixed fraction of a fixed budget. The empty space was a setting.

FigureComparison · 3 columns

Reasonable local claim

Two observations often appear near the same neighbors.

  • Check across several perplexities
  • Compare with original-space neighbors
  • Inspect representative observations
  • Treat disagreement as diagnostic

Weak global claim

Two islands are twice as far apart as two others.

  • Inter-island spacing is not globally metric
  • Orientation has no stable meaning
  • Map scale can change between runs
  • Disconnected appearance can arise from optimization

Unsupported density claim

A larger island represents a more variable or larger population.

  • Local densities are intentionally transformed
  • Area depends on optimization and parameters
  • Point overlap affects perception
  • Population size should be counted in original data

Analogy

Seating guests by who knows whom

Guests arranged on a floor so that people with many shared acquaintances stand nearby will form visible groups, and the arrangement can reveal social neighborhoods even though the distance between two distant groups has no reliable scale.

t-SNE goes further than any seating plan. It rescales density, and a second run of its stochastic optimizer produces a different floor. Social proximity is not geographic distance. Neither is the empty space on the map, which the Belkina result shows can be opened or closed by an iteration budget nobody chose deliberately.

Neighborhood membership is more defensible than island geography.

Visual

From high-dimensional affinities to a two-dimensional map

The method converts local relationships into probabilities, then optimizes a different probability model in the embedding. What that objective buys, and what it spends to buy it, is measurable rather than rhetorical. On the Kobak and Berens benchmark of 15,500 points drawn from fifteen 50-dimensional Gaussians, t-SNE preserved 13% of each point's 10 nearest neighbours where PCA preserved none. On the ordering of the 4 nearest class means it scored 0.23 against PCA's 1.00. The pipeline below produces that trade. It does not escape it.

FigureProcess · 5 steps
  1. 1. Compute local affinities

    Nearby observations receive higher conditional probability in the original space.

  2. 2. Set neighborhood scale

    Perplexity controls an effective range of neighbors for those affinities.

  3. 3. Define low-dimensional affinities

    A heavy-tailed distribution allows moderate separation without severe crowding.

  4. 4. Minimize divergence

    Optimization moves coordinates to align high- and low-dimensional neighborhood probabilities.

  5. 5. Inspect, do not certify

    The final map supports hypotheses that require stability and external validation.

The optimized object is a probability relationship, not the original coordinate geometry.

Example

Disciplined uses of t-SNE

t-SNE is strongest when paired with direct inspection rather than used as the final analytic answer.

  • Embedding audit: Color the map by source batch, missingness rate, or acquisition device to reveal nuisance structure in learned features.
  • Label review: Inspect points whose neighbors carry conflicting labels, then return to the raw records before editing annotations.
  • Retrieval debugging: Compare a query with nearby items in both t-SNE and the production embedding index.
  • Model comparison: Run the same protocol on representations from two models and quantify neighbor retention — the KNN, KNC and CPD scores of Kobak and Berens are three published ways of doing so — instead of judging screenshots alone.
  • Communication: Use the plot to select examples for discussion, while reporting original-space statistics separately.

Steps

Produce a t-SNE figure that can survive review

This protocol makes the exploratory nature of the map explicit and reproducible. Step 3 is the one that gets skipped, and it has a countable demonstration behind it. The same Distill study ran one identical configuration five times on a trefoil-knot dataset at perplexity 2. “The algorithm settles twice on a circle, which at least preserves the intrinsic topology. But in three of the runs it ends up with three different solutions which introduce artificial breaks,” the authors report. Raise the setting and the instability goes away: “Five runs at perplexity 50, however, give results that (up to symmetry) are visually identical.” One run tells you about one run. scikit-learn documents the cause as a property of the objective rather than an accident of the code: “t-SNE has a cost function that is not convex, i.e. with different initializations we can get different results.”

Sample size changes which knobs matter. Kobak and Berens report that “for n≫100,000, t-SNE with default optimisation parameters tends to produce poorly converged solutions and embeddings with continuous clusters fragmented into several parts”, citing Belkina and colleagues. At a million events the settings worth recording therefore include the iteration budget and the early-exaggeration schedule, not the perplexity alone.

FigureProcess · 5 steps
  1. 1. Prepare the space

    Scale appropriate features and often reduce noisy dimensions with PCA before t-SNE.

  2. 2. Sweep neighborhood scales

    Use several perplexities compatible with the sample size and expected local structure.

  3. 3. Repeat optimization

    Vary initialization or seed and retain convergence diagnostics rather than one attractive run.

  4. 4. Validate neighbors

    Measure overlap with original-space neighbors and inspect raw examples around disputed regions.

  5. 5. Report limits

    State parameters, preprocessing, instability, and which visual claims are intentionally avoided.

A t-SNE map is an instrument panel, not a taxonomy

A t-SNE map can reveal duplicated records, batch effects, label conflicts, retrieval neighborhoods, and representation changes. Those uses depend mainly on local relationships and direct example inspection. The moment islands become named categories without independent evidence, exploration has been mistaken for discovery.

How much even the local neighbourhood survives the projection has been measured. Chari and Pachter compared 2D embeddings against the spaces they were computed from, in PLOS Computational Biology in 2023. The average Jaccard distance to the ambient-space 30-nearest-neighbour set stayed consistently above 0.7, and above 0.8 against the PCA space. Cell-type neighbour-ranking correlations reached at most 0.4, and ran at least 33% below the higher-dimensional PCA spaces. “we show that extreme dimension reduction, from hundreds or thousands of dimensions to 2, inevitably induces significant distortion of high-dimensional datasets,” they write. For scale they invoke the Johnson–Lindenstrauss bound: holding pairwise distances to within 20% for 10,000 cells would require at least 1,842 dimensions, not two.

The global limit gets the same directness from Kobak and Berens. “Typical t-SNE plots do not capture such global structure, yielding a suboptimal and potentially misleading visualisation.” Their remedy is initialisation rather than interpretation. A PCA start injects the coarse arrangement that the optimiser then preserves, and PCA initialisation “makes the t-SNE outcome reproducible and not dependent on a random seed”.

The sharpest evidence that this is a setting and not a property of the algorithm is a published dispute. Becht and colleagues had credited UMAP with a global-structure advantage over t-SNE in Nature Biotechnology in 2019. Kobak and Linderman re-examined that claim in the same journal in 2021: “Here we show that this alleged superiority of UMAP can be entirely attributed to different choices of initialization in the implementations used by Becht et al.” The t-SNE implementations had started from random initialization. UMAP had started from Laplacian eigenmaps. “We also contend that these algorithms should always use informative initialization by default.” scikit-learn changed the TSNE default init from 'random' to 'pca' in version 1.2, December 2022, noting that PCA initialization is “usually more globally stable than random initialization”. A comparison between two algorithms had been a comparison between two starting points. Keep the map connected to the original observations, and to a declared question.

Use t-SNE to generate inspectable hypotheses, not ontologies.

Key takeaways