8000 More informative error message when set_params has invalid values by ogrisel · Pull Request #21542 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

More informative error message when set_params has invalid values #21542

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 5 commits into from
Nov 3, 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
4 changes: 4 additions & 0 deletions doc/whats_new/v1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Changelog
message suggests potential solutions.
:pr:`21219` by :user:`Olivier Grisel <ogrisel>`.

- |Enhancement| All scikit-learn models now generate a more informative
error message when setting invalid hyper-parameters with `set_params`.
:pr:`21542` by :user:`Olivier Grisel <ogrisel>`.

Comment on lines +48 to +51
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could go in 1.0.2, no ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather only put bugfixes in bugfix releases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem :D

Suggested change
- |Enhancement| All scikit-learn models now generate a more informative
error message when setting invalid hyper-parameters with `set_params`.
:pr:`21542` by :user:`Olivier Grisel <ogrisel>`.
- |Fix| All scikit-learn models now generate a more informative
error message when setting invalid hyper-parameters with `set_params`.
:pr:`21542` by :user:`Olivier Grisel <ogrisel>`.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jst kidding. I'm ok with that :)

:mod:`sklearn.calibration`
..........................

Expand Down
9 changes: 4 additions & 5 deletions sklearn/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ def get_params(self, deep=True):
return out

def set_params(self, **params):
"""
Set the parameters of this estimator.
"""Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects
(such as :class:`~sklearn.pipeline.Pipeline`). The latter have
Expand All @@ -239,10 +238,10 @@ def set_params(self, **params):
for key, value in params.items():
key, delim, sub_key = key.partition("__")
if key not in valid_params:
local_valid_params = self._get_param_names()
raise ValueError(
"Invalid parameter %s for estimator %s. "
"Check the list of available parameters "
"with `estimator.get_params().keys()`." % (key, self)
f"Invalid parameter {key!r} for estimator {self}. "
f"Valid parameters are: {local_valid_params!r}."
)

if delim:
Expand Down
23 changes: 17 additions & 6 deletions sklearn/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,10 @@ def test_pipeline_init():
repr(pipe)

# Check that params are not set when naming them wrong
msg = "Invalid parameter C for estimator SelectKBest"
msg = re.escape(
"Invalid parameter 'C' for estimator SelectKBest(). Valid parameters are: ['k',"
" 'score_func']."
)
with pytest.raises(ValueError, match=msg):
pipe.set_params(anova__C=0.1)

Expand Down Expand Up @@ -316,18 +319,26 @@ def test_pipeline_raise_set_params_error():

# expected error message
error_msg = re.escape(
f"Invalid parameter fake for estimator {pipe}. "
"Check the list of available parameters "
"with `estimator.get_params().keys()`."
"Invalid parameter 'fake' for estimator Pipeline(steps=[('cls',"
" LinearRegression())]). Valid parameters are: ['memory', 'steps', 'verbose']."
)

with pytest.raises(ValueError, match=error_msg):
pipe.set_params(fake="nope")

# nested model check
# invalid outer parameter name for compound parameter: the expected error message
# is the same as above.
with pytest.raises(ValueError, match=error_msg):
pipe.set_params(fake__estimator="nope")

# expected error message for invalid inner parameter
error_msg = re.escape(
"Invalid parameter 'invalid_param' for estimator LinearRegression(). Valid"
" parameters are: ['copy_X', 'fit_intercept', 'n_jobs', 'normalize',"
" 'positive']."
)
with pytest.raises(ValueError, match=error_msg):
pipe.set_params(cls__invalid_param="nope")


def test_pipeline_methods_pca_svm():
# Test the various methods of the pipeline (pca + svm).
Expand Down
0