为什么我用不同的方法计算宏平均值得到不同的精度、召回率和 f1 分数

Why do i get different precision, recall and f1 score for different methods of calculating the macro avearage

我使用两种方法计算了分类的 P、R 和 F1 的宏观平均值。方法一是

print("Macro-Average Precision:", metrics.precision_score(predictions, test_y, average='macro'))
print("Macro-Average Recall:", metrics.recall_score(predictions, test_y, average='macro'))
print("Macro-Average F1:", metrics.f1_score(predictions, test_y, average='macro'))

给出了这个结果:

Macro-Average Precision: 0.6822
Macro-Average Recall: 0.7750
Macro-Average F1: 0.7094

方法二是:

print(classification_report(y_true, y_pred))

给出了这个结果:

precision    recall  f1-score   support
       0       0.55      0.25      0.34       356
       1       0.92      0.96      0.94      4793
       2       0.85      0.83      0.84      1047
accuracy                           0.90      6196

macro avg       0.78      0.68      0.71      6196
weighted avg       0.89      0.90      0.89      6196

我预计这两种方法的输出是相同的,因为它们是在同一时间同一时间生成的 运行。 有人可以解释为什么会这样吗,或者哪里有错误?

据我从 classification_report 结果中得知,您有多个 class。

如果您在指标模块中检查单个函数的 documentation,则默认参数将 class '1' 视为默认正值 class。

我认为可能发生的情况是,在您的第一次计算中,它是一个对所有计算(0 和 2 是负数 classes,1 是正数 class)。在第二种情况下,您实际上是在考虑真正的多 class 情况。