本文介绍了没有估计器的 plot_confusion_matrix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 plot_confusion_matrix,

from sklearn.metrics 导入混淆_矩阵y_true = [1, 1, 0, 1]y_pred = [1, 1, 0, 0]混淆矩阵(y_true,y_pred)

输出:

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

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

from sklearn.metrics import plot_confusion_matrixplot_confusion_matrix(y_true, y_pred, classes=[0,1], title='混淆矩阵,无归一化')

plot_confusion_matrix(y_true, y_pred, title='混淆矩阵,无归一化')

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

绘制简单的图表,应该不需要估算器.

使用 mlxtend.plotting,

from mlxtend.plotting import plot_confusion_matrix导入 matplotlib.pyplot 作为 plt将 numpy 导入为 npbinary1 = np.array([[4, 1],[1, 2]])图, ax = plot_confusion_matrix(conf_mat=binary1)plt.show()

它提供相同的输出.

基于

I'm trying to use 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)

Output:

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

Now, while using the followings; using 'classes' or without 'classes'

from sklearn.metrics import plot_confusion_matrix

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

or

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

I expect to get similar output like this except the numbers inside,

Plotting simple diagram, it should not require the estimator.

Using 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()

It provides same output.

Based on this

it requires a classifier,

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

Can I plot it without a classifier?

解决方案

plot_confusion_matrix expects a trained classifier. If you look at the source code, what it does is perform the prediction to generate y_pred for you:

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

So in order to plot the confusion matrix without specifying a classifier, you'll have to go with some other tool, or do it yourself.A simple option is to use seaborn:

import seaborn as sns

cm = confusion_matrix(y_true, y_pred)
f = sns.heatmap(cm, annot=True)

这篇关于没有估计器的 plot_confusion_matrix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 19:36