Python ValueError:数据不是二进制的,并且未指定pos_标签

Python ValueError:数据不是二进制的,并且未指定pos_标签,python,scikit-learn,roc,Python,Scikit Learn,Roc,我试图计算roc\u auc\u得分,但我得到以下错误 "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.a

我试图计算roc\u auc\u得分,但我得到以下错误

"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)
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")

请告诉我它出了什么问题。

您只需更改
y\u true
,它看起来是这样的:

y_true=np.array([0, 1, 0, 0, 1, 1, 1, 1, 1])
说明: 如果您查看
roc\u auc\u score
函数在中的作用,您将看到
y\u true
的评估如下:

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)
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\u标签
None
,但只要您将
y\u true
定义为字符数组,则
np。所有的
总是
false
,当所有的
都被否定时,if条件为
true
,并引发异常。

我们在这方面有问题
y\u true=np.array(['0','1','0','0','1','1','1','1'])
将y_true的值转换为布尔值

y_true= '1' <= y_true
print(y_true) # [False  True False False  True  True  True  True  True]

y_true='1'似乎文件很久以前就被删除了,并且在当前版本中不再工作,我已将链接更新到旧版本