[MRG] Bugfix for precision_recall_curve when all labels are negative #14621
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Reference Issue
Fixes #8245
What does this implement/fix? Explain your changes.
When all the
y_true
labels are negative,precision_recall_curve
returnsnan
because ofrecall
being set tonan
instead of1
. This is because of the direction division of thetps
vector bytps[-1]
which happens to be0
.This fix checks if
tps[-1]
is0
and if yes, sets the recall to1
directly since there are no True Positives or False Negatives, else we calculaterecall
as normal.Any other comments?
I had to update
test_precision_recall_curve_toydata
since this test was expecting theTrueDivide
exception to be raised which is no longer the case as a result of this fix. I added 2 test cases, one to check when all truth labels are negative and the other to check when all truth labels are positive to ensure precision calculation is accurate.