欢迎访问个人网络日志🌹🌹知行空间🌹🌹
1.classification_report
sklearn.metrics.classification_report输出分类预测结果的常用评估标准,输入是标签和类别的预测向量,包括精准度,召回率和F1 Score。
借用1个例子来说明一下计算过程。原例子见:
https://towardsdatascience.com/micro-macro-weighted-averages-of-f1-score-clearly-explained-b603420b292f
数据为:

from sklearn.metrics import classification_report
import numpy as np
names = ["Airplane", "Boat", "Car"]
names.sort()
class_dict = dict([[v, i] for i,v in enumerate(names)])
# {'Airplane': 0, 'Boat': 1, 'Car': 2}
tru = np.array([0, 2, 2, 2, 2, 0, 1, 2, 0, 2])
pre = np.array([0, 1, 2, 2, 1, 1, 1, 0, 0, 2])
report = classification_report(tru, pre)
print(report)
输出结果为:
precision recall f1-score support
0 0.67 0.67 0.67 3
1 0.25 1.00 0.40 1
2 1.00 0.50 0.67 6
accuracy 0.60 10
macro avg 0.64 0.72 0.58 10
weighted avg 0.82 0.60 0.64 10
2.计算过程
上述预测结果对应的混淆矩阵为:

每个类别对应的TP(True Positive)\FP(False Negative)\FN(False Negative)为:

计算每个类别的Precision, Recall和F1 Score:

上述的结果对每个类别单个输出,如果要输出分类效果的整体指标,最好的办法就是对每个类别做平均。Macro Average,Weighted Average,Micro Average对应不同的平均方法。
3.Macro Average
Macro Average是直接对各指标求和求平均。
m a c r o _ a v g _ p r e c i s i o n = 0.67 + 0.25 + 1 3 = 0.64 macro\_avg\_precision = \frac{0.67 + 0.25 + 1}{3} = 0.64 macro_avg_pre

本文详细介绍了在分类任务中评估模型性能的classification_report,包括MacroAverage、WeightedAverage和MicroAverage的计算过程。通过示例展示了它们如何处理不平衡数据,并解释了在不同场景下选择哪种平均方法的重要性。
最低0.47元/天 解锁文章
2711

被折叠的 条评论
为什么被折叠?



