mojolearn.DictionaryLearning#
- class mojolearn.DictionaryLearning(n_components=None, *, alpha=1, max_iter=1000, tol=1e-08, fit_algorithm='lars', transform_algorithm='omp', transform_n_nonzero_coefs=None, transform_alpha=None, n_jobs=None, code_init=None, dict_init=None, callback=None, verbose=False, split_sign=False, random_state=None, positive_code=False, positive_dict=False, transform_max_iter=1000, numeric_mode=None)#
Learn a dictionary in which the data has a sparse representation.
Follows scikit-learn’s
DictionaryLearning: alternates sparse coding of X with updates of the dictionary atoms. The starting dictionary comes from an exact SVD of X, and bothfit_algorithmvalues solve the same Lasso problem by coordinate descent.- Parameters:
n_components (int or None, default None) – Number of atoms; None uses
n_features.alpha (float, default 1) – Sparsity penalty used while fitting.
max_iter (int, default 1000) – Maximum number of iterations.
tol (float, default 1e-8) – Convergence tolerance.
fit_algorithm ({“lars”, “cd”}, default “lars”) – Lasso solver name used while fitting (both give the same optimum).
transform_algorithm ({“omp”, “lasso_lars”, “lasso_cd”, “lars”, “threshold”}, default “omp”) – Method used by
transform; see sparse_encode().transform_n_nonzero_coefs (int or None, default None) – Nonzero coefficients for
"omp"and"lars"intransform.transform_alpha (float or None, default None) – Penalty or threshold used by
transform; None usesalpha.n_jobs (ignored)
code_init (array-like of shape (n_samples, n_components) or None, default None) – Starting code; used only together with
dict_init.dict_init (array-like of shape (n_components, n_features) or None, default None) – Starting dictionary; used only together with
code_init.callback (None) – Must be None; a callable raises NotImplementedError.
verbose (ignored)
split_sign (bool, default False) – Return the positive and negative parts of the code as separate columns.
random_state (int or None, default None) – Seed used to resample unused atoms; None means 0.
positive_code (bool, default False) – Force the code to be non-negative.
positive_dict (bool, default False) – Force the dictionary to be non-negative.
transform_max_iter (int, default 1000) – Maximum iterations of the Lasso methods during fit and transform.
numeric_mode ({“identical”, “fast”} or None, default None) – Numeric mode for this estimator. None uses the package default (
"identical"unless changed). Inidenticalmode, mojolearn returns the same bits on Apple, NVIDIA and AMD GPUs."fast"is available on Apple GPUs only.
- Variables:
- Raises:
ValueError – For an unknown
fit_algorithmortransform_algorithm.
Examples
>>> import numpy as np, mojolearn >>> X = np.random.default_rng(0).random((500, 8), dtype=np.float32) >>> dl = mojolearn.DictionaryLearning(n_components=5, max_iter=20, random_state=0) >>> code = dl.fit_transform(X)
Methods
fit(X[, y])Fit the dictionary to X.
fit_transform(X[, y])Fit the dictionary and return the code found while fitting.
get_params([deep])Return this estimator's constructor parameters.
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)#
Fit the dictionary to X.
- Parameters:
X (array-like of shape (n_samples, n_features))
y (ignored)
- Returns:
self
- fit_transform(X, y=None)#
Fit the dictionary and return the code found while fitting.
- Parameters:
X (array-like of shape (n_samples, n_features))
y (ignored)
- Returns:
ndarray of shape (n_samples, n_components), float32 – Twice as many columns when
split_sign=True.
- 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(with2 * n_componentscolumns whensplit_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_algorithmand 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.