混淆矩阵错误“分类指标无法处理多标签指标和多类别目标的混合" [英] confusion matrix error "Classification metrics can't handle a mix of multilabel-indicator and multiclass targets"

查看:4671
本文介绍了混淆矩阵错误“分类指标无法处理多标签指标和多类别目标的混合"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用混淆矩阵时,我得到分类指标无法处理multilabel-indicator和multiclass目标的混合错误.

I am getting the Classification metrics can't handle a mix of multilabel-indicator and multiclass targets error when I try to use confusion matrix.

我正在做我的第一个深度学习项目.我是新来的.我正在使用keras提供的mnist数据集.我已经成功地训练和测试了我的模型.

I am doing my first deep learning project. I am new to it. I am using the mnist dataset provided by keras. I have trained and tested my model successfully.

但是,当我尝试使用scikit学习混淆矩阵时,出现上述错误.我一直在寻找答案,尽管有关于此错误的答案,但没有一个对我有用.从网上发现的结果来看,它可能与损失函数有关(我在代码中使用了 categorical_crossentropy ).我尝试将其更改为 sparse_categorical_crossentropy ,但这只是给我提供了检查目标时的错误:预期density_2具有形状(1,)但具有形状(10,)的数组我在模型上运行fit()函数.

However, when I try to use the scikit learn confusion matrix I get the error stated above. I have searched for an answer and while there are answers on this error, none of them worked for me. From what I found online it probably has something to do with the loss function (I use the categorical_crossentropy in my code). I tried changing it to sparse_categorical_crossentropy but that just gave me the Error when checking target: expected dense_2 to have shape (1,) but got array with shape (10,) when I run the fit() function on the model.

这是代码. (为简洁起见,我省略了进口)

This is the code. (I have left out the imports for the sake of brevity)

model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(28 * 28,)))
model.add(Dense(10, activation='softmax')) 

model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

model.fit(train_images, train_labels, epochs=10, batch_size=128)

rounded_predictions = model.predict_classes(test_images, batch_size=128, verbose=0)

cm = confusion_matrix(test_labels, rounded_predictions)

我该如何解决?预先感谢您决定提供帮助.

How can i fix this? Thanks in advance if you decide to help.

推荐答案

混淆矩阵需要两个标签&预测是个位数,而不是一个热编码的矢量;尽管您已经使用model.predict_classes()(即

Confusion matrix needs both labels & predictions as single-digits, not as one-hot encoded vectors; although you have done this with your predictions using model.predict_classes(), i.e.

rounded_predictions = model.predict_classes(test_images, batch_size=128, verbose=0)
rounded_predictions[1]
# 2

您的test_labels仍然是一键编码:

test_labels[1]
# array([0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)

因此,您也应该将它们转换为一位数字,如下所示:

So, you should convert them too to single-digit ones, as follows:

import numpy as np
rounded_labels=np.argmax(test_labels, axis=1)
rounded_labels[1]
# 2

之后,混淆矩阵应该会出现:

After which, the confusion matrix should come up OK:

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(rounded_labels, rounded_predictions)
cm
# result:
array([[ 971,    0,    0,    2,    1,    0,    2,    1,    3,    0],
       [   0, 1121,    2,    1,    0,    1,    3,    0,    7,    0],
       [   5,    4,  990,    7,    5,    3,    2,    7,    9,    0],
       [   0,    0,    0,  992,    0,    2,    0,    7,    7,    2],
       [   2,    0,    2,    0,  956,    0,    3,    3,    2,   14],
       [   3,    0,    0,   10,    1,  872,    3,    0,    1,    2],
       [   5,    3,    1,    1,    9,   10,  926,    0,    3,    0],
       [   0,    7,   10,    1,    0,    2,    0,  997,    1,   10],
       [   5,    0,    3,    7,    5,    7,    3,    4,  937,    3],
       [   5,    5,    0,    9,   10,    3,    0,    8,    3,  966]])

PS将来,请注意,预计SO问题将在问题本身中包含代码.

PS For the future, please notice that SO questions are expected to include the code in the question itself.

这篇关于混淆矩阵错误“分类指标无法处理多标签指标和多类别目标的混合"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆