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
Halving*SearchCV selects estimator with nan score as best estimator #20678
Halving*SearchCV selects nan scores as the best scores. With the default error_score='warn' that means that the best selected model fails when fitted on the training set. Doing a git bisect, this seems to have been introduced by #20203. I have not yet understood why exactly.
Hmmm digging a bit more it seems like #20203 may be a red herring. This is a change of behaviour for this particular snippet but AFAICT there seems to be something wrong in 0.24.2 with Halving*SearchCV and nan scores.
With the previous snippet in 0.24.2 if you use refit=True you get an error ValueError: max_depth must be greater than zero showing that an estimator with a nan score has been chosen as the best estimator. I'll update the issue title to reflect this.
changed the title [-]Halving*SearchCV selects nan scores as best scores in main[/-][+]Halving*SearchCV selects estimator with nan scores as best estimator[/+]on Aug 4, 2021
changed the title [-]Halving*SearchCV selects estimator with nan scores as best estimator[/-][+]Halving*SearchCV selects estimator with nan score as best estimator[/+]on Aug 4, 2021
For the nan case, we should use np.nanargmax instead of np.argmax.
However, since error_score could be numeric, we should make the documentation also more explicit regarding the fact that we will get the highest numerical value and thus, one should not pass a large error_score value. Otherwise, it will be selected as the best model.
The original example for reproducing this problem doesn't reproduce the problem anymore, because RandomForestClassifier now raises an exception when you pass in -10 for max_depth. Instead an exception is raised. This happens independently of whether you set error_score to the default value or np.nan.
@betatim we better validate parameters nowadays so the simple example in the original post would not fail. However, the problem still exists and could happen in some cases: #20678 (comment)
I can think of a SearchCV with the adult census where you have a single data point with the Dutch nationality that would trigger some failure at predict time and cause the above problem.
Activity
lesteve commentedon Aug 4, 2021
Hmmm digging a bit more it seems like #20203 may be a red herring. This is a change of behaviour for this particular snippet but AFAICT there seems to be something wrong in 0.24.2 with
Halving*SearchCVand nan scores.With the previous snippet in 0.24.2 if you use
refit=Trueyou get an errorValueError: max_depth must be greater than zeroshowing that an estimator with a nan score has been chosen as the best estimator. I'll update the issue title to reflect this.[-]Halving*SearchCV selects nan scores as best scores in main[/-][+]Halving*SearchCV selects estimator with nan scores as best estimator[/+][-]Halving*SearchCV selects estimator with nan scores as best estimator[/-][+]Halving*SearchCV selects estimator with nan score as best estimator[/+]glemaitre commentedon Aug 4, 2021
For the
nancase, we should usenp.nanargmaxinstead ofnp.argmax.However, since
error_scorecould be numeric, we should make the documentation also more explicit regarding the fact that we will get the highest numerical value and thus, one should not pass a largeerror_scorevalue. Otherwise, it will be selected as the best model.glemaitre commentedon Aug 4, 2021
I assume that we could trigger the problem of having a fold failing with rare categories and not handling them in an encoder.
lesteve commentedon Aug 5, 2021
A few notes:
*SearchCVobjects if a model fails during fit on all the splits as mentioned in Warn in the main process when a fit fails during a cross-validation #20619 (comment)rank_test_scorerather than usingmean_test_scorewhich seems more reliable (maybe it is computed before we replace the scores witherror_score)On the case where a models fits only in some splits, not sure about the behaviour and what we want ...
betatim commentedon Sep 23, 2022
The original example for reproducing this problem doesn't reproduce the problem anymore, because
RandomForestClassifiernow raises an exception when you pass in-10formax_depth. Instead an exception is raised. This happens independently of whether you seterror_scoreto the default value ornp.nan.Maybe we can close this?
glemaitre commentedon Sep 26, 2022
@betatim we better validate parameters nowadays so the simple example in the original post would not fail. However, the problem still exists and could happen in some cases: #20678 (comment)
I can think of a SearchCV with the adult census where you have a single data point with the Dutch nationality that would trigger some failure at predict time and cause the above problem.