Skip to content

average_precision_score does not return correct AP when all negative ground truth labels #8245

Closed

Description

@varunagrawal

Description

average_precision_score does not return correct AP when y_true is all negative labels.

Steps/Code to Reproduce

One can run this piece of dummy code:

sklearn.metrics.ranking.average_precision_score(np.array([0, 0, 0, 0, 0]), np.array([0.1, 0.1, 0.1, 0.1, 0.1]))

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

Versions

Linux-4.4.0-59-generic-x86_64-with-Ubuntu-16.04-xenial
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609]
NumPy 1.12.0
SciPy 0.18.1
Scikit-Learn 0.18.1

Activity

  1. 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
  2. varunagrawal commented on Jan 30, 2017

    @varunagrawal
    ContributorAuthor

    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.

  3. amueller commented on Jan 30, 2017

    @amueller
    Member

    Can you check if #7356 fixes this?

  4. lesteve commented on Feb 1, 2017

    @lesteve
    Member

    Can you check if #7356 fixes this?

    No it doesn't. I think we just need to have to do something like this to handle this edge case:

    recall = 1 if tps[-1] == 0 else tps / tps[-1] 

    @varunagrawal if you do a PR please add a non-regression test with only zeros in y_true.

  5. varunagrawal commented on Feb 1, 2017

    @varunagrawal
    ContributorAuthor

    @lesteve can you please also specify what needs to be done for precision? Or should that be as is?

  6. lesteve commented on Feb 1, 2017

    @lesteve
    Member

    I believe the code works as it is. You can add a test with only 1s in y_true to make sure that precision is 1 in this case.

  7. varunagrawal commented on Feb 3, 2017

    @varunagrawal
    ContributorAuthor

    @lesteve hoping you can take a look at the PR.

  8. gvishal commented on Oct 8, 2017

    @gvishal

    Updates on this. Is this merged?

  9. varunagrawal commented on Oct 8, 2017

    @varunagrawal
    ContributorAuthor

    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.

  10. gvishal commented on Oct 9, 2017

    @gvishal

    The standard TREC Eval is able to compute AP and other metrics on the same data.

  11. kangkang59812 commented on May 29, 2019

    @kangkang59812

    so how did you solve it ?

  12. crypdick commented on Apr 28, 2020

    @crypdick
    Contributor

    What's the status on this?

  13. MarkCarbonell98 commented on Oct 14, 2020

    @MarkCarbonell98

    Same problem here with sklearn 0.23.2

  14. varunagrawal commented on Dec 31, 2020

    @varunagrawal
    ContributorAuthor

    Finally got the PR in. Sorry about the delay.

  15. Lorenz92 commented on Sep 10, 2021

    @Lorenz92

    Any update on this issue?

  16. mcever commented on Sep 20, 2021

    @mcever

    Just updated sklearn and this still appears to be an issue?

  17. varunagrawal commented on Sep 21, 2021

    @varunagrawal
    ContributorAuthor

    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.

  18. Lorenz92 commented on Sep 21, 2021

    @Lorenz92

    Hi @lesteve, are you planning to approve this PR? Please let us know. Thank you!

  19. mcever commented on Sep 21, 2021

    @mcever

    @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

       [0., 0., 0., 0., 0.],
       [0., 1., 1., 0., 0.],
       [0., 0., 1., 0., 0.],
       [1., 1., 0., 0., 0.],
    

    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.

  20. ekosman commented on Oct 24, 2021

    @ekosman

    Hey there. Is this issue going to be fixed?

  21. redsphinx commented on May 2, 2022

    @redsphinx

    Hi it's 2022 now and this is still an issue, any plans for fixing it?

    This issue persists for when y_true is all -1's or all 0's

  22. jeremiedbb commented on May 2, 2022

    @jeremiedbb
    Member

    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``.

  23. redsphinx commented on May 2, 2022

    @redsphinx

    pip install scikit-learn==1.1.0rc1

    Ok this works both with all -1 and 0, Thanks!

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions