在Precision、Recall、F1-score、Micro-F1、Macro-F1、Recall K文章中介绍了一些分类指标的理论 计算方式。在本文中将介绍使用sklearn.metrics库来计算这些指标
1. accuracysklearn.metrics.accuracy_score(y_true, y_pred, normalize True, sample_weight None)
y_true: 真实标签
y_pred: 预测标签
normalize: 若为True结果返回准确率 若为False结果返回分类正确的样本个数
注意 不支持多标签
import numpy as np from sklearn.metrics import accuracy_score y_pred [0, 2, 1, 3] y_true [0, 1, 2, 3] rate accuracy_score(y_true, y_pred) num accuracy_score(y_true, y_pred, normalize False) print( rate: , rate) print( num: , num)
输出
rate: 0.5 num: 22. 构建文本报告展示主要分类指标
sklearn.metrics.classification_report(y_true, y_pred, labels None, target_names None, sample_weight None, digits 2)
labels: 标签的索引
target_names: 标签的名字 与labels对应
digits: 小数点位数
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
注意 当labels为None时 target_name中的名字默认将labels视为label_index从小到大排序 并与之对应。当指定labels参数时 target_names中名字与labels中索引对应。将上面代码最后一行改为
print(classification_report(y_true, y_pred, labels [0, 2, 1], target_names target_names))
输出
precision recall f1-score support class 0 0.50 1.00 0.67 1 class 1 1.00 0.67 0.80 3 class 2 0.00 0.00 0.00 1 accuracy 0.60 5 macro avg 0.50 0.56 0.49 5 weighted avg 0.70 0.60 0.61 5
可以看到class1和class2的输出与上面的例子换了下
3. F1-scoresklearn.metrics.f1_score(y_true, y_pred, labels None, pos_label 1, average binary , sample_weight None)
labels: 标签的索引
pos_label: 作用不大
average: 可选[None, ‘binary’ (default), ‘micro’, ‘macro’, ‘samples’, ‘weighted’]之一 如果指定binary结果只返回二分类中的正样本的分数 指定其他分别计算micro-f1 macro-f1 如果是None返回每一个类别的f1-score 是一个列表 若是weighted 先计算每个类别的f1 然后根据每个类别样本数目加权平均
from sklearn.metrics import f1_score, confusion_matrix y_true [0, 1, 2, 0, 1, 2] y_pred [0, 2, 1, 0, 0, 1] matrix confusion_matrix(y_true, y_pred, labels [0, 1, 2]) macro_f1 f1_score(y_true, y_pred, average macro ) micro_f1 f1_score(y_true, y_pred, average micro ) weight_f1 f1_score(y_true, y_pred, average weighted ) all_f1 f1_score(y_true, y_pred, average None) print( matrix: , matrix) print( macro: , macro_f1) print( micro: , micro_f1) print( weight: , weight_f1) print( all: , all_f1)
matrix: [[2 0 0] [1 0 1] [0 2 0]] macro: 0.26666666666666666 micro: 0.3333333333333333 weight: 0.26666666666666666 all: [0.8 0. 0. ]4.



