sklearn.decomposition.dict_learning

sklearn.decomposition.dict_learning(X, n_components, *, alpha, max_iter=100, tol=1e-08, method='lars', n_jobs=None, dict_init=None, code_init=None, callback=None, verbose=False, random_state=None, return_n_iter=False, positive_dict=False, positive_code=False, method_max_iter=1000)[source]

Solves a dictionary learning matrix factorization problem.

Finds the best dictionary and the corresponding sparse code for approximating the data matrix X by solving:

(U^*, V^*) = argmin 0.5 || X - U V ||_2^2 + alpha * || U ||_1
             (U,V)
            with || V_k ||_2 = 1 for all  0 <= k < n_components

where V is the dictionary and U is the sparse code.

Read more in the User Guide.

Parameters
Xarray of shape (n_samples, n_features)

Data matrix.

n_componentsint,

Number of dictionary atoms to extract.

alphaint,

Sparsity controlling parameter.

max_iterint,

Maximum number of iterations to perform.

tolfloat,

Tolerance for the stopping condition.

method{‘lars’, ‘cd’}

lars: uses the least angle regression method to solve the lasso problem (linear_model.lars_path) cd: uses the coordinate descent method to compute the Lasso solution (linear_model.Lasso). Lars will be faster if the estimated components are sparse.

n_jobsint or None, optional (default=None)

Number of parallel jobs to run. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.

dict_initarray of shape (n_components, n_features),

Initial value for the dictionary for warm restart scenarios.

code_initarray of shape (n_samples, n_components),

Initial value for the sparse code for warm restart scenarios.

callbackcallable or None, optional (default: None)

Callable that gets invoked every five iterations

verbosebool, optional (default: False)

To control the verbosity of the procedure.

random_stateint, RandomState instance or None, optional (default=None)

Used for randomly initializing the dictionary. Pass an int for reproducible results across multiple function calls. See Glossary.

return_n_iterbool

Whether or not to return the number of iterations.

positive_dictbool

Whether to enforce positivity when finding the dictionary.

New in version 0.20.

positive_codebool

Whether to enforce positivity when finding the code.

New in version 0.20.

method_max_iterint, optional (default=1000)

Maximum number of iterations to perform.

New in version 0.22.

Returns
codearray of shape (n_samples, n_components)

The sparse code factor in the matrix factorization.

dictionaryarray of shape (n_components, n_features),

The dictionary factor in the matrix factorization.

errorsarray

Vector of errors at each iteration.

n_iterint

Number of iterations run. Returned only if return_n_iter is set to True.