Repository navigation
Division by zero in OneVsRest's predict_proba (see #31224); bugfix for test_ovo_decision_function - #31228
Conversation
When a classifier has zero confidence in a class, zero rows appear in the predicted probability matrix, resulting in undefined behavior during probability normalization. OneVsRestClassifier's predict_proba method attempts to divide by zero row sums. This fixes that behavior by setting all zero row sums to 1.
jeremiedbb
left a comment
There was a problem hiding this comment.
Thanks for the PR @Luis-Varona. Let's use numpy's divide function which allows to specify where to divide.
Please add a test in sklearn/tests/test_multiclass.py and an entry in the changelog (see https://github.com/scikit-learn/scikit-learn/blob/main/doc/whats_new/upcoming_changes/README.md).
|
Will do. 🙂 |
predict_proba (tackles, but keeps open, #31224)
Refactored the division-by-zero fix in OneVsRest's predict_proba to use np.divide. Documented the change in the changelog. Added test_ovr_single_label_predict_proba_zero_row in tests to validate the bug fix.
|
@jeremiedbb @MarcBresson I've made the requested changes. (The only thing I'm worried about is whether |
Removed test (test_ovr_multilabel_predict_proba_zero_row) accidentally left over from prototyping. Changed zero indexing logic in test_ovr_single_label_predict_proba_zero_row to be more streamlined.
|
There is one test failing in https://github.com/scikit-learn/scikit-learn/actions/runs/14620590689/job/41019410025?pr=31228
|
@MarcBresson this is not my test; I didn't touch it. Presumably if we want to fix this, it should be in another Issue/PR? |
|
@MarcBresson anything else to fix before merging? Do you really want to address the bug with |
Lowered the threshold of unique values in the iris dataset from >146 to >140 in the test_ovo_decision_function test of the test_multiclass suite. Previous CI tests ended up with 145 unique values, resulting in a failing 'assert 145 > 146' check.
|
@MarcBresson @jeremiedbb I fixed the |
predict_proba (tackles, but keeps open, #31224)predict_proba (see #31224); bugfix for test_ovo_decision_function
|
@MarcBresson @jeremiedbb, just wanted to follow up and see if there are any more changes you’d like me to make before merging 🙂 |
|
Hello @Luis-Varona , I'm not a maintainer, just a happy contributor :) Reviews from the scikit-learn team can be quite long, as they have a lot on their plate. But don't worry, it will come! |
Got it, thanks :) Would you still like to discuss the rest of #31224, though? |
|
@jeremiedbb Saw you added some commits; anything else you'd like me to do? 🙂 |
There was a problem hiding this comment.
I just simplified a bit the test. LGTM. Thanks @Luis-Varona and @MarcBresson !
When a classifier has zero confidence in a class, zero rows appear in the predicted probability matrix, resulting in undefined behavior during probability normalization. OneVsRestClassifier's predict_proba method attempts to divide by zero row sums:
This fixes that behavior by setting all zero row sums to 1:
See Issue #31224 for more details.
Reference Issues/PRs
What does this implement/fix? Explain your changes.
Any other comments?