-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Fix RuntimeWarning: invalid value encountered in test_calibration.py #19421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix RuntimeWarning: invalid value encountered in test_calibration.py #19421
Conversation
The reason why np.divide(proba, denominator, where=denominator != 0.0) did not work is because of the next line in the code:
Since now you don't divide by 0, you don't get inf and the probas are not normalized. |
Co-authored-by: Jérémie du Boisberranger <34657725+jeremiedbb@users.noreply.github.com>
Interesting, I did not know about the If this is not already the case, we could probably add a specific test to "calibrate" such a badly behaving classifier that always predicts zeros in
|
Thanks, I overlooked the fact that this process is only for division by zero. |
@ogrisel I don't think we want to check that no warning is issued. We don't want to check that every line of code doesn't raise a warning. I think that the fact that the warning disappears with this PR is enough. |
Alright, but don't we want to explicitly check that we get uniform probabilities when the based classifier predicts only zeros. |
I didn't argue against that :) |
OK, now I'm going to add this test. |
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
Since it is difficult to control the behavior of the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the new test. Here are some suggestions to improve it a bit. Otherwise LGTM.
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM once @jeremiedbb's remaining comments have been addressed.
Co-authored-by: Jérémie du Boisberranger <34657725+jeremiedbb@users.noreply.github.com>
Co-authored-by: Jérémie du Boisberranger <34657725+jeremiedbb@users.noreply.github.com>
Co-authored-by: Jérémie du Boisberranger <34657725+jeremiedbb@users.noreply.github.com>
Co-authored-by: Jérémie du Boisberranger <34657725+jeremiedbb@users.noreply.github.com>
Co-authored-by: Jérémie du Boisberranger <34657725+jeremiedbb@users.noreply.github.com>
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm. Thanks @t-kusanagi
Thank you very much for the fix and the nice new test @t-kusanagi. |
ping @lucyleeow to keep you in the loop on your favorite meta-estimator :) |
Reference Issues/PRs
#19334
What does this implement/fix? Explain your changes.
The runtime error was cause by
_CalibratedClassifier.predict_proba
function.The error occurs when
np.sum(proba, axis=1)[:, np.newaxis]
has some zero elements.So, I use
np.divide
to avoid this error. Withoutout
parameter, we will get the warning fromassert_allclose(...)
intest_calibration_multiclass
.Note:
Things I've tried that don't work.
with np.errstate(divide='ignore')
pytest
, still get warnig.np.divide(proba, denominator, where=denominator != 0.0)
(withoutout
parameter)assert_allclose(...)
intest_calibration_multiclass
.proba[np.isnan(proba)] = 1. / n_classes
is not enough to this case. Some of values seems not to become NaN, so sum ofproba
cannnot be 1.0