mojolearn.SparseCoder#

class mojolearn.SparseCoder(dictionary, *, transform_algorithm='omp', transform_n_nonzero_coefs=None, transform_alpha=None, split_sign=False, n_jobs=None, positive_code=False, transform_max_iter=1000, numeric_mode=None)#

Sparse coding against a fixed, precomputed dictionary.

Follows scikit-learn’s SparseCoder; transform encodes each row with sparse_encode(). Nothing is learned, so fit does nothing.

Parameters:
  • dictionary (array-like of shape (n_components, n_features)) – Dictionary atoms, one per row.

  • transform_algorithm ({“omp”, “lasso_lars”, “lasso_cd”, “lars”, “threshold”}, default “omp”) – Coding method. An unknown value raises ValueError at construction.

  • transform_n_nonzero_coefs (int or None, default None) – Nonzero coefficients for "omp" and "lars".

  • transform_alpha (float or None, default None) – Penalty or threshold; None uses 1.0.

  • split_sign (bool, default False) – Return the positive and negative parts of the code as separate columns.

  • n_jobs (ignored)

  • positive_code (bool, default False) – Force the code to be non-negative.

  • transform_max_iter (int, default 1000) – Maximum iterations of the Lasso methods.

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

  • n_components_ (int)

  • n_features_in_ (int)

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> coder = mojolearn.SparseCoder(rng.random((5, 8), dtype=np.float32), transform_n_nonzero_coefs=2)
>>> code = coder.transform(rng.random((100, 8), dtype=np.float32))

Methods

fit(X[, y])

Do nothing and return self (the dictionary is fixed).

fit_transform(X[, y])

Return transform(X).

get_params([deep])

Return this estimator's constructor parameters.

inverse_transform(X)

Rebuild data from a code: code @ components_.

set_params(**params)

Set constructor parameters by keyword.

transform(X)

Encode X as a sparse combination of the dictionary atoms.

fit(X, y=None)#

Do nothing and return self (the dictionary is fixed).

Returns:

self

fit_transform(X, y=None)#

Return transform(X).

Parameters:
  • X (array-like of shape (n_samples, n_features))

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

Rebuild data from a code: code @ components_.

Parameters:

X (array-like of shape (n_samples, n_components)) – A code, as returned by transform (with 2 * n_components columns when split_sign=True).

Returns:

ndarray of shape (n_samples, n_features), float32

set_params(**params)#

Set constructor parameters by keyword.

Raises:

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

Returns:

self

transform(X)#

Encode X as a sparse combination of the dictionary atoms.

Uses transform_algorithm and its settings.

Parameters:

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

Returns:

ndarray of shape (n_samples, n_components), float32 – Twice as many columns when split_sign=True.