dict_learning
- sklearn.decomposition.dict_learning(X, n_components, *, alpha, max_iter=100, tol=1e-08, method='lars', n_jobs=None, dict_init=None, , , , , , , ,
Solve 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 ||_Fro^2 + alpha * || U ||_1,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. ||.||_Fro stands for the Frobenius norm and ||.||_1,1 stands for the entry-wise matrix norm which is the sum of the absolute values of all the entries in the matrix.
Read more in the User Guide.
Data matrix.
Number of dictionary atoms to extract.
Sparsity controlling parameter.
Maximum number of iterations to perform.
Tolerance for the stopping condition.
The method used:
>>> import numpy as np
>>> from sklearn.datasets import make_sparse_coded_signal
>>> from sklearn.decomposition import dict_learning
>>> X, _, _ = make_sparse_coded_signal(
... n_samples=30, n_components=
We can check the level of sparsity of U:
>>> np.mean(U == 0)
np.float64(0.62)
We can compare the average squared euclidean norm of the reconstruction error of the sparse coded signal relative to the squared euclidean norm of the original signal:
>>> X_hat = U @ V
>>> np.mean(np.sum((X_hat - X) ** 2, axis=1) / np.sum(X ** 2, axis=1
'lars': uses the least angle regression method to solve the lassoproblem (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.
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.
Initial value for the dictionary for warm restart scenarios. Only used
if code_init and dict_init are not None.
Initial value for the sparse code for warm restart scenarios. Only used
if code_init and dict_init are not None.
Callable that gets invoked every five iterations.
To control the verbosity of the procedure.
Used for randomly initializing the dictionary. Pass an int for reproducible results across multiple function calls. See Glossary.
Whether or not to return the number of iterations.
Whether to enforce positivity when finding the dictionary.
Whether to enforce positivity when finding the code.
Maximum number of iterations to perform.
The sparse code factor in the matrix factorization.
The dictionary factor in the matrix factorization.
Vector of errors at each iteration.
Number of iterations run. Returned only if return_n_iter is
set to True.
SparsePCASparse Principal Components Analysis.
Mini-batch Sparse Principal Components Analysis.