8000 DOC add examples to SparseCoder and DictionaryLearning (#17521) · scikit-learn/scikit-learn@11c6705 · GitHub
[go: up one dir, main page]

Skip to content

Commit 11c6705

Browse files
Mariam-keMariam-keglemaitre
authored
DOC add examples to SparseCoder and DictionaryLearning (#17521)
Co-authored-by: Mariam-ke <mariam.haji01@gmail.com> Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
1 parent 45a6ef7 commit 11c6705

File tree

1 file changed

+77
-3
lines changed

1 file changed

+77
-3
lines changed

sklearn/decomposition/_dict_learning.py

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,28 @@ class SparseCoder(SparseCodingMixin, BaseEstimator):
10061006
components_ : array, [n_components, n_features]
10071007
The unchanged dictionary atoms
10081008
1009-
See also
1009+
Examples
1010+
--------
1011+
>>> import numpy as np
1012+
>>> from sklearn.decomposition import SparseCoder
1013+
>>> X = np.array([[-1, -1, -1], [0, 0, 3]])
1014+
>>> dictionary = np.array(
1015+
... [[0, 1, 0],
1016+
... [-1, -1, 2],
1017+
... [1, 1, 1],
1018+
... [0, 1, 1],
1019+
... [0, 2, 1]],
1020+
... dtype=np.float64
1021+
... )
1022+
>>> coder = SparseCoder(
1023+
... dictionary=dictionary, transform_algorithm='lasso_lars',
1024+
... transform_alpha=1e-10,
1025+
... )
1026+
>>> coder.transform(X)
1027+
array([[ 0., 0., -1., 0., 0.],
1028+
[ 0., 1., 1., 0., 0.]])
1029+
1030+
See Also
10101031
--------
10111032
DictionaryLearning
10121033
MiniBatchDictionaryLearning
@@ -1060,7 +1081,7 @@ class DictionaryLearning(SparseCodingMixin, BaseEstimator):
10601081
10611082
Solves the optimization problem::
10621083
1063-
(U^*,V^*) = argmin 0.5 || Y - U V ||_2^2 + alpha * || U ||_1
1084+
(U^*,V^*) = argmin 0.5 || X - U V ||_2^2 + alpha * || U ||_1
10641085
(U,V)
10651086
with || V_k ||_2 = 1 for all 0 <= k < n_components
10661087
@@ -1173,6 +1194,33 @@ class DictionaryLearning(SparseCodingMixin, BaseEstimator):
11731194
n_iter_ : int
11741195
Number of iterations run.
11751196
1197+
Examples
1198+
--------
1199+
>>> import numpy as np
1200+
>>> from sklearn.datasets import make_sparse_coded_signal
1201+
>>> from sklearn.decomposition import DictionaryLearning
1202+
>>> X, dictionary, code = make_sparse_coded_signal(
1203+
... n_samples=100, n_components=15, n_features=20, n_nonzero_coefs=10,
1204+
... random_state=42,
1205+
... )
1206+
>>> dict_learner = DictionaryLearning(
1207+
... n_components=15, transform_algorithm='lasso_lars', random_state=42,
1208+
... )
1209+
>>> X_transformed = dict_learner.fit_transform(X)
1210+
1211+
We can check the level of sparsity of `X_transformed`:
1212+
1213+
>>> np.mean(X_transformed == 0)
1214+
0.88...
1215+
1216+
We can compare the average squared euclidean norm of the reconstruction
1217+
error of the sparse coded signal relative to the squared euclidean norm of
1218+
the original signal:
1219+
1220+
>>> X_hat = X_transformed @ dict_learner.components_
1221+
>>> np.mean(np.sum((X_hat - X) ** 2, axis=1) / np.sum(X ** 2, axis=1))
1222+
0.07...
1223+
11761224
Notes
11771225
-----
11781226
**References:**
@@ -1258,7 +1306,7 @@ class MiniBatchDictionaryLearning(SparseCodingMixin, BaseEstimator):
12581306
12591307
Solves the optimization problem::
12601308
1261-
(U^*,V^*) = argmin 0.5 || Y - U V ||_2^2 + alpha * || U ||_1
1309+
(U^*,V^*) = argmin 0.5 || X - U V ||_2^2 + alpha * || U ||_1
12621310
(U,V)
12631311
with || V_k ||_2 = 1 for all 0 <= k < n_components
12641312
@@ -1378,6 +1426,32 @@ class MiniBatchDictionaryLearning(SparseCodingMixin, BaseEstimator):
13781426
RandomState instance that is generated either from a seed, the random
13791427
number generattor or by `np.random`.
13801428
1429+
Examples
1430+
--------
1431+
>>> import numpy as np
1432+
>>> from sklearn.datasets import make_sparse_coded_signal
1433+
>>> from sklearn.decomposition import MiniBatchDictionaryLearning
1434+
>>> X, dictionary, code = make_sparse_coded_signal(
1435+
... n_samples=100, n_components=15, n_features=20, n_nonzero_coefs=10,
1436+
... random_state=42)
1437+
>>> dict_learner = MiniBatchDictionaryLearning(
1438+
... n_components=15, transform_algorithm='lasso_lars', random_state=42,
1439+
... )
1440+
>>> X_transformed = dict_learner.fit_transform(X)
1441+
1442+
We can check the level of sparsity of `X_transformed`:
1443+
1444+
>>> np.mean(X_transformed == 0)
1445+
0.87...
1446+
1447+
We can compare the average squared euclidean norm of the reconstruction
1448+
error of the sparse coded signal relative to the squared euclidean norm of
1449+
the original signal:
1450+
1451+
>>> X_hat = X_transformed @ dict_learner.components_
1452+
>>> np.mean(np.sum((X_hat - X) ** 2, axis=1) / np.sum(X ** 2, axis=1))
1453+
0.10...
1454+
13811455
Notes
13821456
-----
13831457
**References:**

0 commit comments

Comments
 (0)
0