Sklearn的roc_auc_score用于多标记二进制分类

时间:2018-02-22 00:32:37

标签: python scikit-learn

与MWE一起回复:

我正在尝试计算roc_auc_score

这是我得到的错误:

Traceback (most recent call last):
  File "Feb22so.py", line 58, in <module>
    test_roc_vals(od)   
  File "Feb22so.py", line 29, in test_roc_vals
    roc_values.append(roc_auc_score(target, pred))
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/metrics/ranking.py", line 260, in roc_auc_score
    sample_weight=sample_weight)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/metrics/base.py", line 127, in _average_binary_score
    sample_weight=score_weight)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/metrics/ranking.py", line 251, in _binary_roc_auc_score
    raise ValueError("Only one class present in y_true. ROC AUC score "
ValueError: Only one class present in y_true. ROC AUC score is not defined in that case.

这是我的代码的MWE版本。

from scipy.sparse import csr_matrix
import numpy as np
from collections import OrderedDict
from sklearn.metrics import roc_auc_score

def test_roc_vals(od):
        #od will be an OrderedDict with integer keys and scipy.sparse.csr_matrix OR list values
        #if the value is a list, it will be empty.
        #a scipy.sparse.csr_matrix may have only 0s or only 1s
        roc_values = []
        for i in range(len(od.keys())-1):
                print "i is: ", i,
                target = od[od.keys()[i+1]]
                pred = od[od.keys()[i]]

                if isinstance(target, list) or isinstance(pred, list):
                        print 'one of them is a list: cannot compute roc_auc_score'
                        continue
                else:   
                        target = target.toarray()
                        pred = pred.toarray()
                        if len(np.unique(target)) != 2 or len(np.unique(pred)) !=2:
                                print 'either target or pred or both contain only one class: cannot compute roc_auc_score'
                                continue
                        else:   
                                roc_values.append(roc_auc_score(target, pred))
        return 0

if __name__ == '__main__':

        #Generate some fake data
        #This makes an OrderedDict of 20 scipy.sparse.csr_matrix objects, with 10 rows and 10 columns and binary values
        od = OrderedDict()
        for i in range(20):
                row = np.random.randint(10, size=10)
                col = np.random.randint(10, size=10)
                data = np.random.randint(2, size=10)
                sp_matrix = csr_matrix((data, (row, col)), shape=(10, 10))
                od[i] = sp_matrix

        #Now let's include some empty lists at the end of the Ordered Dict.

        for j in range(20, 23):
                od[j] = []

        #Calling the roc_auc_score function on all non-list values that have at least one instance of each 0/1 class
        test_roc_vals(od)

我无法弄清楚为什么我的if / else没有捕获'只有一个类'的实例。或许它是,并且错误是由其他东西引起的?

OLD:

我找不到这个in the docs。在sklearn中,roc_auc_score每个类的实例数是否最少?

我在计算它时遇到了麻烦,即使我在代表性不足的班级中有10个例子。

1 个答案:

答案 0 :(得分:0)

您的y_true集应具有多种标签。例如,应为y_true = [1,1,0,0]而不是y_true =[1,1,1,1]y_true =[0,0,0,0]