如何使用python绘制混淆矩阵

问题描述

我正在尝试绘制混淆矩阵,但出现错误
TypeError: plot_confusion_matrix() got an unexpected keyword argument 'conf_mat'
这是我尝试过的

cm_des_tree = [[5,0],[3,20,1,[0,4,3,31,2,41]]
target_names = ['Q1','E1','Q2','E2','P']

fig,ax = plot_confusion_matrix(conf_mat=cm_des_tree,colorbar=True,show_absolute=False,show_normed=True,class_names=target_names)
plt.show()

解决方法

如果使用plot_confusion_matrix,则向其传递estimator,然后传递自变量,然后传递标签,从2修改示例:

import matplotlib.pyplot as plt  
from sklearn.datasets import make_classification
from sklearn.metrics import plot_confusion_matrix
from sklearn.svm import SVC

X,y = make_classification(random_state=0)
clf = SVC(random_state=0)
clf.fit(X,y)
plot_confusion_matrix(clf,X,y) 

vignette

看起来您已经有了混淆矩阵,一种选择是使用seaborn:

import seaborn as sns
sns.heatmap(confusion_matrix(y,clf.predict(X)),annot=True)

enter image description here

使用数据集:

cm_des_tree = [[5,0],[3,20,1,[0,4,3,31,2,41]]
target_names = ['Q1','E1','Q2','E2','P']
sns.heatmap(cm_des_tree,annot=True,xticklabels=target_names,yticklabels=target_names)

enter image description here