y_检验和y_得分之间的roc_auc_得分不匹配

2024-04-27 00:50:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图计算以下各项:

auc = roc_auc_score(gt, pr, multi_class="ovr")

其中gt是一个大小为3470208的列表,包含0到41之间的值(全部int),而pr是一个大小为3470208的列表,每个列表的大小为42,每个位置的概率总和为1

但是,我得到了以下错误:

ValueError: Number of classes in y_true not equal to the number of columns in 'y_score'

所以我有点不知所措,因为y_true (gt)中的类数是42,因为我有一个从0到41的整数列表

既然pr是一个大小为42的列表,那么我认为它应该可以工作

谢谢你的帮助


Tags: ofingttrue列表pr概率multi
2条回答

roc\u auc\u score方法有一个labels参数,可用于指定缺少的标签

不幸的是,这只适用于multi_class=“ovo”模式,而不适用于“ovr”模式

# without labels
gt1 = np.array([0,1,3])
pr1 = np.array(
    [[0.1, 0.7, 0.1, 0.1], 
     [0.3, 0.3, 0.2, 0.2], 
     [0.5, 0.1, 0.1, 0.3]]
)
roc_auc_score(gt1, pr1, multi_class='ovo')
> ValueError: Number of classes in y_true not equal to the number of columns in 'y_score'

# with labels and multi-class="ovo":
gt1 = np.array([0,1,3])
pr1 = np.array(
    [[0.1, 0.7, 0.1, 0.1], 
     [0.3, 0.3, 0.2, 0.2], 
     [0.5, 0.1, 0.1, 0.3]]
)
roc_auc_score(gt1, pr1, multi_class='ovo', labels=[0, 1, 2, 3])
> 0.5

# with labels and multi-class="ovr":
gt1 = np.array([0,1,3])
pr1 = np.array(
    [[0.1, 0.7, 0.1, 0.1], 
     [0.3, 0.3, 0.2, 0.2], 
     [0.5, 0.1, 0.1, 0.3]]
)
roc_auc_score(gt1, pr1, multi_class='ovr', labels=[0, 1, 2, 3])
> ValueError: Only one class present in y_true. ROC AUC score is not defined in that case.

在这种情况下,y\u true中只存在一个类,因为roc\u auc\u score函数迭代每个类(标识为类A),并将它们与其他类(标识为类B)进行比较。对于类别2,y_真数组等于[B,B,B],因此只有一个类别,无法计算ROC AUC分数

确保gt中存在0和41(包括)之间的所有整数

一个简单的例子:

import numpy as np
from sklearn.metrics import roc_auc_score

# results in error:
gt1 = np.array([0,1,3])
pr1 = np.array(
    [[0.1, 0.7, 0.1, 0.1], 
     [0.3, 0.3, 0.2, 0.2], 
     [0.5, 0.1, 0.1, 0.3]]
)
#roc_auc_score(gt1, pr1, multi_class='ovr')


# does not result in error:
gt2 = np.array([0,2,1,3])
pr2 = np.array(
    [[0.1, 0.7, 0.1, 0.1], 
     [0.3, 0.3, 0.2, 0.2], 
     [0.5, 0.1, 0.1, 0.3],
     [0.3, 0.3, 0.2, 0.2]] 
)
#roc_auc_score(gt2, pr2, multi_class='ovr')

因为整数/标签2在gt1中不存在,所以会抛出一个错误。换句话说,gt1(3)中的类数不等于pr1(4)中的列数

相关问题 更多 >