当我尝试使用混淆矩阵时,我得到分类指标无法处理multilabel-indicator和multiclass目标的混合错误。
我正在做我的第一个深度学习项目。我是新来的。我正在使用keras提供的mnist数据集。我已经成功地训练和测试了我的模型。
但是,当我尝试使用scikit学习混淆矩阵时,出现上述错误。我一直在寻找答案,尽管有关于此错误的答案,但没有一个对我有用。从网上发现的结果来看,它可能与损失函数有关(我在代码中使用了 categorical_crossentropy )。我尝试将其更改为 sparse_categorical_crossentropy ,但这只是给我提供了检查目标时出现的错误:预期density_2具有形状(1,)但具有形状(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)
我该如何解决?预先感谢您决定提供帮助。
答案 0 :(得分:2)
同样的问题重复here,解决方案大体相同。这就是为什么,该问题已关闭,无法得到答案。所以我想在这里添加一个对这个 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
答案 1 :(得分:1)
混淆矩阵需要标签和预测都为个位数,而不是一个编码的矢量。尽管您已经使用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
之后,混淆矩阵应该会出现:
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问题将在问题本身中包含代码。