python - sklearn 绘制带有标签的混淆矩阵

标签 python matplotlib scikit-learn

我想绘制一个混淆矩阵来可视化分类器的性能,但它只显示标签的数量,而不是标签本身:

from sklearn.metrics import confusion_matrix
import pylab as pl
y_test=['business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business']

pred=array(['health', 'business', 'business', 'business', 'business',
       'business', 'health', 'health', 'business', 'business', 'business',
       'business', 'business', 'business', 'business', 'business',
       'health', 'health', 'business', 'health'], 
      dtype='|S8')

cm = confusion_matrix(y_test, pred)
pl.matshow(cm)
pl.title('Confusion matrix of the classifier')
pl.colorbar()
pl.show()

如何将标签(健康、业务等)添加到混淆矩阵中?

最佳答案

更新:

在 scikit-learn 0.22 中,有一个新功能可以直接绘制混淆矩阵(但是,该功能在 1.0 中已弃用,将在 1.2 中删除)。

查看文档:sklearn.metrics.plot_confusion_matrix


旧答案:

我认为值得一提的是 seaborn.heatmap 的使用在这里。

import seaborn as sns
import matplotlib.pyplot as plt     

ax= plt.subplot()
sns.heatmap(cm, annot=True, fmt='g', ax=ax);  #annot=True to annotate cells, ftm='g' to disable scientific notation

# labels, title and ticks
ax.set_xlabel('Predicted labels');ax.set_ylabel('True labels'); 
ax.set_title('Confusion Matrix'); 
ax.xaxis.set_ticklabels(['business', 'health']); ax.yaxis.set_ticklabels(['health', 'business']);

enter image description here

关于python - sklearn 绘制带有标签的混淆矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19233771/

相关文章:

python - GridSearchCV 和 LogisticRegression 引发 ValueError : Can't handle mix of continuous and binary

python - 在 Python 中将一个热编码结果转换回单列

python - 从递归函数返回字典

python - 如何在 matplotlib 配置文件中定义次要刻度数

python - 使用朴素贝叶斯情感分析进行 PoS 实现

python - 如何将绘图分配给变量并将该变量用作 Python 函数中的返回值

matplotlib - 有什么方法可以从seaborn fiddle 图中得到迷你箱线图吗?

python-3.x - scikit learn 机器学习中时间序列的交叉验证

python - 替换发生在这里,因为完全扩展的 BINDIR 在配置中不可用

python - 使用 pandas 导入转换后的数据