【问题标题】:ValueError: Data is not binary and pos_label is not specifiedValueError:数据不是二进制且未指定 pos_label
【发布时间】:2013-08-26 09:41:19
【问题描述】:

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

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

我的代码sn-p如下:

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)

请告诉我它有什么问题。

【问题讨论】:

    标签: python scikit-learn roc


    【解决方案1】:

    你只需要更改y_true所以它看起来像这样:

    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 的评估如下:

    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_label 是None,但只要您将y_true 定义为一个字符数组,np.all 始终是false,因为它们都被否定了,那么如果条件为 true 并引发异常。

    【讨论】:

    • 文件好像很久以前就被删除了,在当前版本中不再工作,我已经更新了旧版本的链接
    【解决方案2】:

    我们有问题 y_true=np.array(['0', '1', '0', '0', '1', '1', '1', '1', '1']) 将 y_true 的值转换为布尔值

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

    【讨论】:

      猜你喜欢
      • 2019-09-21
      • 2016-06-27
      • 2021-09-24
      • 2020-05-22
      • 2021-04-16
      • 1970-01-01
      • 2023-04-06
      • 2011-09-12
      • 1970-01-01
      相关资源
      最近更新 更多