You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
average_precision_score does not return correct AP when all negative ground truth labels #8245
It returns nan instead the correct value with the error:
RuntimeWarning: invalid value encountered in true_divide
recall = tps / tps[-1]
Expected Results
As per this Stackoverflow answer, Recall = 1 when FN=0, since 100% of the TP were discovered and Precision = 1 when FP=0, since no there were no spurious results.
Actual Results
Current output is:
/usr/local/lib/python3.5/dist-packages/sklearn/metrics/ranking.py:415: RuntimeWarning: invalid value encountered in true_divide
recall = tps / tps[-1]
Out[201]: nan
Reacted by Naoto Usuyama, Shihab Shahriar Khan, Krishna Penukonda, Ricardo Decal, Jakub Beránek, ghbacct, R. Austin McEver, KinWaiCheuk, Adhokshaja Madhwaraj and Matt M. Mo
changed the title [-]average_precision_score does not return correct AP when `y_true` is all negative labels[/-][+]average_precision_score does not return correct AP when all negative ground truth labels[/+]on Jan 30, 2017
If someone can help me understand how to correctly do the False Positive calculation, I can submit a Pull Request.
For this bug, recall will be 1 since there are 0 True Positives and False Negatives. However, to calculate precision, I need to understand the correct way to find False Positives. The above sample results in _binary_clf_curve returning the same number of False Positives as the number of samples. Should I simply use that as the cue and say that is TPs=0 and FPs=len(y_true), then precision is 1?
I am not sure I quite understand how the threshold calculation is being performed here.
@varunagrawal thanks for submitting a fix :) can you link your update so that I can copy it to my own code base? Also, I think I'm finding that this issue also presents itself when a class has no examples. For example, obviously the following example y_true will trigger this bug
But I think it will still be present even removing the all 0 vector because the final column is all 0s. Do you also find this to be the case? If so, does your PR cover this case? If not, perhaps there's something wrong with my own code.
Hi, it's 2022 now and this is issue has been fixed (see #19085). I encourage you to test the release candidate for version 1.1.0: ```pip install scikit-learn==1.1.0rc1``.
Activity
[-]average_precision_score does not return correct AP when `y_true` is all negative labels[/-][+]average_precision_score does not return correct AP when all negative ground truth labels[/+]varunagrawal commentedon Jan 30, 2017
If someone can help me understand how to correctly do the False Positive calculation, I can submit a Pull Request.
For this bug, recall will be 1 since there are 0 True Positives and False Negatives. However, to calculate precision, I need to understand the correct way to find False Positives. The above sample results in
_binary_clf_curvereturning the same number of False Positives as the number of samples. Should I simply use that as the cue and say that is TPs=0 and FPs=len(y_true), then precision is 1?I am not sure I quite understand how the threshold calculation is being performed here.
amueller commentedon Jan 30, 2017
Can you check if #7356 fixes this?
lesteve commentedon Feb 1, 2017
No it doesn't. I think we just need to have to do something like this to handle this edge case:
@varunagrawal if you do a PR please add a non-regression test with only zeros in
y_true.varunagrawal commentedon Feb 1, 2017
@lesteve can you please also specify what needs to be done for precision? Or should that be as is?
lesteve commentedon Feb 1, 2017
I believe the code works as it is. You can add a test with only 1s in
y_trueto make sure that precision is 1 in this case.varunagrawal commentedon Feb 3, 2017
@lesteve hoping you can take a look at the PR.
gvishal commentedon Oct 8, 2017
Updates on this. Is this merged?
varunagrawal commentedon Oct 8, 2017
The current functionality of average_precision has changed. I'm planning to submit a new PR for that. Will close this when the other PR is ready.
gvishal commentedon Oct 9, 2017
The standard TREC Eval is able to compute AP and other metrics on the same data.
kangkang59812 commentedon May 29, 2019
so how did you solve it ?
crypdick commentedon Apr 28, 2020
What's the status on this?
MarkCarbonell98 commentedon Oct 14, 2020
Same problem here with sklearn 0.23.2
varunagrawal commentedon Dec 31, 2020
Finally got the PR in. Sorry about the delay.
Lorenz92 commentedon Sep 10, 2021
Any update on this issue?
mcever commentedon Sep 20, 2021
Just updated sklearn and this still appears to be an issue?
varunagrawal commentedon Sep 21, 2021
It's been 5 years now with this issue. I've opened the PR and updated it countless times but the only blocker is the approving review.
Lorenz92 commentedon Sep 21, 2021
Hi @lesteve, are you planning to approve this PR? Please let us know. Thank you!
mcever commentedon Sep 21, 2021
@varunagrawal thanks for submitting a fix :) can you link your update so that I can copy it to my own code base? Also, I think I'm finding that this issue also presents itself when a class has no examples. For example, obviously the following example y_true will trigger this bug
But I think it will still be present even removing the all 0 vector because the final column is all 0s. Do you also find this to be the case? If so, does your PR cover this case? If not, perhaps there's something wrong with my own code.
ekosman commentedon Oct 24, 2021
Hey there. Is this issue going to be fixed?
redsphinx commentedon May 2, 2022
Hi it's 2022 now and this is still an issue, any plans for fixing it?
This issue persists for when
y_trueis all -1's or all 0'sjeremiedbb commentedon May 2, 2022
Hi, it's 2022 now and this is issue has been fixed (see #19085). I encourage you to test the release candidate for version 1.1.0: ```pip install scikit-learn==1.1.0rc1``.
redsphinx commentedon May 2, 2022
Ok this works both with all -1 and 0, Thanks!