Skip to content

[MRG+1] Change default error_score to raise_deprecating - #10677

Merged
jnothman merged 7 commits into
scikit-learn:masterfrom
Zhdanovich:is_10309
Mar 2, 2018
Merged

jnothman merged 7 commits into
scikit-learn:masterfrom
Zhdanovich:is_10309

Conversation

@Zhdanovich

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #10309.

What does this implement/fix? Explain your changes.

Changes default parameter of error_score to raise-deprecating

Any other comments?

Should be method _fit_and_score(cross_validation.py) be updated in the same way?

@Zhdanovich Zhdanovich changed the title Change default error_score to raise_deprecating [MRG] Change default error_score to raise_deprecating Feb 22, 2018

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to

  • Test that the FutureWarning is raised, perhaps only in the case that the fit errors
  • Test that if a score is NaN *SearchCV does not select this as the best score because np.argmax([1, np.nan]) == 1.

Sorry to make this more complicated, though you've done a good job so far. Thanks!

Comment thread sklearn/model_selection/_search.py Outdated
Additional parameter passed to the fit function of the estimator.

error_score : 'raise' (default) or numeric
error_score : 'raise-deprecating' (default), 'raise' or numeric

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think of raise-deprecating as an implementation detail that we shouldn't document.

Rather, we need to say that From version 0.22 the default will be np.nan

Comment thread sklearn/model_selection/_validation.py Outdated
raise
elif error_score == 'raise-deprecating':
warnings.warn("Estimator fit failed. In the future,"
"raise-deprecating error will not be"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to reference raise-deprecating, just explain how the default error_score behaviour will change

Comment thread sklearn/model_selection/_validation.py Outdated
"raise-deprecating error will not be"
"used as a default value. There will be"
"possibility to choose between error_score='raise',"
"numeric or the new default error_score=np.nan")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a FutureWarning

*fit_and_score_args, **fit_and_score_kwargs)

# check if exception was raised, with default error_score argument
assert_raise_message(ValueError, "", _fit_and_score,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we not test for the message text?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please elaborate on this? Maybe I'm wrong, but when raise method is invoked there is no message.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be the original error from fit, in this case "Failing classifier failed as required"

@Zhdanovich Zhdanovich changed the title [MRG] Change default error_score to raise_deprecating [WIP] Change default error_score to raise_deprecating Feb 22, 2018
@Zhdanovich

Copy link
Copy Markdown
Contributor Author

@jnothman "Test that if a score is NaN *SearchCV does not select this as the best score because np.argmax([1, np.nan]) == 1." - should this be done now? Feels like it's necessary to do when an actual change(error_score=np.nan) is in place.
It's already possible to pass np.nan as error_score, this will break current behavior, not sure if anyone uses it in this way.

@Zhdanovich Zhdanovich changed the title [WIP] Change default error_score to raise_deprecating [MRG] Change default error_score to raise_deprecating Feb 23, 2018
@jnothman

jnothman commented Feb 24, 2018 via email

Copy link
Copy Markdown
Member

@Zhdanovich

Copy link
Copy Markdown
Contributor Author

@jnothman is not ok to merge this now. And I start on another part on Monday?

@jnothman

Copy link
Copy Markdown
Member

We'll only merge with two core devs approving. But you can start on the other pr simultaneously

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise lgtm

Comment thread sklearn/model_selection/_validation.py Outdated
raise

elif error_score == 'raise-deprecating':
warnings.warn("Estimator fit failed. From version 0.22"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing a space between "0.22" and "the"

Comment thread sklearn/model_selection/_validation.py Outdated

elif error_score == 'raise-deprecating':
warnings.warn("Estimator fit failed. From version 0.22"
"the default valuer will be error_score=np.nan",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valuer -> value

However, I think this is better expressed with something like "From version 0.22, errors during fit will result in a cross validation score of NaN by default. Use error_score='raise' if you want an exception raised."

Comment thread sklearn/model_selection/_validation.py Outdated
" numeric value. (Hint: if using 'raise', please"
" make sure that it has been spelled correctly.)")
raise ValueError("error_score must be the string 'raise',"
"'raise-deprecating' or a numeric value. (Hint:"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want users to ever manually specify 'raise-deprecating', so please just don't mention it.

@Zhdanovich

Copy link
Copy Markdown
Contributor Author

@jnothman updated

@jnothman

Copy link
Copy Markdown
Member

Please append commits rather than amending previous commits. It makes it much easier to track what's changed from review to review.

Comment thread sklearn/model_selection/_validation.py Outdated

elif error_score == 'raise-deprecating':
warnings.warn("Estimator fit failed. From version 0.22 "
"the default value will be error_score=np.nan",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggested a more verbose error message here because I don't believe that every user who encounters this message will be familiar with error_score or appreciate what "the default value will be" means to them

@Zhdanovich

Copy link
Copy Markdown
Contributor Author

@jnothman just to be sure about another part. If a user doesn't specify error_score(error_score=np.nan) and estimator fails to fit, then we filter such cases when checking the best parameters?

@jnothman

Copy link
Copy Markdown
Member

If a user doesn't specify error_score(error_score=np.nan) and estimator fails to fit, then we filter such cases when checking the best parameters?

If it's raising an error then there's no such thing as a best fit. If the user specifies a non-NaN number, then we treat that as currently handled: sort the mean score. If it's np.NaN then the mean score becomes NaN, and the only problem is that NaN sorts as a higher value than any other:

>>> np.argsort(np.asarray([np.nan, 1, 2, 3]))
array([1, 2, 3, 0])

However, it looks like scipy.stats.rankdata, which is what we use in BaseSearchCV to determine the best system, behaves differently, and always assigns np.NaN the lowest rank:

>>> from scipy.stats import rankdata
>>> rankdata(np.asarray([np.nan, 1, 2, 3]))
array([4., 1., 2., 3.])
>>> rankdata(-np.asarray([np.nan, 1, 2, 3]))
array([4., 3., 2., 1.])

So what we have already is probably working correctly, but it needs a test (which I don't think we already have).

@jnothman

Copy link
Copy Markdown
Member

(We don't test that despite testing other aspects of error_score=NaN in test_grid_search_failing_classifier. Because we set all fits to fail, we can't see what happens when comparing a NaN score to a non-NaN score.)

1 similar comment
@jnothman

Copy link
Copy Markdown
Member

(We don't test that despite testing other aspects of error_score=NaN in test_grid_search_failing_classifier. Because we set all fits to fail, we can't see what happens when comparing a NaN score to a non-NaN score.)

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grammar nitpicks

Comment thread sklearn/model_selection/_validation.py Outdated
raise

elif error_score == 'raise-deprecating':
warnings.warn("Estimator failed to fit train-test for this "

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this -> these. Drop "train-test".

Comment thread sklearn/model_selection/_validation.py Outdated

elif error_score == 'raise-deprecating':
warnings.warn("Estimator failed to fit train-test for this "
"parameters, the error will be raised. If you want "

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

, -> ;

Comment thread sklearn/model_selection/_validation.py Outdated
"then specify error_score parameter. From version "
"0.22 the default value will be error_score=np.nan",
warnings.warn("Estimator failed to fit for these parameters; the "
"error will be raised. If you want value to be "

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really should be "want a value"

Comment thread sklearn/model_selection/_validation.py Outdated
"0.22 the default value will be error_score=np.nan",
warnings.warn("Estimator failed to fit for these parameters; the "
"error will be raised. If you want value to be "
"assigned to the score in such case, then specify "

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really should be "such a case" or "such cases"

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM

@Zhdanovich Zhdanovich changed the title [MRG] Change default error_score to raise_deprecating [MRG+1] Change default error_score to raise_deprecating Feb 26, 2018
@Zhdanovich

Copy link
Copy Markdown
Contributor Author

@jnothman thanks a lot for the help! How it usually works, how I can get a second reviewer?

@jnothman

jnothman commented Feb 26, 2018 via email

Copy link
Copy Markdown
Member

@rasbt rasbt mentioned this pull request Feb 27, 2018
2 tasks
@Zhdanovich

Copy link
Copy Markdown
Contributor Author

@rth please take a look

@Zhdanovich

Copy link
Copy Markdown
Contributor Author

@qinhanmin2014 please take a look

@rth

rth commented Feb 27, 2018

Copy link
Copy Markdown
Member

Thanks for this PR @Zhdanovich

I think of raise-deprecating as an implementation detail that we shouldn't document.

@jnothman I see your point, but it's still kind of unexpected to have a default value not documented for a couple of versions. Anyone looking at the docs (latest build here) or the code will wonder what the default value actually does.

Instead of

From version 0.22 the default will be np.nan

how about

Default is 'raise' but from version 0.22 it will change to np.nan.

particularly since most users will never see the warning as it requires for grid search to fail, which is not that frequent. WDYT?

assert_raise_message(ValueError, "Failing classifier failed as required",
_fit_and_score, *fit_and_score_args)

# check if warning was raised, with default error_score argument

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, but I think "is raised" in the present is more common (in all 3 comments): after all, it's "assert_raise_message" not "asssert_raised_message".

"default value will be error_score=np.nan")
try:
assert_warns_message(FutureWarning, warning_message, _fit_and_score,
*fit_and_score_args)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use,

with pytest.raises(ValueError):
    assert_warns_message(..)

here? As this try: except: pass leaves the reader puzzled as to whether it will raise an exception or not.

@Zhdanovich

Copy link
Copy Markdown
Contributor Author

@rth thanks for the review.

how about

Default is 'raise' but from version 0.22 it will change to np.nan.

But it's also not true since the default value is raise-deprecating.
ping @jnothman

@jnothman

jnothman commented Feb 28, 2018 via email

Copy link
Copy Markdown
Member

@Zhdanovich

Copy link
Copy Markdown
Contributor Author

@rth updated. Please take a look

Comment thread sklearn/model_selection/_search.py Outdated
FitFailedWarning is raised. This parameter does not affect the refit
step, which will always raise the error.
step, which will always raise the error. From version 0.22 the default
will be np.nan

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"." is missing at the end. Also could you please change the last sentence to match the message in other docstrings?

Comment thread sklearn/model_selection/_validation.py Outdated
"error will be raised. If you want a value to be "
"assigned to the score in such cases, then specify "
"error_score parameter. Default is 'raise' but from "
"version 0.22 it will change to np.nan.",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this warning should be more clear a) about why a user is getting it b) how to make it go away.
Something along the lines of what @jnothman said in #10677 (comment) might be better I think,

"From version 0.22, errors during fit will result in a cross validation score of NaN by default. Use error_score='raise' if you want an exception raised."

Once sentence for a) and one for b) . and maybe add "[...] or error_score=np.nan to adopt the behavior from version 0.22."

@Zhdanovich

Copy link
Copy Markdown
Contributor Author

@rth please take a look

@rth

rth commented Mar 1, 2018

Copy link
Copy Markdown
Member

Just one space too many after "or" in raised or error_score=np.nan in 2 warning messages.

Otherwise LGTM. Thanks @Zhdanovich

@Zhdanovich

Copy link
Copy Markdown
Contributor Author

@rth Thanks. Updated!

@jnothman
jnothman merged commit 76b5b2a into scikit-learn:master Mar 2, 2018
@jnothman

jnothman commented Mar 2, 2018

Copy link
Copy Markdown
Member

Thanks!

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, now that I've merged, I've realised two small things are wrong:

  • missing an entry in doc/whats_new/v0.20.rst
  • we've changed _fit_and_score's default, and we should not have, because cross_validate and cross_val_score do not (currently) provide an error_score option. Maybe that's something else that should change. But at a minimum, we need to make sure that cross_validate doesn't raise an irrelevant FutureWarning.

@Zhdanovich

Copy link
Copy Markdown
Contributor Author

@jnothman I can have a look

@Zhdanovich

Copy link
Copy Markdown
Contributor Author

Now I see that I asked this question in "Any other comments?" section

@Zhdanovich

Copy link
Copy Markdown
Contributor Author

@jnothman

we've changed _fit_and_score's default, and we should not have, because cross_validate and cross_val_score do not (currently) provide an error_score option. Maybe that's something else that should change. But at a minimum, we need to make sure that cross_validate doesn't raise an irrelevant FutureWarning.

Should I for now pass error_score='raise' to _fit_and_score methods in cross_validate and cross_val_score?

ccatalfo pushed a commit to ccatalfo/scikit-learn that referenced this pull request Mar 5, 2018
@Zhdanovich

Copy link
Copy Markdown
Contributor Author

@jnothman Hello, can you check my previous comment, please?

@jnothman

jnothman commented Mar 14, 2018 via email

Copy link
Copy Markdown
Member

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Change the default of *SearchCV's error_score to np.nan

3 participants