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 类)。对于class 2,y_true数组等于[B, B, B],所以只有一个class,无法计算ROC AUC分数。