为什么 roc_auc 在 sklearn 中产生奇怪的结果?

Why roc_auc produces weird results in sklearn?

我有一个二进制分类问题,我使用以下代码来获取我的 weighted avarege precisionweighted avarege recallweighted avarege f-measureroc_auc

df = pd.read_csv(input_path+input_file)

X = df[features]
y = df[["gold_standard"]]

clf = RandomForestClassifier(random_state = 42, class_weight="balanced")
k_fold = StratifiedKFold(n_splits=10, shuffle=True, random_state=0)
scores = cross_validate(clf, X, y, cv=k_fold, scoring = ('accuracy', 'precision_weighted', 'recall_weighted', 'f1_weighted', 'roc_auc'))

print("accuracy")
print(np.mean(scores['test_accuracy'].tolist()))
print("precision_weighted")
print(np.mean(scores['test_precision_weighted'].tolist()))
print("recall_weighted")
print(np.mean(scores['test_recall_weighted'].tolist()))
print("f1_weighted")
print(np.mean(scores['test_f1_weighted'].tolist()))
print("roc_auc")
print(np.mean(scores['test_roc_auc'].tolist()))
 

对于具有 2 个不同特征设置的同一数据集,我得到了以下结果。

Feature setting 1 ('accuracy', 'precision_weighted', 'recall_weighted', 'f1_weighted', 'roc_auc'):  
0.6920, 0.6888, 0.6920, 0.6752, 0.7120

Feature setting 2 ('accuracy', 'precision_weighted', 'recall_weighted', 'f1_weighted', 'roc_auc'):
0.6806  0.6754  0.6806  0.6643  0.7233

因此,我们可以看到在 feature setting 1 中,与 [=18] 相比,'accuracy'、'precision_weighted'、'recall_weighted'、'f1_weighted' 得到了很好的结果=].

但是,'roc_auc' feature setting 2feature setting 1 好。我发现这很奇怪,因为 feature setting 1.

的所有其他指标都更好

一方面,我怀疑发生这种情况是因为我使用 weighted 得分 precision, recall and f-measure 而不是 roc_auc。是否可以在 sklearn 中对二进制分类进行 weighted roc_auc

这个奇怪的 roc_auc 结果的真正问题是什么?

这并不奇怪,因为将所有这些其他指标与 AUC 进行比较就像将苹果与橙子进行比较。

下面是整个过程的高级描述:

  • 概率 class 生成器(如此处的 RF)在 [0, 1] 中产生概率输出 p
  • 为了获得硬 class 预测 (0/1),我们对这些概率应用 阈值 ;如果未明确设置(如此处),则此阈值隐含为 0.5,即如果 p>0.5class=1,否则 class=0.
  • 准确度、精确度、召回率和 f1 分数等指标是根据硬 class 预测 0/1 计算的,即 阈值之后应用。
  • 相比之下,AUC 衡量二元 class 运算符在所有可能阈值 范围内 的平均性能,而不是针对特定阈值。

所以,它肯定会发生,而且确实会导致新从业者的困惑。

我在 中回答的第二部分可能有助于了解更多详细信息。引用:

According to my experience at least, most ML practitioners think that the AUC score measures something different from what it actually does: the common (and unfortunate) use is just like any other the-higher-the-better metric, like accuracy, which may naturally lead to puzzles like the one you express yourself.