mojolearn.sparse_encode#
- mojolearn.sparse_encode(X, dictionary, *, algorithm='lasso_lars', n_nonzero_coefs=None, alpha=None, init=None, max_iter=1000, positive=False, numeric_mode=None, **_ignored)[source]#
Sparse-code each row of X against a fixed dictionary.
Follows scikit-learn’s
sparse_encode."lasso_lars"and"lasso_cd"solve the same Lasso problem (both by coordinate descent; the optimum does not depend on the path). Other scikit-learn keywords (such asn_jobsorcheck_input) are accepted and ignored.- Parameters:
X (array-like of shape (n_samples, n_features))
dictionary (array-like of shape (n_components, n_features)) – Dictionary atoms, one per row.
algorithm ({“lasso_lars”, “lasso_cd”, “lars”, “omp”, “threshold”}, default “lasso_lars”) – Coding method.
n_nonzero_coefs (int or None, default None) – Nonzero coefficients per row for
"lars"and"omp". None usesmin(max(n_features / 10, 1), n_components).alpha (float or None, default None) – Penalty for the Lasso methods, or threshold for
"threshold". None uses 1.0.init (array-like of shape (n_samples, n_components) or None, default None) – Starting code for
"lasso_cd".max_iter (int, default 1000) – Maximum iterations for the Lasso methods.
positive (bool, default False) – Force the code to be non-negative (Lasso methods and
"threshold").numeric_mode ({“identical”, “fast”} or None, default None) – Numeric mode for this call. None uses the package default (
"identical"unless changed)."identical"gives the same bits on NVIDIA and AMD GPUs;"fast"applies on Apple GPUs.
- Returns:
ndarray of shape (n_samples, n_components), float32
- Raises:
ValueError – For an unknown
algorithm.
Examples
>>> import numpy as np, mojolearn >>> rng = np.random.default_rng(0) >>> D = rng.random((5, 8), dtype=np.float32) >>> code = mojolearn.sparse_encode(rng.random((100, 8), dtype=np.float32), D, algorithm="omp", n_nonzero_coefs=2)