如何解决如何使用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)
看起来您已经有了混淆矩阵,一种选择是使用seaborn:
import seaborn as sns
sns.heatmap(confusion_matrix(y,clf.predict(X)),annot=True)
使用数据集:
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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。