tsne
Builtin index · Statistics & ML
result = tsne(X, [n_components=2], [perplexity=], [seed=], [max_iter=300])
Input: X, n_components, perplexity, seed, max_iter · Output: result
What it is
t-SNE embedding of an N-row, D-column matrix X into n_components dimensions (an integer, default 2), controlled by perplexity (a number balancing local vs. global structure; unset uses the implementation's default), an optional integer seed=, and max_iter (an integer iteration budget, default 300).
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = tsne(X, [n_components=2], [perplexity=], [seed=], [max_iter=300]) | X matrix or numbert-SNE embedding of an N-row, D-column matrix X into n_components dimensions (an integer, default 2), controlled by perplexity (a number balancing local vs.n_components matrix or number, optional (default 2)t-SNE embedding of an N-row, D-column matrix X into n_components dimensions (an integer, default 2), controlled by perplexity (a number balancing local vs.perplexity matrix or number, optionalt-SNE embedding of an N-row, D-column matrix X into n_components dimensions (an integer, default 2), controlled by perplexity (a number balancing local vs.seed optionalunset uses the implementation's default), an optional integer seed=, and max_iter (an integer iteration budget, default 300).max_iter optional (default 300)unset uses the implementation's default), an optional integer seed=, and max_iter (an integer iteration budget, default 300). | result matrix | Returns the (N, n_components) embedded coordinate matrix — for looking at structure rather than measuring it. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
G = vstack(randn(40, 2, seed = 31) + 4,
randn(40, 2, seed = 32) - 4,
randn(40, 2, seed = 33))
d = dbscan(G, 1.5, 4)
print("dbscan labels: {distinct(d)}")
proj = pca(G, 2) # the (n, 2) projected coordinates
comps = pca_components(G, 2) # the (2, 2) loading matrix
ev = pca_explained_variance(G, 2)
print("pca projected shape: {rows(proj)} x {cols(proj)}")
print("pca components:\n{round(comps, 3)}")
print("explained variance: {round(ev, 3)}")
small = vstack(randn(6, 2, seed = 41) + 3, randn(6, 2, seed = 42) - 3)
low = tsne(small, n_components = 2, perplexity = 3, seed = 1, max_iter = 100)
print("tsne output rows: {rows(low)}")
dbscan labels: (0, 1, 2) pca projected shape: 120 x 2 pca components: [0.694, 0.72; 0.72, -0.694] explained variance: [0.969, 0.031] tsne output rows: 12
from statistics-ml
How it works
tsne(X, [n_components=2], [perplexity=], [seed=], [max_iter=], [method=], [angle=]) — t-SNE (van der Maaten & Hinton 2008) dimensionality reduction for visualization: calibrates a per-point Gaussian bandwidth in the input space by binary-searching for perplexity (the reference paper's own pseudocode, tsne_hbeta), then runs gradient descent (momentum + per-parameter adaptive gains + early exaggeration for the first 100 iterations — all the paper's own constants, not invented ones) on a low-dimensional embedding under a Student-t kernel. perplexity defaults to min(30, (n-1)/3) — a real, well-known constraint (perplexity must stay well under the point count or the per-point neighborhood the binary search is solving for doesn't exist). max_iter defaults to a conservative 300 (most reference implementations run ~1000): this reliably resolves the well-separated-cluster case it's verified against, but is NOT guaranteed to converge to a good embedding for a harder/pathological input — supply a larger max_iter= explicitly for those. method picks how the gradient itself gets computed — "exact" (the paper's own O(n^2)-per-iteration dense sum) or "barnes_hut" (van der Maaten 2014's O(n log n) tree approximation, same algorithm and default scikit-learn's own TSNE uses; only supports n_components=2 — see tsne_fit's own doc comment for both the algorithm and why that restriction exists). Defaults to "barnes_hut" when n_components=2 (matching scikit-learn's own default) and "exact" otherwise, since Barnes-Hut has nothing to offer outside 2 dimensions here. angle (default 0.5, only meaningful for method="barnes_hut") is that approximation's accuracy/speed knob — named to match scikit-learn's own parameter. See pairwise_sqdist_via_matmul's own doc comment for how method="exact"'s repeated per-iteration pairwise-distance step routes through qu_gpu when built with --features gpu ("barnes_hut" has no comparable matmul-shaped step to route through it).
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.