sklearn的classification_report详解
precision、recall 、f1-score这三个基本就不介绍了,主要介绍平均的一些指标micro avg、macro avg、weighted avg、samples avg、accuracy
accuracy:正确率,分类正确样本数/总样本数
macro avg:用每一个类别对应的precision、recall、f1-score直接平均
weighted avg:用每一类别个数的权重乘对应类别指标
from sklearn.metrics import classification_report
print(classification_report([3,3,3,1], [1,3,1,2], target_names=['a', 'b', 'c']))
precision recall f1-score support
a 0.00 0.00 0.00 1
b 0.00 0.00 0.00 0
c 1.00 0.33 0.50 3
accuracy 0.25 4
macro avg 0.33 0.11 0.17 4
weighted avg 0.75 0.25 0.38 4
多分类中增加了两个指标micro avg、samples avg是针对样本计算的,其他指标是针对标签计算的
micro avg:针对样本,对每一个样本所有类别的TP加起来除以所有类别(TP+FP)
macro avg: 用每一个类别对应的precision、recall、f1-score直接平均
weighted avg:用每一类别个数的权重乘对应类别指标
samples avg:针对样本,首先对每一个样本计算precision、recall指标然后对样本进行平均
from sklearn.metrics import classification_report
y_true = np.array([[1, 0, 1, 0, 0],
[0, 1, 0, 1, 1],
[1, 1, 1, 0, 1]])
y_pred = np.array([[1, 0, 0, 0, 1],
[0, 1, 1, 1, 0],
[1, 1, 1, 0, 0]])
print(classification_report(y_true, y_pred))
precision recall f1-score support precision计算方式(自加)
0 1.00 1.00 1.00 2 2/2
1 1.00 1.00 1.00 2 2/2
2 0.50 0.50 0.50 2 1/2
3 1.00 1.00 1.00 1 1/1
4 0.00 0.00 0.00 2 0/1
micro avg 0.75 0.67 0.71 9 (1+2+3)/(2+3+3)=0.75
macro avg 0.70 0.70 0.70 9 (1+1+0.5+1+0)/5=0.7
weighted avg 0.67 0.67 0.67 9 (2/9)*1+(2/9)*1+(2/9)*0.5+(1/9)*1+(2/9)*0=0.67
samples avg 0.72 0.64 0.67 9 (1/2+2/3+3/3)/3=0.72
表头 | precision | recall | f1-score | support | precision计算方式(自加) |
---|---|---|---|---|---|
0 | 1.000 | 1.000 | 1.000 | 2 | 2/2 |
1 | 1.000 | 1.000 | 1.000 | 2 | 2/2 |
2 | 0.500 | 0.500 | 0.500 | 2 | 1/2 |
3 | 1.000 | 1.000 | 1.000 | 1 | 1/1 |
4 | 0.000 | 0.000 | 0.000 | 2 | 0/1 |
micro avg | 0.75 | 0.67 | 0.71 | 9 | (1+2+3)/(2+3+3) |
macro avg | 0.70 | 0.70 | 0.70 | 9 | (1+1+0.5+1+0)/5 |
weighted avg | 0.67 | 0.67 | 0.67 | 9 | (2/9)*1+(2/9)*1+(2/9)*0.5+(1/9)*1+(2/9)*0 |
samples avg | 0.72 | 0.64 | 0.67 | 9 | (1/2+2/3+3/3)/3 |