mojolearn.SparseRandomProjection#

class mojolearn.SparseRandomProjection(n_components='auto', *, density='auto', eps=0.1, dense_output=False, compute_inverse_components=False, random_state=None, numeric_mode=None)[source]#

Bases: _RandomProjection

Reduce dimension with a sparse random matrix.

Follows scikit-learn’s SparseRandomProjection: each entry is +sqrt(1/density)/sqrt(n_components) or its negative with probability density/2 each, and 0 otherwise. The components are stored as a dense array, and transform always returns a dense array.

Parameters:
  • n_components (int or “auto”, default “auto”) – Target dimension. "auto" uses johnson_lindenstrauss_min_dim(n_samples, eps=eps).

  • density (float or “auto”, default “auto”) – Fraction of nonzero entries, in (0, 1]. "auto" is 1 / sqrt(n_features). Out-of-range values raise ValueError.

  • dense_output (bool, default False) – Accepted for scikit-learn compatibility; the output is always dense.

  • eps (float, default 0.1) – Distortion bound used by n_components="auto" through johnson_lindenstrauss_min_dim().

  • compute_inverse_components (bool, default False) – Compute inverse_components_ (the pseudo-inverse of the components) in fit, needed by inverse_transform. Raises ValueError if the components are rank deficient.

  • random_state (int or None, default None) – Seed of mojolearn’s counter-based random generator; None means 0. The same seed gives the same matrix on every device, but not the matrix scikit-learn draws for that seed.

  • numeric_mode ({“identical”, “fast”} or None, default None) – Numeric mode for this estimator. None uses the package default ("identical" unless changed). "identical" gives the same bits on NVIDIA and AMD GPUs; "fast" applies on Apple GPUs.

Variables:
  • components_ (ndarray of shape (n_components, n_features))

  • inverse_components_ (ndarray of shape (n_features, n_components)) – Only with compute_inverse_components=True.

  • density_ (float)

  • n_components_ (int)

  • n_features_in_ (int)

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((1000, 64), dtype=np.float32)
>>> model = mojolearn.SparseRandomProjection(n_components=8, random_state=0).fit(X)
>>> model.transform(X).shape
(1000, 8)
__init__(n_components='auto', *, density='auto', eps=0.1, dense_output=False, compute_inverse_components=False, random_state=None, numeric_mode=None)[source]#

Methods

fit(X[, y])

Draw the projection matrix for X's shape.

fit_transform(X[, y])

Fit to X, then return transform(X).

get_params([deep])

Return this estimator's constructor parameters.

inverse_transform(X)

Map projected data back with the pseudo-inverse of the components.

set_params(**params)

Set constructor parameters by keyword.

transform(X)

Project X: X @ components_.T.

Attributes

components_

The random projection matrix, shape (n_components, n_features).