import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import confusion_matrix
def plot_confusion_matrix(confusion_mat):
plt.imshow(confusion_mat)
plt.title('Confusion Matrix')
plt.colorbar()
labels = ['a', 'b', 'c', 'd']
tick_marks = np.arange(len(labels))
plt.xticks(tick_marks, labels)
plt.yticks(tick_marks, labels)
plt.ylabel('True Label')
plt.xlabel('Predicted Label')
plt.show()
if __name__ == '__main__':
y_true = [1, 0, 0, 2, 1, 0, 3, 3, 3]
y_pred = [1, 1, 0, 2, 1, 0, 1, 3, 3]
confusion_mat = confusion_matrix(y_true, y_pred)
plot_confusion_matrix(confusion_mat)
对角线越亮越好
其中y_true: array, shape=[n_sample]
y_pred: array, shape=[n_sample]
confusion_matrix的返回值: array, shaple=[n_classes, n_classes]
混淆矩阵是总结分类模型预测结果的清醒分析表,属于机器学习的范畴,具体以矩阵形式将数据集中的记录按照真实的类别与分类模型作出的分类判断两个标准进行汇总。名字来源于他可以容易的表明多个类别是否有混淆(也就是一个class被预测成另一个class)
结果显示的意义:
A | B | C | D | |
A | 实际A - 预测A | 实际A - 预测B | 实际A - 预测C | 实际A - 预测D |
B | 实际B - 预测A | 实际B - 预测B | 实际B - 预测C | 实际B - 预测D |
C | 实际C - 预测A | 实际C - 预测B | 实际C - 预测C | 实际C - 预测D |
D | 实际D - 预测A | 实际D - 预测B | 实际D - 预测C | 实际D - 预测D |