首页 > 解决方案 > 使用 Python 绘制已经计算好的混淆矩阵

问题描述

对于已经给定的混淆矩阵值,我如何在 Python 中绘制一个类似于此处显示的混淆矩阵?

在代码中,他们使用sklearn.metrics.plot_confusion_matrix基于基本事实和预测计算混淆矩阵的方法。

但就我而言,我已经计算了我的混淆矩阵。例如,我的混淆矩阵是(百分比值):

[[0.612, 0.388]
 [0.228, 0.772]]

标签: pythonmatplotlibconfusion-matrix

解决方案


如果您检查 的来源sklearn.metrics.plot_confusion_matrix您可以看到如何处理数据以创建绘图。然后您可以重用构造函数ConfusionMatrixDisplay并绘制自己的混淆矩阵。

import matplotlib.pyplot as plt
from sklearn.metrics import ConfusionMatrixDisplay

cm = [0.612, 0.388, 0.228, 0.772] # your confusion matrix
ls = [0, 1] # your y labels
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=ls)
disp.plot(include_values=include_values, cmap=cmap, ax=ax, xticks_rotation=xticks_rotation)
plt.show()

推荐阅读