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
Inconsistent results with HalvingGridSearchCV #24901
That is going to be hard. I am working on the Titanic dataset but I run a lengthy pipeline to get to training and test data. Can I attach a notebook that you can run?
Thanks for making the notebook. It looks like the test scores for all parameter combinations is NaN. This means there is no good way to rank the combinations. All you can really say is that combinations with a test score of NaN are all equally bad, and should be ranked last. However, the last release of scikit-learn didn't implement this behaviour.
Can you try the latest scikit-learn from main? In #24539 we tried to fix how NaN test scores are handled to implement the above ("all combos with NaN share the lowest rank")
I think there are a few things happening in this example. One is that roc_auc as a scorer produces a lot of warnings because KNeighborsClassifier doesn't have a decision_function. At least there are a lot of warnings about that. I don't understand why sometimes a score can be calculated and sometimes not though. This is one thing to investigate, but probably unrelated to this.
The other thing that is happening is that the highest mean_test_score is achieved not in the last iteration but in an earlier one. As a result grid_result.best_params_, grid_result.cv_results_['params'][best_idx] and grid_result.cv_results_['mean_test_score'][best_idx] all agree (with best_idx = grid_result.best_index_). But when you look at the output of:
you see a result ranked number 1 with a score of 0.789. Which is higher than 0.746 which is reported as the score of grid_result.best_params_. If instead you limit yourself to the last iteration when printing the results:
you recover the result from grid_result.best_params_.
It took me a while to figure this out, because it is just too intuitive to look at grid_result.cv_results_['rank_test_score'] and think "yeah, the one ranked 1 should be in best_params_". I wonder what we can do about this.
On the one hand, the successive halving algorithm says "pick solution from final iteration" and most likely the uncertainty on the score from an earlier iteration means that the score fluctuated up (it isn't truly better).
On the other hand, HalvingGridSearchCV provides a ranking for all parameter combinations of all iterations in its public API which suggests that you should look at it and use it.
A third thought/question: does it happen that algorithms get a worse test score when the resource (in this case n_samples) is increased?
Thanks for those thoughtful comments. If I understand correctly, I easily could see higher MTS on prior iterations given they come from reduced sample size. Hence, I should ignore the table and just concentrate on the last iteration. But you already do that with best_params_.
What I don't understand, and is probably a separate thread, is why I am getting nan values with KNN. KNN does have a predict_proba method so it seems I can calculate auc. So not sure what I am missing there. I also commented out the socring line (#scoring="roc_auc"), which I believe then defaults to the score method on KNN. Still getting warnings and nans. If someone asked about all these warnings and you answered in another post, please give me the link, thanks.
I have no good answer/knowledge of why the warnings occur :-/ I think the best way to figure it out would be to run the example in a debugger with a break point somewhere "far enough before but not too far before" the place the warning is raised. Then look around and try to understand why it happens. From making a change to the code a while back I remember that you have to have an agile mind to be able to follow what is happening where.
I think an actionable thing to do for this issue is to think about how to improve the documentation for HalvingGridSearchCV and friends. Or maybe add some functions/methods to help with interpreting the results. If you count the issue from a while back that pointed out that the ranking wasn't quite right when NaNs were present and this issue, then you have two in a short span of time that are at least semi related to the fact that it is tricky to understand what is what in the results from HalvingGridSearchCV. Three instances if you count the fact that it took me a moment of poking around to realise what was happening, even though I worked on a the code a few weeks ago.
I'd like to tackle this documentation clarification.
Based on the discussion above, my plan is to:
Update the best_estimator_, best_score_, and best_params_ attribute docstrings to make clear they reflect the last halving iteration, not the globally highest-ranked row in cv_results_
Add a short note in the user guide clarifying the difference between best_score_ and the highest value in cv_results_['mean_test_score']
Does this match what @betatim had in mind? Happy to refine the scope before starting.
Activity
glemaitre commentedon Nov 13, 2022
Could you provide a minimum example with simple data such that we can reproduce the issue.
fickas commentedon Nov 14, 2022
That is going to be hard. I am working on the Titanic dataset but I run a lengthy pipeline to get to training and test data. Can I attach a notebook that you can run?
fickas commentedon Nov 14, 2022
I could put my training set out on github and just provide code from there if that helps.
glemaitre commentedon Nov 14, 2022
yep until this is a file and a possibly short snippet of code that reproduce the issue, then it will be helpful for debugging.
fickas commentedon Nov 14, 2022
I am leaving you a link to a colab notebook that demonstrates the problem.
https://colab.research.google.com/drive/1b7mSvj-4k_Jo90HBwf-qP2M0Yog0NZCi?usp=sharing
betatim commentedon Nov 15, 2022
Thanks for making the notebook. It looks like the test scores for all parameter combinations is NaN. This means there is no good way to rank the combinations. All you can really say is that combinations with a test score of NaN are all equally bad, and should be ranked last. However, the last release of scikit-learn didn't implement this behaviour.
Can you try the latest scikit-learn from
main? In #24539 we tried to fix how NaN test scores are handled to implement the above ("all combos with NaN share the lowest rank")glemaitre commentedon Nov 15, 2022
In case you don't want to build from source, you can install the nightly build: https://scikit-learn.org/stable/developers/advanced_installation.html#installing-nightly-builds
betatim commentedon Nov 15, 2022
I think there are a few things happening in this example. One is that
roc_aucas a scorer produces a lot of warnings becauseKNeighborsClassifierdoesn't have adecision_function. At least there are a lot of warnings about that. I don't understand why sometimes a score can be calculated and sometimes not though. This is one thing to investigate, but probably unrelated to this.The other thing that is happening is that the highest
mean_test_scoreis achieved not in the last iteration but in an earlier one. As a resultgrid_result.best_params_,grid_result.cv_results_['params'][best_idx]andgrid_result.cv_results_['mean_test_score'][best_idx]all agree (withbest_idx = grid_result.best_index_). But when you look at the output of:you see a result ranked number 1 with a score of 0.789. Which is higher than 0.746 which is reported as the score of
grid_result.best_params_. If instead you limit yourself to the last iteration when printing the results:you recover the result from
grid_result.best_params_.It took me a while to figure this out, because it is just too intuitive to look at
grid_result.cv_results_['rank_test_score']and think "yeah, the one ranked 1 should be inbest_params_". I wonder what we can do about this.On the one hand, the successive halving algorithm says "pick solution from final iteration" and most likely the uncertainty on the score from an earlier iteration means that the score fluctuated up (it isn't truly better).
On the other hand,
HalvingGridSearchCVprovides a ranking for all parameter combinations of all iterations in its public API which suggests that you should look at it and use it.A third thought/question: does it happen that algorithms get a worse test score when the resource (in this case
n_samples) is increased?fickas commentedon Nov 16, 2022
Thanks for those thoughtful comments. If I understand correctly, I easily could see higher MTS on prior iterations given they come from reduced sample size. Hence, I should ignore the table and just concentrate on the last iteration. But you already do that with
best_params_.What I don't understand, and is probably a separate thread, is why I am getting nan values with KNN. KNN does have a
predict_probamethod so it seems I can calculate auc. So not sure what I am missing there. I also commented out the socring line (#scoring="roc_auc"), which I believe then defaults to thescoremethod on KNN. Still getting warnings and nans. If someone asked about all these warnings and you answered in another post, please give me the link, thanks.betatim commentedon Nov 17, 2022
I have no good answer/knowledge of why the warnings occur :-/ I think the best way to figure it out would be to run the example in a debugger with a break point somewhere "far enough before but not too far before" the place the warning is raised. Then look around and try to understand why it happens. From making a change to the code a while back I remember that you have to have an agile mind to be able to follow what is happening where.
I think an actionable thing to do for this issue is to think about how to improve the documentation for
HalvingGridSearchCVand friends. Or maybe add some functions/methods to help with interpreting the results. If you count the issue from a while back that pointed out that the ranking wasn't quite right when NaNs were present and this issue, then you have two in a short span of time that are at least semi related to the fact that it is tricky to understand what is what in the results fromHalvingGridSearchCV. Three instances if you count the fact that it took me a moment of poking around to realise what was happening, even though I worked on a the code a few weeks ago.dveni commentedon Nov 10, 2023
Hi!
Just came across this issue investigating why the best score was not the rank 1 in the cv_results dictionary. I think it would make sense to specify this in the documentation (https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.HalvingGridSearchCV.html). In the
best_estimator_attribute, something in the lines of "Estimator that was chosen by the search, i.e. estimator which gave highest score (or smallest loss if specified) on the left out data in the last iteration of the gridsearch."What do you think? I can propose the PR
RudrenduPaul commentedon Apr 11, 2026
I'd like to tackle this documentation clarification.
Based on the discussion above, my plan is to:
best_estimator_,best_score_, andbest_params_attribute docstrings to make clear they reflect the last halving iteration, not the globally highest-ranked row incv_results_best_score_and the highest value incv_results_['mean_test_score']Does this match what @betatim had in mind? Happy to refine the scope before starting.
RudrenduPaul commentedon Apr 11, 2026
DOC HalvingGridSearchCV/HalvingRandomSearchCV: clarify best_estimator…