python: multi-classification-calculate confusion matrix confusion_matrix, precision, recall, f1-score score

1. Goal:

For multi-classification, calculate the confusion matrix confusion_matrix, as well as accuracy, precision, recall, and f1-score scores.

2. Code:

  • 1) Use sklearn to calculate and drawconfusion matrix (confusion_matrix);

  • 2)使用sklearn计算accuracy(accuracy_score)

  • 3) Use sklearn to calculate multi-classificationprecision, recall, f1-score scores. And calculate precision, recall, f1-score for each category.

  • By default, the precision_score, recall_score, and f1_score functions can only calculate the accuracy of the two-classification problem (the average parameter defaults to binary). However, if you want to be able to calculate the accuracy of the multi-classification problem, you can choose a reasonable average parameter value, such as: micro, macro etc.

  • The parameter average has 5 options: {‘micro’ micro average, ‘macro’ macro average, ‘samples’, ‘weighted’, ‘binary’}. The default is default=’binary’ two-category.

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))

3. Result:

reference:

https://blog.csdn.net/kan2281123066/article/details/103237273 利用sklearn 计算 precision、recall、F1 score

https://zhuanlan.zhihu.com/p/147663370 An in-depth discussion on the accuracy, precision, recall and F1-score of multi-classification models

https://cloud.tencent.com/developer/article/1632611 Introduction to Machine Learning 10-8 Confusion Matrix in Multi-Classification Problems

Guess you like

Origin blog.csdn.net/weixin_39450145/article/details/129644248