ValueError: 数据不是二进制的并且未指定 pos_label [英] ValueError: Data is not binary and pos_label is not specified

查看:91
本文介绍了ValueError: 数据不是二进制的并且未指定 pos_label的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to calculate roc_auc_score, but I am getting following error.

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

请告诉我它有什么问题.

Please tell me what is wrong with it.

推荐答案

你只需要改变 y_true 看起来像这样:

You only need to change y_trueso it looks like this:

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

说明:如果您查看 roc_auc_score 函数在 https://github.com/scikit-learn/scikit-learn/blob/0.15.X/sklearn/metrics/metrics.py 你会看到 y_true 的计算方式如下:

Explanation: If you take a look to what roc_auc_score functions does in https://github.com/scikit-learn/scikit-learn/blob/0.15.X/sklearn/metrics/metrics.py you will see that y_true is evaluated as follows:

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 并引发异常.

At the moment of the execution pos_label is None, but as long as your are defining y_true as an array of characters the np.all are always false and as all of them are negated then the if condition is trueand the exception is raised.

这篇关于ValueError: 数据不是二进制的并且未指定 pos_label的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆