mojolearn.TSNE#
- class mojolearn.TSNE(n_components=2, perplexity=30.0, early_exaggeration=12.0, learning_rate='auto', max_iter=1000, init='pca', random_state=0)[source]#
Bases:
NumericModeMixint-distributed Stochastic Neighbor Embedding (t-SNE) into two dimensions.
Follows scikit-learn’s
TSNE(and cuML’s): affinities from exact k-nearest neighbors with the perplexity search, then gradient descent with scikit-learn’s gains and momentum, using early exaggeration for the first 250 iterations. Unlike scikit-learn’s default Barnes-Hut method, the repulsive forces are computed exactly, and the optimizer always runs exactlymax_iteriterations (there is no early stop when progress stalls). The computation runs in a fixed order, so results are reproducible.- Parameters:
n_components (int, default 2) – Dimension of the embedding. Only 2 is supported.
perplexity (float, default 30.0) – Effective number of neighbors. Must be in
(0, n_samples).early_exaggeration (float, default 12.0) – Factor applied to the attractive forces during the early phase.
learning_rate (float or ‘auto’, default ‘auto’) – Optimizer step size. ‘auto’ is scikit-learn’s
max(n_samples / early_exaggeration / 4, 50).max_iter (int, default 1000) – Number of optimization iterations, all of which run.
init ({‘pca’, ‘random’} or array-like of shape (n_samples, 2), default ‘pca’) – Initial embedding. ‘pca’ is mojolearn’s PCA embedding scaled as scikit-learn scales it: divided by the first column’s standard deviation and multiplied by 1e-4. mojolearn uses an exact eigensolver where scikit-learn uses a randomized one. ‘random’ draws uniformly from
(-5e-5, 5e-5)seeded byrandom_state. An array is used as given.random_state (int, default 0) – Seed for the ‘random’ initialization.
numeric_mode (str or None, default None) – Numeric mode for this estimator; None uses the package default set with
mojolearn.set_numeric_mode.
- Variables:
embedding_ (ndarray of shape (n_samples, 2), float32) – The embedding.
kl_divergence_ (float) – Kullback-Leibler divergence after optimization.
n_iter_ (int) – Number of iterations run (always
max_iter).learning_rate_ (float) – The learning rate used, after resolving ‘auto’.
n_features_in_ (int) – Number of features seen during
fit.
Examples
>>> import numpy as np, mojolearn >>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32) >>> embedding = mojolearn.TSNE(perplexity=30.0).fit_transform(X) >>> embedding.shape (1000, 2)
- __init__(n_components=2, perplexity=30.0, early_exaggeration=12.0, learning_rate='auto', max_iter=1000, init='pca', random_state=0)[source]#
Methods
fit(X[, y])Compute the embedding of
X.fit_transform(X[, y])Compute the embedding of
Xand return it.get_params([deep])numeric_mode_used()The numeric mode this estimator runs in.
set_params(**params)Set constructor parameters.
vendor_used()The GPU backend this estimator runs on.
- fit(X, y=None)[source]#
Compute the embedding of
X.- Parameters:
X (array-like of shape (n_samples, n_features)) – Input rows, converted to float32.
y (None) – Ignored.
- Returns:
self (TSNE) – The fitted estimator, with the result in
embedding_.- Raises:
ValueError – If
n_componentsis not 2,perplexityis out of range,initis an unknown string, or aninitarray has the wrong shape.TypeError – If
max_iterorrandom_stateis not an int.