tsne — Embed high-dimensional observations using t-distributed stochastic neighbor embedding.
tsne(X) embeds the rows of a numeric observation matrix into a lower-dimensional numeric matrix. [Y,loss] = tsne(...) also returns the final Kullback-Leibler divergence.
Syntax
Y = tsne(X)
Y = tsne(X, Name, Value)
[Y, loss] = tsne(___)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
X | NumericArray | Yes | — | Observation matrix with observations in rows. |
options | Any | Variadic | — | Name-value options such as Algorithm, Distance, NumDimensions, NumPCAComponents, Perplexity, Standardize, InitialY, LearnRate, Options, Theta, and Verbose. |
Returns
| Name | Type | Description |
|---|---|---|
Y | NumericArray | Low-dimensional embedding matrix. |
loss | NumericArray | Kullback-Leibler divergence for the final embedding. |
Returned values from tsne depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:tsne:InvalidArgument | Inputs, dimensions, distances, initialization, or name-value options are malformed or unsupported. | tsne: invalid argument |
RunMat:tsne:Internal | RunMat cannot allocate or construct the t-SNE result. | tsne: internal error |
How tsne works
Xmust be a real numeric vector or 2-D matrix. Matrix rows are observations; a column vector has one predictor per row, while a row vector is one observation with multiple predictors.- Rows containing
NaNare omitted before fitting.Infvalues are rejected. NumDimensionsselects the output dimension and defaults to 2.InitialYcan match either all input rows or the complete rows that remain afterNaNomission.Algorithmaccepts"barneshut"and"exact". RunMat computes a bounded exact CPU embedding for both modes so scripts receive compatible outputs without silent shape changes.Distancesupports"euclidean","fasteuclidean","seuclidean","fastseuclidean","cityblock","chebychev","minkowski","mahalanobis","cosine","correlation","spearman","hamming", and"jaccard". The"minkowski"option uses the default exponent 2. Custom function-handle distances are rejected explicitly.Standardizecenters and scales each predictor column before distance computation.NumPCAComponentsapplies PCA before pairwise distance computation when it is positive and smaller than the predictor count.Perplexity,Exaggeration,LearnRate,Theta,Verbose,NumPrint, andCacheSizeare parsed and validated for MATLAB-style script compatibility.Theta,Verbose,NumPrint, andCacheSizedo not affect the exact CPU optimizer.Optionsaccepts a statset-style struct withMaxIter,TolFun, and emptyOutputFcn. NonemptyOutputFcnis rejected because RunMat does not invoke iteration callbacks from builtins yet.- RunMat rejects very large dense exact workloads before allocation to avoid unbounded pairwise, PCA, and Mahalanobis memory use.
Examples
Embed observations into two dimensions
X = [0 0; 0.2 0.1; 9.8 9.9; 10 10.1];
Y = tsne(X, 'Perplexity', 2)Expected output:
`Y` is a 4-by-2 embedding matrix.Request the final loss
[Y,loss] = tsne(X, 'Algorithm', 'exact', 'Options', struct('MaxIter', 250))Expected output:
`loss` is the final Kullback-Leibler divergence for the embedding.Omit incomplete rows
Y = tsne([1 2; NaN 3; 10 11])Expected output:
`Y` has two rows because the incomplete observation is omitted.Using tsne with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how tsne changes the result.
Run a small tsne example, explain the result, then change one input and compare the output.
FAQ
Does RunMat implement Barnes-Hut acceleration?⌄
No. Algorithm="barneshut" is accepted for compatibility, but RunMat currently uses its bounded exact CPU optimizer for both algorithm names.
Are stochastic results bit-for-bit identical to MATLAB?⌄
No. RunMat uses its own RNG stream behind rng, so output shape and semantics are compatible but random initial embeddings are not bit-for-bit identical.
Related Stats functions
Ml
bayesopt · classify · confusionmat · crossvalind · cvpartition · fitclinear · fitctree · fitlm · kmeans · knnsearch · lasso · lassoglm · linkage · lscov · mnrfit · optimizableVariable · pdist · pdist2 · perfcurve · predict · regress · ridge · squareform · test · training
Summary
binocdf · boxplot · cdf · cdfplot · chi2cdf · corr · corrcoef · corrcov · cov · cov2corr · dummyvar · ecdf · filloutliers · fitdist · geomean · grpstats · harmmean · icdf · isoutlier · kstest · kurtosis · lsline · mad · mode · nanmax · normalize · normcdf · norminv · normpdf · onehotdecode · onehotencode · pdf · prctile · quantile · refline · rmse · skewness · tabulate · tcdf · tiedrank · tinv · tpdf · ttest2 · wblinv
Random
binornd · bootstrp · datasample · dividerand · exprnd · gamrnd · lhsdesign · mvnrnd · normrnd · random · randsample · rng · trnd · unidrnd · unifrnd · wblrnd
Hist
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how tsne is executed, line by line, in Rust.
- View the source for tsne in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.