简体   繁体   English

错误绘制来自 Sklearn 的混淆矩阵

[英]Error Plotting Confusion matrix from Sklearn

I am trying to use the sklearn confusion matrix class to plot a confusion matrix.我正在尝试使用 sklearn 混淆矩阵 class 到 plot 混淆矩阵。

Here's the code I used:这是我使用的代码:

from sklearn.metrics import roc_curve, auc, plot_confusion_matrix

import matplotlib.pyplot as plt

disp = plot_confusion_matrix(self.g_cv.best_estimator_ , self.test_X, self.test_Y,
                                 cmap=plt.cm.Blues)

        plt.title('Confusion Matrix')
        plt.plot(disp)

This is based on this example on Sklearn website:这是基于 Sklearn 网站上的这个例子:

https://scikit-learn.org/stable/modules/generated/sklearn.metrics.plot_confusion_matrix.html https://scikit-learn.org/stable/modules/generated/sklearn.metrics.plot_confusion_matrix.html

However I get an error when this is called.但是,当它被调用时我得到一个错误。

TypeError: float() argument must be a string or a number, not 'ConfusionMatrixDisplay'

It does plot it but also throws this error.它会执行 plot 但也会引发此错误。 Is there something wrong in the return object which is being plotted?正在绘制的返回 object 是否有问题?

The error is raised when trying to plot the ConfusionMatrixDisplay-object that disp = plot_confusion_matrix(... returns. It is meant to create a plot right away.尝试 plot disp = plot_confusion_matrix(...返回的 ConfusionMatrixDisplay-object 时引发错误。它旨在立即创建 plot。

It is sufficient to use plt.show() , rather than plt.plot(disp) .使用plt.show()而不是plt.plot(disp)就足够了。 matplotlib.pyplot.plot() expects two arrays as input. matplotlib.pyplot.plot()需要两个 arrays 作为输入。

Either you can plot the confusion matrix right away with您可以立即使用 plot 混淆矩阵

disp = plot_confusion_matrix(self.g_cv.best_estimator_ , self.test_X, self.test_Y, cmap=plt.cm.Blues)
plt.show()

or you create it first and then display it或者你先创建它然后显示它

disp = ConfusionMatrixDisplay(self.g_cv.best_estimator_ , self.test_X, self.test_Y)
disp = disp.plot(cmap=plt.cm.Blues)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM