相关疑难解决方法(0)

如何在python中使用字符串轴而不是整数来绘制混淆矩阵

我正在关注如何在Matplotlib中绘制混淆矩阵的前一个主题.脚本如下:

from numpy import *
import matplotlib.pyplot as plt
from pylab import *

conf_arr = [[33,2,0,0,0,0,0,0,0,1,3], [3,31,0,0,0,0,0,0,0,0,0], [0,4,41,0,0,0,0,0,0,0,1], [0,1,0,30,0,6,0,0,0,0,1], [0,0,0,0,38,10,0,0,0,0,0], [0,0,0,3,1,39,0,0,0,0,4], [0,2,2,0,4,1,31,0,0,0,2], [0,1,0,0,0,0,0,36,0,2,0], [0,0,0,0,0,0,1,5,37,5,1], [3,0,0,0,0,0,0,0,0,39,0], [0,0,0,0,0,0,0,0,0,0,38] ]

norm_conf = []
for i in conf_arr:
        a = 0
        tmp_arr = []
        a = sum(i,0)
        for j in i:
                tmp_arr.append(float(j)/float(a))
        norm_conf.append(tmp_arr)

plt.clf()
fig = plt.figure()
ax = fig.add_subplot(111)
res = ax.imshow(array(norm_conf), cmap=cm.jet, interpolation='nearest')


for i,j in ((x,y) for x in xrange(len(conf_arr))
            for y in xrange(len(conf_arr[0]))):
    ax.annotate(str(conf_arr[i][j]),xy=(i,j))

cb = fig.colorbar(res)
savefig("confusion_matrix.png", format="png")
Run Code Online (Sandbox Code Playgroud)

我想改变轴显示的字母串,表示(A,B,C,...),而不是整数(0,1,2,3,..10).怎么能这样做.谢谢. …

python matplotlib

28
推荐指数
4
解决办法
3万
查看次数

类数 4 与 target_names 的大小 6 不匹配。尝试指定 labels 参数

当我尝试制作 CNN 模型的混淆矩阵时,我遇到了一些问题。当我运行代码时,它返回一些错误,例如:

print(classification_report(np.argmax(y_test,axis=1), y_pred,target_names=target_names))

Traceback (most recent call last):

  File "<ipython-input-102-82d46efe536a>", line 1, in <module>
    print(classification_report(np.argmax(y_test,axis=1), y_pred,target_names=target_names))

  File "G:\anaconda_installation_file\lib\site-packages\sklearn\metrics\classification.py", line 1543, in classification_report
    "parameter".format(len(labels), len(target_names))

ValueError: Number of classes, 4, does not match size of target_names, 6. Try specifying the labels parameter
Run Code Online (Sandbox Code Playgroud)

我已经搜索过解决这个问题的方法,但仍然没有得到完美的解决方案。我在这个领域完全陌生,有人可以帮助我吗?谢谢。

from sklearn.metrics import classification_report,confusion_matrix
import itertools

Y_pred = model.predict(X_test)
print(Y_pred)
y_pred = np.argmax(Y_pred, axis=1)
print(y_pred)

target_names = ['class 0(cardboard)', 'class 1(glass)', 'class 2(metal)','class 3(paper)', 'class 4(plastic)','class 5(trash)']

print(classification_report(np.argmax(y_test,axis=1), y_pred,target_names=target_names))
Run Code Online (Sandbox Code Playgroud)

python machine-learning confusion-matrix scikit-learn

8
推荐指数
1
解决办法
2万
查看次数

没有估计器的 plot_confusion_matrix

我正在尝试使用 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)
Run Code Online (Sandbox Code Playgroud)

输出:

array([[1, 0],
       [1, 2]])
Run Code Online (Sandbox Code Playgroud)

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

from sklearn.metrics import plot_confusion_matrix

plot_confusion_matrix(y_true, y_pred, classes=[0,1], title='Confusion matrix, without normalization')
Run Code Online (Sandbox Code Playgroud)

或者

plot_confusion_matrix(y_true, y_pred, title='Confusion matrix, without normalization')
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明

绘制简单的图表,应该不需要估计器。

使用 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()
Run Code Online (Sandbox Code Playgroud)

它提供相同的输出。

基于

它需要一个分类器,

disp = plot_confusion_matrix(classifier, X_test, y_test, …
Run Code Online (Sandbox Code Playgroud)

python confusion-matrix scikit-learn

7
推荐指数
2
解决办法
3605
查看次数

如何用 Keras 制作混淆矩阵?

我已经使用 keras 训练了 CNN 模型(多类分类),现在我想在我的测试图像集上评估该模型。有没有办法创建混淆矩阵?

python machine-learning confusion-matrix keras tensorflow

1
推荐指数
1
解决办法
2万
查看次数