该函返回一个分类指标的结果,包括样本的precision、recall、accuracy、f1-score等
from sklearn.metrics import classification_report
sklearn.metrics.classification_report(y_true, y_pred, *, labels=None, target_names=None, sample_weight=None, digits=2, output_dict=False, zero_division='warn')
官方score
report: str or dict
返回的内容包括precision、recall、f1-score、accuracy(后文根据距离例子介绍)、averages包括macro average、weight average、Micro average。
macro avarage: 每个标签的无权重均值;
weighted average: 根据每个标签样本权重计算的均值
Micro average: 所有TP、FN、FP的均值,只在多标签或者含子类的多分类问题中
from sklearn.metrics import classification_report
y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]
target_names = ['class 0', 'class 1', 'class 2']
print(classification_report(y_true, y_pred, target_names=target_names))
precision recall f1-score support
class 0 0.50 1.00 0.67 1
class 1 0.00 0.00 0.00 1
class 2 1.00 0.67 0.80 3
accuracy 0.60 5
macro avg 0.50 0.56 0.49 5
weighted avg 0.70 0.60 0.61 5
y_pred = [1, 1, 0]
y_true = [1, 1, 1]
print(classification_report(y_true, y_pred, labels=[1, 2, 3]))
precision recall f1-score support
1 1.00 0.67 0.80 3
2 0.00 0.00 0.00 0
3 0.00 0.00 0.00 0
micro avg 1.00 0.67 0.80 3
macro avg 0.33 0.22 0.27 3
weighted avg 1.00 0.67 0.80 3
首先对于y_true和y_pred画出混淆矩阵便于观察:
对于上图依次计算return中各个指标:
p r e c i s i o n = T P T P + F P precision = \frac{TP}{TP+FP} precision=TP+FPTP
r e c a l l = T P T P + F N recall = \frac{TP}{TP+FN} recall=TP+FNTP
a c c u r a c y = r i g h t a l l accuracy = \frac{right}{all} accuracy=allright
f 1 − s c o r e = 2 1 p r e c i s i o n + 1 r e c a l l f1-score = \frac{2}{ \frac{1}{precision}+ \frac{1}{recall}} f1−score=precision1+recall12
从class0开始分析:
TP,就是实际为正样本,预测也为正样本的,为混淆矩阵的(0,0)处的值:1
FP,就是实际为负样本,被预测为正样本的,为混淆矩阵的(1,0)和(1,2)处的值:1+0=1
FN,就是实际为正样本,被预测为负样本的,为混淆矩阵的(0,1)和(0,2)处的值:0+0=0
right,就是正样本被预测为正样本,负样本被预测为负样本,为(0,0)、(1,1)、(2,2)出处的值:1+0+2=3
all,就是总体样本数:5
因此class0对应的指标依次为:
p r e c i s i o n = 1 1 + 1 = 0.5 precision = \frac{1}{1+1} = 0.5 precision=1+11=0.5
r e c a l l = 1 1 + 0 = 1 recall = \frac{1}{1+0} = 1 recall=1+01=1
a c c u r a c y = 3 5 = 0.6 accuracy = \frac{3}{5} = 0.6 accuracy=53=0.6
f 1 − s c o r e = 2 1 0.5 + 1 1 = 0.67 f1-score = \frac{2}{ \frac{1}{0.5}+ \frac{1}{1}}=0.67 f1−score=0.51+112=0.67
class1&class2相关指标计算同上,注意,对于class0他自身的标签可以看作为正例,它自身之外的所有标签都是反例。
最后,macro average为对应垂直方向上的指标的非加权均值,如precision的macro average为(0.5+0+1)/3=0.5;weight average为对应的指标与所占总体样本的权重的乘积,如precision的加权average为[(1/5)x0.5+(1/5)x0+(3/5)x1]=0.7