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)[source]#

Bases: _SparseCoding

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). "identical" gives the same bits on NVIDIA and AMD GPUs; "fast" applies on Apple GPUs.

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))
__init__(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)[source]#

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)[source]#

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

Returns:

self

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

Return transform(X).

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

  • y (ignored)

Returns:

ndarray of shape (n_samples, n_components), float32