Skip to content

Halving*SearchCV selects estimator with nan score as best estimator #20678

Closed

Description

@lesteve

Describe the bug

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.

Result of the git bisect:

Steps/Code to Reproduce

from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.experimental import enable_halving_search_cv  # noqa
from sklearn.model_selection import HalvingGridSearchCV

X, y = load_iris(return_X_y=True)
clf = RandomForestClassifier(random_state=0)
clf.fit(X, y)

# max_depth=-10 fails during fit
param_grid = {"max_depth": [-10, 3, 10]}
search = HalvingGridSearchCV(
    clf,
    param_grid,
    resource="n_estimators",
    max_resources=10,
    random_state=0,
    refit=False,
)
search.fit(X, y)

print("best score:", search.best_score_)
print("best params:", search.best_params_)

Expected Results

Halving*SearchCV discards models with nan scores. Output in 0.24.2 and 6484c4f is (at the very bottom, after plenty of warnings):

best score: 0.9333333333333332
best params: {'max_depth': 3, 'n_estimators': 1}

Actual Results

Halving*SearchCV thinks the best model is the one with a nan score

Output of previous snippet with 6484c4f:

best score: nan
best params: {'max_depth': -10, 'n_estimators': 1}

Versions

System:
    python: 3.9.5 | packaged by conda-forge | (default, Jun 19 2021, 00:32:32)  [GCC 9.3.0]
executable: /home/lesteve/miniconda3/bin/python
   machine: Linux-5.4.0-77-generic-x86_64-with-glibc2.31

Python dependencies:
          pip: 21.1.3
   setuptools: 49.6.0.post20210108
      sklearn: 1.0.dev0
        numpy: 1.21.0
        scipy: 1.7.0
       Cython: 0.29.23
       pandas: 1.3.0
   matplotlib: 3.4.2
       joblib: 1.0.1
threadpoolctl: 2.1.0

Built with OpenMP: True

Activity

  1. lesteve commented on Aug 4, 2021

    @lesteve
    MemberAuthor

    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.

  2. 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
  3. 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
  4. glemaitre commented on Aug 4, 2021

    @glemaitre
    Member

    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.

  5. glemaitre commented on Aug 4, 2021

    @glemaitre
    Member

    I assume that we could trigger the problem of having a fold failing with rare categories and not handling them in an encoder.

  6. lesteve commented on Aug 5, 2021

    @lesteve
    MemberAuthor

    A few notes:

    On the case where a models fits only in some splits, not sure about the behaviour and what we want ...

  7. betatim commented on Sep 23, 2022

    @betatim
    Member

    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.

    Maybe we can close this?

  8. glemaitre commented on Sep 26, 2022

    @glemaitre
    Member

    @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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions