Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions sklearn/decomposition/_dict_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ class DictionaryLearning(SparseCodingMixin, BaseEstimator):

Solves the optimization problem::

(U^*,V^*) = argmin 0.5 || Y - U V ||_2^2 + alpha * || U ||_1
(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

Expand Down Expand Up @@ -1170,6 +1170,21 @@ class DictionaryLearning(SparseCodingMixin, BaseEstimator):
n_iter_ : int
Number of iterations run.

Examples
--------
>>> import numpy as np
>>> from sklearn.datasets import make_sparse_coded_signal
>>> from sklearn.decomposition import DictionaryLearning
>>>
>>> np.random.seed(42)
>>> X, dictionary, code = make_sparse_coded_signal(
... n_samples=100, n_components=15, n_features=20, n_nonzero_coefs=10)
>>> dico = DictionaryLearning(n_components=15,
... transform_algorithm='lasso_lars')
>>> X_transformed = dico.fit_transform(X)
>>> np.mean(X_transformed == 0) # represents data using a sparse code
0.87

Notes
-----
**References:**
Expand Down Expand Up @@ -1255,7 +1270,7 @@ class MiniBatchDictionaryLearning(SparseCodingMixin, BaseEstimator):

Solves the optimization problem::

(U^*,V^*) = argmin 0.5 || Y - U V ||_2^2 + alpha * || U ||_1
(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

Expand Down Expand Up @@ -1375,6 +1390,21 @@ class MiniBatchDictionaryLearning(SparseCodingMixin, BaseEstimator):
RandomState instance that is generated either from a seed, the random
number generattor or by `np.random`.

Examples
--------
>>> import numpy as np
>>> from sklearn.datasets import make_sparse_coded_signal
>>> from sklearn.decomposition import MiniBatchDictionaryLearning
>>>
>>> np.random.seed(42)
>>> X, dictionary, code = make_sparse_coded_signal(
... n_samples=100, n_components=15, n_features=20, n_nonzero_coefs=10)
>>> dico = MiniBatchDictionaryLearning(n_components=15,
... transform_algorithm='lasso_lars')
>>> X_transformed = dico.fit_transform(X)
>>> np.mean(X_transformed == 0) # represents data using a sparse code
0.83...

Notes
-----
**References:**
Expand Down