From 3e9df9c6d2ea75e077c100f344486db089e38136 Mon Sep 17 00:00:00 2001 From: Stephan Steinfurt Date: Mon, 13 Apr 2020 00:00:12 +0200 Subject: [PATCH] Add examples to dictionary learning --- sklearn/decomposition/_dict_learning.py | 34 +++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/sklearn/decomposition/_dict_learning.py b/sklearn/decomposition/_dict_learning.py index 6b2d76ce2143b..6aa9ea1cc80b7 100644 --- a/sklearn/decomposition/_dict_learning.py +++ b/sklearn/decomposition/_dict_learning.py @@ -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 @@ -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:** @@ -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 @@ -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:**