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

Bases: _SparseCoding

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 both fit_algorithm values 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" in transform.

  • transform_alpha (float or None, default None) – Penalty or threshold used by transform; None uses alpha.

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

Variables:
  • components_ (ndarray of shape (n_components, n_features)) – The dictionary atoms.

  • error_ (list of float) – Objective value at each iteration.

  • n_iter_ (int)

  • n_features_in_ (int)

Raises:

ValueError – For an unknown fit_algorithm or transform_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)
__init__(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)[source]#

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.

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

Fit the dictionary to X.

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

  • y (ignored)

Returns:

self

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

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.