use*_*903 4 machine-learning scikit-learn cross-validation
使用scikit-learn的评估指标评估二进制分类器的正确方法是什么?
给定y_test和y_pred作为黄金和预测标签,classification_report输出中的F1分数不应该与f1_score产生的分数相同吗?
这是我的方法:
print(classification_reprot(y_test, y_pred)
Run Code Online (Sandbox Code Playgroud)
给出下表:
precision recall f1-score support
0 0.49 0.18 0.26 204
1 0.83 0.96 0.89 877
avg / total 0.77 0.81 0.77 1081
Run Code Online (Sandbox Code Playgroud)
然而,
print(f1_score(y_test, y_pred)
Run Code Online (Sandbox Code Playgroud)
得出F1分数= 0.89
现在,根据上述输出,该模型F1的性能得分= 0.89还是0.77?
简而言之,对于您的情况,f1分数为0.89,加权平均f1分数为0.77。
看一下的文档字符串sklearn.metrics.f1_score
:
The F1 score can be interpreted as a weighted average of the precision and
recall, where an F1 score reaches its best value at 1 and worst score at 0.
The relative contribution of precision and recall to the F1 score are
equal. The formula for the F1 score is::
F1 = 2 * (precision * recall) / (precision + recall)
In the multi-class and multi-label case, this is the weighted average of
the F1 score of each class.
Run Code Online (Sandbox Code Playgroud)
关键是这里的最后一句话。如果要查找每个类的加权平均f1分数,则不应向该函数提供0/1二进制分类。因此,例如,您可以
f1_score(y_test + 1, y_pred + 1)
# 0.77
Run Code Online (Sandbox Code Playgroud)
如果类别标签不是0/1,则将其视为多类指标(您在乎所有精度/召回分数),而不是二进制指标(其中您仅在关注正样本时关注精度/召回)。我同意这可能有点令人惊讶,但通常将0/1类视为二进制分类的标记。
编辑:自Scikit-learn 0.16起,此处列出的某些行为已被弃用–特别是关于二进制和非二进制分类的令人困惑的隐式假设。有关详细信息,请参见此github线程。