【问题标题】:confusion matrix error "Classification metrics can't handle a mix of multilabel-indicator and multiclass targets"混淆矩阵错误“分类指标无法处理多标签指标和多类目标的混合”
【发布时间】:2021-05-14 15:31:24
【问题描述】:

我得到了一个

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

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

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

但是,当我尝试使用 scikit 学习混淆矩阵时,我得到了上述错误。我已经搜索了一个答案,虽然有关于这个错误的答案,但没有一个对我有用。从我在网上找到的内容来看,它可能与损失函数有关(我在代码中使用了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)

我该如何解决这个问题?

【问题讨论】:

    标签: python machine-learning keras scikit-learn confusion-matrix


    【解决方案1】:

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

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

    您的 test_labels 仍然是 one-hot 编码:

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

    【讨论】:

    【解决方案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
    

    【讨论】:

    • 您能解释一下验证部分吗?你为什么要做模特。 predict(X_train) 而不是 X_test?
    • 没关系,我只是将它用于演示目的。您也可以简单地传递X_test,但一定要预处理X_testy_testX_trainy_train 相同。
    • 既然你通过了 y_train,这是否有助于理解模型在训练中的表现,如果我通过 X_test,这意味着模型在测试数据上的表现?
    • 是的。它实际上是机器学习的基础。在我的回答中,基本上有两个分裂:(1)。训练集(x_trainy_train)和(2)。测试集(x_testy_test)。我们使用训练集进行训练model.fit,稍后使用测试集进行评估model.predict
    • 是的,predictions 是概率,当您执行 np.argmax(..) 时,它会为您提供预测的标签。
    猜你喜欢
    • 2021-11-21
    • 2021-06-25
    • 2018-08-26
    • 2018-12-03
    • 2019-10-23
    • 2021-03-18
    • 2021-12-20
    • 2019-06-29
    相关资源
    最近更新 更多