简体   繁体   English

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

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

I am getting a我得到一个

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

error when I try to use confusion matrix.当我尝试使用混淆矩阵时出错。

I am doing my first deep learning project.我正在做我的第一个深度学习项目。 I am new to it.我是新手。 I am using the mnist dataset provided by keras.我正在使用由 keras 提供的 mnist 数据集。 I have trained and tested my model successfully.我已经成功地训练和测试了我的模型。

However, when I try to use the scikit learn confusion matrix I get the error stated above.但是,当我尝试使用 scikit learn 混淆矩阵时,我收到了上述错误。 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).从我在网上找到的内容来看,它可能与损失函数有关(我在代码中使用了categorical_crossentropy )。 I tried changing it to sparse_categorical_crossentropy but that just gave me the我尝试将其更改为sparse_categorical_crossentropy但这只是给了我

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.当我在模型上运行fit()函数时。

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?我怎样才能解决这个问题?

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() , ie尽管您已经使用model.predict_classes()进行了预测,即

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

your test_labels are still one-hot encoded:您的test_labels仍然是 one-hot 编码:

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]])

The same problem is repeated 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 here (hope that's not illegal).所以我想在这里添加这个问题的答案(希望这不是非法的)。

The below code is self-explanatory.下面的代码是不言自明的。 @desertnaut gave exact reasons, so no need to explain more stuff. @desertnaut 给出了确切的原因,因此无需解释更多内容。 The author of the question tried to pass predicted features separately to the fit functions, which I believe can give a better understanding to the newcomer.在笔者的问题试图通过单独预测功能以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)

Extract features from pre-trained weights (Transfer Learning).从预训练的权重中提取特征(转移学习)。

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

Reshape for further training process.重塑以进行进一步的培训过程。

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

The model with sequential API.具有顺序 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'))

Compile and Run.编译并运行。

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

Evaluate.评估。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何处理 ValueError:分类指标无法处理多标签指标和多类目标错误的混合 - how to handle ValueError: Classification metrics can't handle a mix of multilabel-indicator and multiclass targets error 错误:分类指标无法处理多类多输出和多标记指标目标的混合 - Error: Classification metrics can't handle a mix of multiclass-multioutput and multilabel-indicator targets 如何修复 ValueError:分类指标无法处理模型的多类和多标签指标目标的混合? - How to fix ValueError: Classification metrics can't handle a mix of multiclass and multilabel-indicator targets for model? ValueError:分类指标无法在 ROC 曲线计算中处理多类和多标签指标目标的混合 - ValueError: Classification metrics can't handle a mix of multiclass and multilabel-indicator targets in ROC curve calculation ValueError:分类指标无法处理多类和多标记指标目标的混合 - ValueError: Classification metrics can't handle a mix of multiclass and multilabel-indicator targets 分类指标无法处理多类和多标签指标目标的混合 - Classification metrics can't handle a mix of multiclass and multilabel-indicator targets ValueError:分类指标无法处理多标签指标和连续多输出目标错误的混合 - ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets error ValueError:分类指标无法处理多标签指标和连续多输出目标的混合 - ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets ValueError:分类指标无法处理多标签指标和连续多输出目标 sklearn 的混合 - ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets sklearn ValueError:分类指标无法处理多标签指标和二进制目标的混合 - ValueError: Classification metrics can't handle a mix of multilabel-indicator and binary targets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM