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)#

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). In identical mode, mojolearn returns the same bits on Apple, NVIDIA and AMD GPUs. "fast" is available on Apple GPUs only.

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)

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).

property components_#

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

fit(X, y=None)#

Draw the projection matrix for X’s shape.

Only X’s shape is used (and X must be finite); the values are not.

Parameters:
  • X (array-like or scipy.sparse matrix of shape (n_samples, n_features))

  • y (ignored)

Returns:

self

Raises:
  • ValueError – If X is empty or non-finite, n_components is not positive, or n_components="auto" gives a dimension larger than n_features.

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

fit_transform(X, y=None)#

Fit to X, then return transform(X).

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data.

  • y (ignored)

Returns:

ndarray of shape (n_samples, n_components), float32

get_params(deep=True)#

Return this estimator’s constructor parameters.

Parameters:

deep (bool, default True) – Accepted for scikit-learn compatibility; it has no effect.

Returns:

dict – Parameter name to current value.

inverse_transform(X)#

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

Parameters:

X (array-like of shape (n_samples, n_components))

Returns:

ndarray of shape (n_samples, n_features), float32

Raises:

ValueError – If the model was fitted without compute_inverse_components=True.

set_params(**params)#

Set constructor parameters by keyword.

Raises:

ValueError – If a keyword is not a parameter of this estimator.

Returns:

self

transform(X)#

Project X: X @ components_.T.

Parameters:

X (array-like or scipy.sparse matrix of shape (n_samples, n_features))

Returns:

ndarray of shape (n_samples, n_components), float32 – Always a dense array.

Raises:

ValueError – If X has the wrong number of features or is non-finite.