You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
GridSearchCV & RandomizedSearchCV has a ranking issue when test scores are same. #21895
During best parameter candidate search when two parameter candidates have the same mean_test_<scorer_name> then the rank_test_<scorer_name> ranks them the same, and if both have rank 1 then the algorithm picks the one that come first.
Both function calls should return the same output for clf.best_params_
i.e best_parameters = {'C': 1, 'kernel': 'linear'} because the difference between the mean_train_score and the mean_test_score is the least for this parameter candidate.
The issue with using train_score is that it does not describe the generalized performance of the configuration. Also it is only available if return_train_score=True. If we do want to rank parameters with the same mean test performance, then I think using the fit time would be better. In this case, when configurations have the same mean_test_score, then the configuration that fitted quicker will be ranked higher.
Any change to the ranking will break backward compatibility. It would be best to get opinions from other maintainers before getting started.
The issue with using train_score is that it does not describe the generalized performance of the configuration. Also it is only available if return_train_score=True. If we do want to rank parameters with the same mean test performance, then I think using the fit time would be better. In this case, when configurations have the same mean_test_score, then the configuration that fitted quicker will be ranked higher.
Any change to the ranking will break backward compatibility. It would be best to get opinions from other maintainers before getting started.
But fit time is not a consistent variable. It will change the rank every time we run the code (Tested this on the example code above).
How about we don't change the ranking, instead we return all configurations that have the same mean_test_score and are ranked 1.
So now in my particular example above clf.best_params_ will return both parameter candidates and the end user will try both and choose the one that performs best for his test data.
But fit time is not a consistent variable. It will change the rank every time we run the code (Tested this on the example code above).
Good point. In that case fit time is not the most stable way to rank parameter candidates.
So now in my particular example above clf.best_params_ will return both parameter candidates
In the end *SearchCV will be refitted on the complete dataset with one parameter configuration. Even if clf.best_params_ returns a multiple candidates, *SearchCV will still need to decide which candidates to use to refit on the complete dataset.
Sort the parameters combination having same mean test score in lexicographical order and return the top most.
Having a stable way to pick between candidates with the same rank is a good stop gap solution.
@thomasjpfan Hi Thomas, Would you please elaborate a bit more on - 'Having a stable way to pick between candidates with the same rank is a good stop gap solution.' ? If you can provide any guidance to me, I will be thankful to you.
Then I think we should go for a stable way to pick between candidates as suggested by @thomasjpfan , and if "lexicographical sort" provides this stability then we should go for it.
Yes, updating the opening message in PR #21925 would be helpful.
Moreover if this issue is confirmed as a 'Bug', can someone change the label of this issue from 'Bug: triage' to 'Bug'.
@AChand20 , @thomasjpfan Have test and implemented the sorting and all local tests have passed. But azurebuild-piplines test has failed as it is asserting on the values that you will get using the previous implementation which we are trying to resolve. So looks like need to change some test output value. Working on that. I will update the detailed approach in PR.
Hello, I have updated the PR.
Can anyone of you please have a look and provide feedback on the approach and if there is anything that can be improved or modified?
Thank you in advance.
As noted in #21925 (comment) and #21925 (comment), I think this is more of a documentation issue on how we rank ties. A long term solution would be to have a callable to allow users to specific how they want to rank the results.
Activity
AChand20 commentedon Dec 6, 2021
I would like to work on this issue if the bug gets confirmed.
Nivi09 commentedon Dec 7, 2021
Hello, I would like to work on this issue.
thomasjpfan commentedon Dec 7, 2021
The issue with using
train_scoreis that it does not describe the generalized performance of the configuration. Also it is only available ifreturn_train_score=True. If we do want to rank parameters with the same mean test performance, then I think using thefittime would be better. In this case, when configurations have the samemean_test_score, then the configuration that fitted quicker will be ranked higher.Any change to the ranking will break backward compatibility. It would be best to get opinions from other maintainers before getting started.
AChand20 commentedon Dec 8, 2021
But
fit timeis not a consistent variable. It will change the rank every time we run the code (Tested this on the example code above).How about we don't change the ranking, instead we return all configurations that have the same
mean_test_scoreand are ranked 1.So now in my particular example above
clf.best_params_will return both parameter candidates and the end user will try both and choose the one that performs best for his test data.Nivi09 commentedon Dec 8, 2021
Hello @AChand20 and @thomasjpfan, as AChand20 mentioned, I also thought of the same thing.
Two approaches, :
I would like to work on this. Please let me know if my approach is correct and if I can start to work on this.
Working on scikit-learn#21895
Added code block to add fix for scikit-learn#21895
Added code block to add fix for scikit-learn#21895
Added code block to add fix for scikit-learn#21895
Added code block to add fix for scikit-learn#21895
Added code block to add fix for scikit-learn#21895
Added code block to add fix for scikit-learn#21895
Added code block to add fix for scikit-learn#21895
Added code block to add fix for scikit-learn#21895
thomasjpfan commentedon Dec 9, 2021
Good point. In that case fit time is not the most stable way to rank parameter candidates.
In the end
*SearchCVwill be refitted on the complete dataset with one parameter configuration. Even ifclf.best_params_returns a multiple candidates,*SearchCVwill still need to decide which candidates to use to refit on the complete dataset.Having a stable way to pick between candidates with the same rank is a good stop gap solution.
Nivi09 commentedon Dec 10, 2021
@thomasjpfan Hi Thomas, Would you please elaborate a bit more on - 'Having a stable way to pick between candidates with the same rank is a good stop gap solution.' ? If you can provide any guidance to me, I will be thankful to you.
Added code block to add fix for scikit-learn scikit-learn#21895
thomasjpfan commentedon Dec 10, 2021
I think you already proposed a solution in #21925. Please update the opening message in the PR to describe what you mean by "lexicographical sort".
AChand20 commentedon Dec 11, 2021
Then I think we should go for a stable way to pick between candidates as suggested by @thomasjpfan , and if "lexicographical sort" provides this stability then we should go for it.
Yes, updating the opening message in PR #21925 would be helpful.
Moreover if this issue is confirmed as a 'Bug', can someone change the label of this issue from 'Bug: triage' to 'Bug'.
Nivi09 commentedon Dec 11, 2021
@AChand20 , @thomasjpfan Have test and implemented the sorting and all local tests have passed. But azurebuild-piplines test has failed as it is asserting on the values that you will get using the previous implementation which we are trying to resolve. So looks like need to change some test output value. Working on that. I will update the detailed approach in PR.
Nivi09 commentedon Dec 21, 2021
@AChand20 , @thomasjpfan
Hello, I have updated the PR.
Can anyone of you please have a look and provide feedback on the approach and if there is anything that can be improved or modified?
Thank you in advance.
Regards.
thomasjpfan commentedon Dec 21, 2021
For future reference, it is best to ping in the PR when you want attention on a PR. This makes the notification link directly to the PR.
thomasjpfan commentedon Sep 13, 2022
As noted in #21925 (comment) and #21925 (comment), I think this is more of a documentation issue on how we rank ties. A long term solution would be to have a callable to allow users to specific how they want to rank the results.