-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
GridSearchCV and HalvingGridSearchCV remove validation from __init__ and set_params #21880
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
Conversation
@glemaitre @ogrisel Could you please guide me on how to fix the failing tests? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of doing this, I think we should adapt the ParamGrid.__init__
constructor to consolidate it with the missing checks of _check_param_grid
and remove the <
8000
code class="notranslate">_check_param_grid private helper entirely.
Note that the existing code in ParamGrid.__init__
tend to raise TypeError
while approximately matching statements in _check_param_grid
would instead raise ValueError
. I am not sure if others agree but whenever the condition if checking with isinstance
clauses, I think we should raise TypeError
. For the clause that checks len(v) == 0
, I think it's correct to raise ValueError
.
This means that this code will introduce an additional behavior change for cases where we will raise TypeError
instead of ValueError
but I think this is fine because we are introducing a behavior change anyway.
Forgot to reply to:
This is because you introduced a new public function ( |
surely will be proceeding with your suggestion. |
@ogrisel I have tried to make changes to ParameterGrid in a way you asked. Let me know if I was able to replicate the behaviour you told me to. Also in case this works I think, there should be changes to tests for the same regarding the error message that its currently testing to compare using pytest. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me but the change should be documented in the changelog as done for the other PRs related to #21406.
I am surprised the tests for HalvingGridSearchCV
did not require a similar update but no big deal.
@@ -448,16 +449,18 @@ def test_grid_search_bad_param_grid(): | |||
" Single values need to be wrapped in a list" | |||
" with one element." | |||
) | |||
search = GridSearchCV(clf, param_dict) | |||
with pytest.raises(ValueError, match=error_msg): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this needs to be updated expect a TypeError
with a new expected error message. Launch the test locally to see the failure of this specific test more quickly on your local machine before pushing:
pytest sklearn/model_selection/tests/test_search.py -k test_grid_search_bad_param_grid
@ogrisel I was not able to figure out how to update the test despite several tries. Will it be possible for you to update the error messages? |
@MrinalTyagi I will have a more detailed look at the first case to get you started and then let you do the others. As a side note, please used dedicated named branch (different from Assuming that
You obviously need to replace |
I pushed 0f1e162 with what I think it is a good consolidation of the previous checks of I also fixed the first assertion. Please fetch this new commit in your local
and then run the tests as:
then fix the next failing assertion with an updated error message and/or exception type and iterate. |
Once you are done with |
Done with the |
The If you are not familiar with regexps, you at least need to know that the sub-pattern Note that, in Python, regular expressions patterns are typically written using "raw" Python string literals ( If you don't want, to write regexp patterns, you can alternatively copy the full expected message for the tuple entry that defines the |
@ogrisel Updated the error message to give more info as well as in the third test for |
@ogrisel Please merge. Thanks for the great mentoring. |
For such a complex PR we will need for a second reviewer.
Thanks for your patience ;) I think that the error messages are much improved and more consistent, on top of the original purpose of the PR to perform the checks only in |
Just so that this does not get forgotten: there is a remaining comment to address: Other than that, LGTM. |
Thank you for the reminder. Forgot that by mistake. Have updated that now. Hopefully, it looks as you expected it to be now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR @MrinalTyagi !
Co-authored-by: Thomas J. Fan <thomasjpfan@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor comments, otherwise LGTM!
@ogrisel @thomasjpfan I have written that code due to which conflicts were happening. Do I remove that code now after these tests? |
@ogrisel @thomasjpfan Do merge |
Merging since 2 approvals and after making a quick check. Thanks @MrinalTyagi |
Reference Issues/PRs
Fixes #21406
What does this implement/fix? Explain your changes.
All test working for HalvingGridSearchCV. Working on GridSearchCV
Any other comments?