Skip to content

Dictionary Learning: transform_alpha default equal to alpha #19159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions doc/whats_new/v1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ Changelog
- |API| :class:`cluster.Birch` attributes, `fit_` and `partial_fit_`, are
deprecated and will be removed in 1.2. :pr:`19297` by `Thomas Fan`_.

:mod:`sklearn.decomposition`
............................

- |API| In :class:`decomposition.DictionaryLearning`,
:class:`decomposition.MiniBatchDictionaryLearning`,
:func:`dict_learning` and :func:`dict_learning_online`,
`transform_alpha` will be equal to `alpha` instead of 1.0 by default
starting from version 1.2
:pr:`19159` by :user:`Benoît Malézieux <bmalezieux>`.

- |Fix| Fixes incorrect multiple data-conversion warnings when clustering
boolean data. :pr:`19046` by :user:`Surya Prakash <jdsurya>`.

Expand Down
32 changes: 19 additions & 13 deletions sklearn/decomposition/_dict_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import time
import sys
import itertools
import warnings

from math import ceil

Expand Down Expand Up @@ -911,10 +912,21 @@ def _transform(self, X, dictionary):
SparseCoder."""
X = self._validate_data(X, reset=False)

# transform_alpha has to be changed in _transform
# this is done for consistency with the value of alpha
if (hasattr(self, "alpha") and self.alpha != 1. and
self.transform_alpha is None):
warnings.warn("By default transform_alpha will be equal to"
"alpha instead of 1.0 starting from version 1.2",
FutureWarning)
transform_alpha = 1. # TODO change to self.alpha in 1.2
else:
transform_alpha = self.transform_alpha

code = sparse_encode(
X, dictionary, algorithm=self.transform_algorithm,
n_nonzero_coefs=self.transform_n_nonzero_coefs,
alpha=self.transform_alpha, max_iter=self.transform_max_iter,
alpha=transform_alpha, max_iter=self.transform_max_iter,
n_jobs=self.n_jobs, positive=self.positive_code)

if self.split_sign:
Expand Down Expand Up @@ -1186,19 +1198,16 @@ class DictionaryLearning(_BaseSparseCoding, BaseEstimator):

transform_n_nonzero_coefs : int, default=None
Number of nonzero coefficients to target in each column of the
solution. This is only used by `algorithm='lars'` and `algorithm='omp'`
and is overridden by `alpha` in the `omp` case. If `None`, then
solution. This is only used by `algorithm='lars'` and
`algorithm='omp'`. If `None`, then
`transform_n_nonzero_coefs=int(n_features / 10)`.

transform_alpha : float, default=None
If `algorithm='lasso_lars'` or `algorithm='lasso_cd'`, `alpha` is the
penalty applied to the L1 norm.
If `algorithm='threshold'`, `alpha` is the absolute value of the
threshold below which coefficients will be squashed to zero.
If `algorithm='omp'`, `alpha` is the tolerance parameter: the value of
the reconstruction error targeted. In this case, it overrides
`n_nonzero_coefs`.
If `None`, default to 1.0
If `None`, defaults to `alpha`.

n_jobs : int or None, default=None
Number of parallel jobs to run.
Expand Down Expand Up @@ -1428,19 +1437,16 @@ class MiniBatchDictionaryLearning(_BaseSparseCoding, BaseEstimator):

transform_n_nonzero_coefs : int, default=None
Number of nonzero coefficients to target in each column of the
solution. This is only used by `algorithm='lars'` and `algorithm='omp'`
and is overridden by `alpha` in the `omp` case. If `None`, then
solution. This is only used by `algorithm='lars'` and
`algorithm='omp'`. If `None`, then
`transform_n_nonzero_coefs=int(n_features / 10)`.

transform_alpha : float, default=None
If `algorithm='lasso_lars'` or `algorithm='lasso_cd'`, `alpha` is the
penalty applied to the L1 norm.
If `algorithm='threshold'`, `alpha` is the absolute value of the
threshold below which coefficients will be squashed to zero.
If `algorithm='omp'`, `alpha` is the tolerance parameter: the value of
the reconstruction error targeted. In this case, it overrides
`n_nonzero_coefs`.
If `None`, default to 1.
If `None`, defaults to `alpha`.

verbose : bool, default=False
To control the verbosity of the procedure.
Expand Down
8 changes: 8 additions & 0 deletions sklearn/decomposition/tests/test_dict_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,3 +573,11 @@ def test_sparse_coder_n_features_in():
d = np.array([[1, 2, 3], [1, 2, 3]])
sc = SparseCoder(d)
assert sc.n_features_in_ == d.shape[1]


@pytest.mark.parametrize("Estimator", [DictionaryLearning,
MiniBatchDictionaryLearning])
def test_warning_default_transform_alpha(Estimator):
dl = Estimator(alpha=0.1)
with pytest.warns(FutureWarning, match="default transform_alpha"):
dl.fit_transform(X)