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: NumericModeMixin

t-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 exactly max_iter iterations (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 by random_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 X and 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_components is not 2, perplexity is out of range, init is an unknown string, or an init array has the wrong shape.

  • TypeError – If max_iter or random_state is not an int.

fit_transform(X, y=None)[source]#

Compute the embedding of X and return it.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Input rows, converted to float32.

  • y (None) – Ignored.

Returns:

embedding (ndarray of shape (n_samples, 2), float32) – The embedding, also stored in embedding_.