Solve a dictionary learning matrix factorization problem online.
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. This is accomplished by repeatedly iterating over mini-batches by slicing the input data.
Read more in the User Guide.
Data matrix.
Number of dictionary atoms to extract. If None, then n_components is set to n_features.
Sparsity controlling parameter.
Maximum number of iterations over the complete dataset before stopping independently of any early stopping criterion heuristics.
Added in version 1.1.
Whether to also return the code U or just the dictionary V.
Initial values for the dictionary for warm restart scenarios. If None, the initial values for the dictionary are created with an SVD decomposition of the data via randomized_svd.
A callable that gets invoked at the end of each iteration.
The number of samples to take in each batch.
Changed in version 1.3: The default value of batch_size changed from 3 to 256 in version 1.3.
To control the verbosity of the procedure.
Whether to shuffle the data before splitting it in batches.
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.
'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.Used for initializing the dictionary when dict_init is not specified, randomly shuffling the data when shuffle is set to True, and updating the dictionary. Pass an int for reproducible results across multiple function calls. See Glossary.
Whether to enforce positivity when finding the dictionary.
Added in version 0.20.
Whether to enforce positivity when finding the code.
Added in version 0.20.
Maximum number of iterations to perform when solving the lasso problem.
Added in version 0.22.
Control early stopping based on the norm of the differences in the dictionary between 2 steps.
To disable early stopping based on changes in the dictionary, set tol to 0.0.
Added in version 1.1.
Control early stopping based on the consecutive number of mini batches that does not yield an improvement on the smoothed cost function.
To disable convergence detection based on cost function, set max_no_improvement to None.
Added in version 1.1.
The sparse code (only returned if return_code=True).
The solutions to the dictionary learning problem.
Number of iterations run. Returned only if return_n_iter is set to True.
See also
dict_learningSolve a dictionary learning matrix factorization problem.
DictionaryLearningFind a dictionary that sparsely encodes data.
MiniBatchDictionaryLearningA faster, less accurate, version of the dictionary learning algorithm.
SparsePCASparse Principal Components Analysis.
MiniBatchSparsePCAMini-batch Sparse Principal Components Analysis.
>>> import numpy as np >>> from sklearn.datasets import make_sparse_coded_signal >>> from sklearn.decomposition import dict_learning_online >>> X, _, _ = make_sparse_coded_signal( ... n_samples=30, n_components=15, n_features=20, n_nonzero_coefs=10, ... random_state=42, ... ) >>> U, V = dict_learning_online( ... X, n_components=15, alpha=0.2, max_iter=20, batch_size=3, random_state=42 ... )
We can check the level of sparsity of U:
>>> np.mean(U == 0) np.float64(0.53...)
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)) np.float64(0.05...)
© 2007–2025 The scikit-learn developers
Licensed under the 3-clause BSD License.
https://scikit-learn.org/1.6/modules/generated/sklearn.decomposition.dict_learning_online.html