ValueError:数据不是二进制且未指定pos_label


问题内容

我正在尝试计算roc_auc_score,但是出现以下错误。

"ValueError: Data is not binary and pos_label is not specified"

我的代码段如下:

import numpy as np
from sklearn.metrics import roc_auc_score
y_scores=np.array([ 0.63, 0.53, 0.36, 0.02, 0.70 ,1 , 0.48, 0.46, 0.57])
y_true=np.array(['0', '1', '0', '0', '1', '1', '1', '1', '1'])
roc_auc_score(y_true, y_scores)

请告诉我这是怎么回事。


问题答案:

您只需要进行更改即可y_true,如下所示:

y_true=np.array([0, 1, 0, 0, 1, 1, 1, 1, 1])

说明: 如果查看一下https://github.com/scikit-learn/scikit-
learn/blob/0.15.X/sklearn/metrics/metrics.py中的
roc_auc_score功能,您将看到其评估如下:y_true

classes = np.unique(y_true)
if (pos_label is None and not (np.all(classes == [0, 1]) or
 np.all(classes == [-1, 1]) or
 np.all(classes == [0]) or
 np.all(classes == [-1]) or
 np.all(classes == [1]))):
    raise ValueError("Data is not binary and pos_label is not specified")

在执行的时刻pos_labelNone,但是只要您将y_true字符定义为一个字符数组,并且np.all它们总是false被否定,并且所有条件都被否定,则if条件为,true并且引发异常。