我正在尝试计算以下内容:
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的列表的列表,所以我认为它应该可以工作。
我们将不胜感激!
答案 0 :(得分:1)
请确保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')
由于gt1中不存在整数/标签2,因此会引发错误。换句话说, gt1 (3)中的类数不等于pr1 (4)中的列数。
答案 1 :(得分:1)
roc_auc_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_true 中只有一个类,因为 roc_auc_score 函数遍历每个类(标识为类 A)并将它们与其他类进行比较(确定为 B 类)。对于第 2 类,y_true 数组等于 [B, B, B],因此只有一个类,无法计算 ROC AUC 分数。