使用没有分类器的 scikit-learn 绘制混淆矩阵 [英] Plot Confusion Matrix with scikit-learn without a Classifier
问题描述
我有一个使用 sklearn.metrics.confusion_matrix
创建的混淆矩阵.
I have a confusion matrix created with sklearn.metrics.confusion_matrix
.
现在,我想用 sklearn.metrics.plot_confusion_matrix
绘制它,但第一个参数是训练好的分类器,如 文档.问题是我没有分类器;结果是通过手动计算获得的.
Now, I would like to plot it with sklearn.metrics.plot_confusion_matrix
, but the first parameter is the trained classifier, as specified in the documentation. The problem is that I don't have a classifier; the results were obtained doing manual calculations.
是否仍然可以通过 scikit-learn 在一行中绘制混淆矩阵,还是必须使用 matplotlib 自己编写代码?
Is it still possible to plot the confusion matrix in one line via scikit-learn, or do I have to code it myself with matplotlib?
推荐答案
您可以直接导入 plot_confusion_matrix
的事实表明您安装了最新版本的 scikit-learn (0.22).所以你可以看看plot_confusion_matrix()
的源代码,看看它是如何使用estimator
的.
The fact that you can import plot_confusion_matrix
directly suggests that you have the latest version of scikit-learn (0.22) installed. So you can just look at the source code of plot_confusion_matrix()
to see how its using the estimator
.
来自 最新来源这里,估计器用于:
- 使用
confusion_matrix
计算混淆矩阵 - 获取标签(y 的唯一值,对应于混淆矩阵中的 0,1,2..)
所以如果你已经有了这两样东西,你只需要下面的部分:
So if you have those two things already, you just need the below part:
import matplotlib.pyplot as plt
from sklearn.metrics import ConfusionMatrixDisplay
disp = ConfusionMatrixDisplay(confusion_matrix=cm,
display_labels=display_labels)
# NOTE: Fill all variables here with default values of the plot_confusion_matrix
disp = disp.plot(include_values=include_values,
cmap=cmap, ax=ax, xticks_rotation=xticks_rotation)
plt.show()
请查看注释中的注释.
对于旧版本,您可以查看 matplotlib 部分的编码方式 这里
For older versions, you can look at how the matplotlib part is coded here
这篇关于使用没有分类器的 scikit-learn 绘制混淆矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!