混淆矩阵错误 "Classification metrics can't handle a mix of multilabel-indicator and multiclass targets"

confusion matrix error "Classification metrics can't handle a mix of multilabel-indicator and multiclass targets"

我得到一个

Classification metrics can't handle a mix of multilabel-indicator and multiclass targets

当我尝试使用混淆矩阵时出错。

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

但是,当我尝试使用 scikit 学习混淆矩阵时,出现了上述错误。我已经搜索了一个答案,虽然有关于此错误的答案,但其中 none 对我有用。从我在网上找到的,它可能与损失函数有关(我在我的代码中使用 categorical_crossentropy )。我尝试将其更改为 sparse_categorical_crossentropy 但这只是给了我

Error when checking target: expected dense_2 to have shape (1,) but got array with shape (10,)

当我 运行 模型上的 fit() 函数时。

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

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)

我该如何解决这个问题?

混淆矩阵需要将标签和预测都作为单个数字,而不是单热编码向量;尽管您已经使用 model.predict_classes() 对您的预测进行了此操作,即

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)

所以,你也应该将它们转换为个位数,如下所示:

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

之后,混淆矩阵应该就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]])

这里重复了同样的问题here, and the solution is overall the same. That's why, that question is closed and unable to receive an answer. So I like to add an answer to this question(希望这不是非法的)。

下面的代码是不言自明的。 @desertnaut 给出了确切的原因,所以不需要解释更多的东西。 question的作者尝试将预测的特征分别传递给fit的函数,相信可以让新手更好的理解

import numpy as np
import pandas as pd 
import tensorflow as tf 
from sklearn.model_selection import train_test_split
from tensorflow.keras.applications.resnet50 import ResNet50

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

x_train = np.expand_dims(x_train, axis=-1)
x_train = np.repeat(x_train, 3, axis=-1)
x_train = x_train.astype('float32') / 255
y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)

print(x_train.shape, y_train.shape)
# (60000, 28, 28, 3) (60000, 10)

从预训练权重中提取特征(迁移学习)。

base_model = ResNet50(weights='imagenet', include_top=False)
pred_x_train = base_model.predict(x_train)
pred_x_train.shape
# (60000, 1, 1, 2048)

为进一步的训练过程重塑。

pred_x_train = pred_x_train.reshape(60000, 1*1*2048)
pred_x_train.shape
# (60000, 2048)

具有顺序的模型API。

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(512, activation='relu', input_shape=(2048,)))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(10, activation='softmax'))

编译并运行.

model.compile(loss='categorical_crossentropy',optimizer='Adam',metrics=['accuracy'])
model.fit(pred_x_train, y_train, epochs=2, verbose=2)

Epoch 1/2
1875/1875 - 4s - loss: 0.6993 - accuracy: 0.7744
Epoch 2/2
1875/1875 - 4s - loss: 0.4451 - accuracy: 0.8572

评价。

from sklearn.metrics import classification_report

# predict 
pred = model.predict(pred_x_train, batch_size = 32)
pred = np.argmax(predictions, axis=1)
# label
y_train = np.argmax(y_train, axis=1)

print(y_train.shape, pred.shape)
print(y_train[:5], pred[:5])
# (60000,) (60000,)
# [5 0 4 1 9] [5 0 4 1 9]
print(classification_report(y_train, pred))

              precision    recall  f1-score   support

           0       0.95      0.97      0.96      5923
           1       0.97      0.99      0.98      6742
           2       0.90      0.94      0.92      5958
           3       0.89      0.91      0.90      6131
           4       0.97      0.89      0.93      5842
           5       0.88      0.91      0.89      5421
           6       0.95      0.97      0.96      5918
           7       0.94      0.95      0.94      6265
           8       0.94      0.78      0.85      5851
           9       0.87      0.93      0.90      5949

    accuracy                           0.93     60000
   macro avg       0.93      0.92      0.92     60000
weighted avg       0.93      0.93      0.92     60000