多分类,计算混淆矩阵confusion_matrix,以及accuracy、precision、recall、f1-score分数。
1)使用sklearn计算并画出混淆矩阵(confusion_matrix);
2)使用sklearn计算accuracy(accuracy_score);
3)使用sklearn计算多分类的precision、recall、f1-score分数。以及计算每个类别的precision、recall、f1-score。
precision:precision_score https://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_score.html
recall:recall_score https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html
F1 score:f1_score https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html
默认precision_score、recall_score、f1_score函数只能求出二分类问题的精准率(average参数默认为binary),不过如果要想能够求出多分类问题的精准率可以选择合理average参数的值,比如:micro、macro等。
参数average有5个选项:{‘micro’微平均, ‘macro’宏平均, ‘samples’, ‘weighted’, ‘binary’},默认是default=’binary’二分类。
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score, precision_score, f1_score, recall_score, classification_report
# y_true为真实值,y_pred为预测值(此处y_true和y_pred仅作举例,随便取的值,有0~4共5个类别。)
y_true = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
y_pred = [1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 3, 3, 3, 0, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4]
# 1.计算混淆矩阵
cm = confusion_matrix(y_true, y_pred)
conf_matrix = pd.DataFrame(cm, index=['1','2','3','4','5'], columns=['1','2','3','4','5']) #数据有5个类别
# 画出混淆矩阵
fig, ax = plt.subplots(figsize=(4.5, 3.5))
sns.heatmap(conf_matrix, annot=True, annot_kws={"size": 14}, cmap="Blues")
plt.ylabel('True label', fontsize=14)
plt.xlabel('Predicted label', fontsize=14)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
plt.savefig('confusion.pdf', bbox_inches='tight')
plt.show()
# 2.计算accuracy
print('accuracy_score', accuracy_score(y_true, y_pred))
# 3.计算多分类的precision、recall、f1-score分数
print('Micro precision', precision_score(y_true, y_pred, average='micro'))
print('Micro recall', recall_score(y_true, y_pred, average='micro'))
print('Micro f1-score', f1_score(y_true, y_pred, average='micro'))
print('Macro precision', precision_score(y_true, y_pred, average='macro'))
print('Macro recall', recall_score(y_true, y_pred, average='macro'))
print('Macro f1-score', f1_score(y_true, y_pred, average='macro'))
# 下面这个可以显示出每个类别的precision、recall、f1-score。
print('classification_report\n',classification_report(y_true, y_pred))
参考:
https://blog.csdn.net/kan2281123066/article/details/103237273 利用sklearn 计算 precision、recall、F1 score
https://zhuanlan.zhihu.com/p/147663370 多分类模型Accuracy, Precision, Recall和F1-score的超级无敌深入探讨
https://cloud.tencent.com/developer/article/1632611 机器学习入门 10-8 多分类问题中的混淆矩阵