Skip to content

Bug in AUC metric when TP = 100%? #1257

@seth-brown

Description

@seth-brown

As an example, this works correctly:

In [13]: import numpy as np                                                                                                                         

In [14]: from sklearn import metrics                                                                                                                

In [15]: true = [1, 1, 1, 1, 1, 1, 1, 1, 1, 0.99]                                                                                                   

In [16]: pred = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]                                                                                                      

In [17]: fpr, tpr, thresholds = metrics.roc_curve(true, pred)                                                                                       

In [18]: metrics.auc(fpr, tpr)                                                                                                                      
Out[18]: 0.22222222222222221

However, if there are no true negatives (e.g. there is only one class), an error is thrown:

In [19]: true = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

In [20]: pred = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]                                                                                                      

In [21]: fpr, tpr, thresholds = metrics.roc_curve(true, pred)                                                                                       
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-21-35631f51a7c5> in <module>()
----> 1 fpr, tpr, thresholds = metrics.roc_curve(true, pred)

    132     # ROC only for binary classification
    133     if classes.shape[0] != 2:
--> 134         raise ValueError("ROC is defined for binary classification only")
    135 
    136     y_score = np.ravel(y_score)

ValueError: ROC is defined for binary classification only

Is this the correct behavior?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions