BUG fix SparseCoder to follow scikit-learn API and allow cloning#17679
BUG fix SparseCoder to follow scikit-learn API and allow cloning#17679agramfort merged 33 commits intoscikit-learn:masterfrom sdpython:i8675sparse
Conversation
|
Uhm did something about this one. Let me check. |
|
Found it: #16346 |
|
Let me know which one you prefer. PR #16346 does not test cloning and that's what caused the issue. I recommend adding one more test. |
|
I think this is important to make the estimator scikit-learn compliant (it will crash soon otherwise). Cloning is part of that so we should merge both PR. Could you merge my PR inside yours like this you supersede mine? I will review it then. |
|
I just solve the conflicts in mine so you should have no so much trouble to merge them. |
|
ok :) |
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
…into i8675sparse
|
@sdpython I will do a review. I think that we might need to add some of the test (usually done in common test) but that require specific shape for |
ogrisel
left a comment
There was a problem hiding this comment.
A few comments but otherwise LGTM. I don't think there is an easy way to re-add SparseCoder to test_common as it requires to specify the dictionary in the constructor params and the dictionary shape should be consistent with X.shape[1].
|
We would need to have something like that (it is only the transformer). def test_sparse_coder_common_transformer():
from functools import partial
from sklearn.utils.estimator_checks import check_transformer_data_not_an_array
from sklearn.utils.estimator_checks import check_transformer_general
from sklearn.utils.estimator_checks import check_transformers_unfitted
rng = np.random.RandomState(777)
n_components, n_features = 40, 3
init_dict = rng.rand(n_components, n_features)
sc = SparseCoder(init_dict)
check_transformer_data_not_an_array(sc.__class__.__name__, sc)
check_transformer_general(sc.__class__.__name__, sc)
check_transformer_general_memmap = partial(
check_transformer_general, readonly_memmap=True
)
check_transformer_general_memmap(sc.__class__.__name__, sc)
check_transformers_unfitted(sc.__class__.__name__, sc) |
|
I think this is wiser to merge with the current test and see how the |
I have encountered this while working on other estimators outside sklearn, and it's partly why I wanted to have a better way of telling common tests now to generate required data for certain tests. |
Added. |
|
There is a single thing missing which is an entry in what's new because we are deprecated one parameter. |
agramfort
left a comment
There was a problem hiding this comment.
@glemaitre merge if happy
thx @sdpython
|
actually @glemaitre had approved so go ! thx @sdpython |
|
I will make a PR adding the deprecation in what's new :) |
…kit-learn#17679) * BUG fix SparseCoder to follow scikit-learn API * TST check that get_params and set_params work as expected * address comments * PEP8 * iter * Fixes scikit-learn#8675, fix cloning for SparseCoder * remove spaces * Update _dict_learning.py * fix confusing arguments * remove unnecessary code * Update test_common.py * removes spaces * PEP8 * iter * fix merge * ignore a mypy warning * type: ignore * remove one deprecated verification * Update sklearn/decomposition/_dict_learning.py Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com> * Update sklearn/decomposition/_dict_learning.py Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com> * Update sklearn/decomposition/tests/test_dict_learning.py Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com> * test clone produces different id * review * add one more test * lint * Update sklearn/decomposition/tests/test_dict_learning.py Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com> Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
…kit-learn#17679) * BUG fix SparseCoder to follow scikit-learn API * TST check that get_params and set_params work as expected * address comments * PEP8 * iter * Fixes scikit-learn#8675, fix cloning for SparseCoder * remove spaces * Update _dict_learning.py * fix confusing arguments * remove unnecessary code * Update test_common.py * removes spaces * PEP8 * iter * fix merge * ignore a mypy warning * type: ignore * remove one deprecated verification * Update sklearn/decomposition/_dict_learning.py Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com> * Update sklearn/decomposition/_dict_learning.py Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com> * Update sklearn/decomposition/tests/test_dict_learning.py Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com> * test clone produces different id * review * add one more test * lint * Update sklearn/decomposition/tests/test_dict_learning.py Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com> Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
Reference Issues/PRs
Fixes #8675
Fixes #16336
Supersedes and closes #16346
What does this implement/fix? Explain your changes.
SparseCoder cannot be used in a GridSearchCV because it could not be cloned. A parameter in the constructor had a different meaning when stored in an instance (dictionary).