混淆矩阵错误:“分类指标无法处理多标签指示器和多类目标的混合”

我遇到一个

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

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

这是我的第一个深度学习项目。我对此很新。我使用了Keras提供的MNIST数据集。我已经成功地训练和测试了我的模型。

然而,当我尝试使用scikit-learn的混淆矩阵时,我得到了上述错误。我已经搜索了答案,尽管有关于这个错误的回答,但对我都不适用。从我在网上找到的信息来看,这可能与损失函数有关(我在代码中使用了categorical_crossentropy)。我尝试将其更改为sparse_categorical_crossentropy,但这只是在运行模型的fit()函数时给我带来了

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

错误。

这是我的代码。(为了简洁,我省略了导入部分)

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') / 255test_images = test_images.reshape((10000, 28 * 28))test_images = test_images.astype('float32') / 255train_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)

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

之后,混淆矩阵应该能正常生成:

from sklearn.metrics import confusion_matrixcm = 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]])

Related Posts

如何使用Google Protobuf解析、编辑和生成object_detection/pipeline.config文件

我在一个常见的集成学习范式中训练多个模型,目前我在处理…

我的GridSearchCV不起作用,我不知道为什么

大家好,我在使用GridSearchCV时遇到了问题,…

Keras: 两个同时进行的层,其中一个对前一层的输出进行卷积

我想实现这样的模型连接: 输入图像1 -> 卷积层1 …

如何将行数据转换为列数据而不使用独热编码

我有一个如下所示的数据集。 MonthDate Day…

使用 ML Kit 与 NNAPI

我正在尝试在运行 Android 9 的设备上使用新的…

Vowpal Wabbit 可能的哈希冲突

我在VW中生成了一个模型,并且在相同的数据上生成了两个…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注