Categories &

Functions List

Function Reference: tsne

statistics: Y = tsne (X)
statistics: [Y, loss] = tsne (X)
statistics: […] = tsne (…, Name, Value)

t-distributed stochastic neighbor embedding (t-SNE).

Y = tsne (X) embeds the N × P data matrix X (rows are observations) into a low-dimensional space and returns the N × NumDimensions matrix Y of embedded points, whose pairwise (Student-t) affinities approximate the Gaussian affinities of the rows of X.

[Y, loss] = tsne (…) also returns the Kullback-Leibler divergence loss between the two affinity distributions at the returned embedding.

Name/Value pairs:

'Algorithm'
'exact' (default) forms the affinities and the gradient over every pair of points, which costs O(N^2) in time and memory at each iteration. 'barneshut' approximates both: the high-dimensional affinities are kept only over each point’s 3 × Perplexity nearest neighbours, and the repulsive part of the gradient is summed over a space-partitioning tree of the embedding, giving O(N log N). Use it when N is large enough that the exact algorithm is slow or cannot allocate; on this machine the two cost the same at about N = 500 and 'barneshut' is eight times faster at N = 2000.

The two do not return the same embedding, and their loss values are not comparable either: the divergence is summed over the pairs that carry an affinity, and 'barneshut' keeps far fewer of them.

'Theta'
The tree opening criterion for 'barneshut', a non-negative scalar (default 0.5). A cell of the tree is collapsed to its centre of mass when its width is smaller than Theta times its distance from the point being pushed, so a larger value is faster and coarser. 0 collapses nothing and makes the repulsion exact, at O(N^2); note that this still leaves the affinities sparse, so it does not reproduce 'exact'. Ignored by 'exact'.
'Distance'
The distance metric used for the high-dimensional affinities, as accepted by pdist (default 'euclidean').
'NumDimensions'
The dimension of the embedding Y (default min (P, 2)).
'NumPCAComponents'
If positive, reduce X to this many principal components before embedding (default 0, no reduction).
'Standardize'
Logical; center and scale each column of X before embedding (default false).
'Perplexity'
The effective number of local neighbors (default 30). It must be smaller than N.
'Exaggeration'
Tightness factor applied to the high-dimensional affinities for the first 100 iterations (default 4, no less than 1).
'LearnRate'
The learning rate of the optimization (default 500).
'InitialY'
An N × NumDimensions initial embedding (default 1e-4 * randn).
'Options'
A structure (as returned by statset) whose MaxIter (default 1000) and TolFun (default 1e-10) fields control the optimization.

The embedding is not unique: it depends on the initial configuration and the random state. Set 'InitialY' (or the random seed) for a reproducible result.

See also: pca, pdist, statset

Source Code: tsne

Embed a small five-dimensional data set into two dimensions.

 X = [randn(20, 5); randn(20, 5) + 5];
 Y = tsne (X, "Perplexity", 10);
 plot (Y(1:20,1), Y(1:20,2), "bo", Y(21:end,1), Y(21:end,2), "rx");
 title ("t-SNE embedding");
plotted figure