Sklearn:plot_confusion_matrix,未经训练的分类器

问题描述 投票:0回答:2

我正在尝试使用plot_confusion_matrix,

from sklearn.metrics import confusion_matrix

y_true = [1, 1, 0, 1]
y_pred = [1, 1, 0, 0]

confusion_matrix(y_true, y_pred)

输出:

array([[1, 0],
       [1, 2]])

现在,使用以下内容时;使用“类”或不使用“类”

from sklearn.metrics import plot_confusion_matrix

plot_confusion_matrix(y_true, y_pred, classes=[0,1], title='Confusion matrix, without normalization')

plot_confusion_matrix(y_true, y_pred, title='Confusion matrix, without normalization')

除了里面的数字,我希望得到类似的输出,

enter image description here

绘制简单图,它不需要估计器。

使用mlxtend.plotting,

from mlxtend.plotting import plot_confusion_matrix
import matplotlib.pyplot as plt
import numpy as np

binary1 = np.array([[4, 1],
                   [1, 2]])

fig, ax = plot_confusion_matrix(conf_mat=binary1)
plt.show()

它提供相同的输出。

基于this

它需要分类器,

disp = plot_confusion_matrix(classifier, X_test, y_test,
                                 display_labels=class_names,
                                 cmap=plt.cm.Blues,
                                 normalize=normalize)

我可以在没有分类器的情况下进行绘制吗?

python scikit-learn confusion-matrix
2个回答
0
投票

plot_confusion_matrix需要训练有素的分类器。如果您查看source code,它所做的就是为您执行预测,以便生成y_pred

y_pred = estimator.predict(X)
    cm = confusion_matrix(y_true, y_pred, sample_weight=sample_weight,
                          labels=labels, normalize=normalize)

因此,为了在不指定classifier的情况下绘制混淆矩阵,您必须使用其他工具


0
投票

由于plot_confusion_matrix要求参数'estimator'不为None,答案是:不,您不能。但是您可以用其他方式绘制混淆矩阵,例如,参见以下答案:How can I plot a confusion matrix?

© www.soinside.com 2019 - 2024. All rights reserved.