【问题标题】:Mnist: get confusion matrixMnist:获取混淆矩阵
【发布时间】:2018-11-17 14:04:47
【问题描述】:

我尝试获取mnist数据集的混淆矩阵。

这是我的代码:

mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(512, activation=tf.nn.tanh),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])


model.fit(x_train, y_train, epochs=1, callbacks=[history])

test_predictions = model.predict(x_test)


# Compute confusion matrix
confusion = tf.confusion_matrix(y_test, test_predictions)

问题在于test_prediction 是一个 10000 x 10 矩阵,而 y_test 是 10000 x 1 矩阵。事实上,神经网络不会为每个测试样本提供输出。如何计算这种情况下的混淆矩阵?

然后我怎样才能呈现混淆矩阵?我可以为此目的使用 sci-kit 库吗?

【问题讨论】:

    标签: python tensorflow keras confusion-matrix


    【解决方案1】:

    如果你使用 .predict_classes 方法而不是仅仅进行预测,你会得到一个概率最高的类向量。

    然后,您可以使用sklearn 中的confusion_matrix。

    test_predictions = model.predict_classes(x_test)
    
    from sklearn.metrics import confusion_matrix
    
    cm = confusion_matrix(y_true = y_test, y_pred = test_predictions)
    print(cm)
    

    这里test_predictions的形状是(10000,)。

    打印出来的结果是这样的:

    array([[ 967,    1,    1,    2,    0,    1,    5,    0,    2,    1],
       [   0, 1126,    3,    1,    0,    1,    1,    0,    3,    0],
       [   3,    2, 1001,    8,    1,    0,    3,    6,    8,    0],
       [   0,    0,    1, 1002,    0,    1,    0,    1,    5,    0],
       [   3,    1,    2,    2,  955,    2,    6,    1,    3,    7],
       [   3,    1,    0,   37,    1,  833,    9,    0,    6,    2],
       [   4,    3,    1,    1,    1,    3,  941,    0,    4,    0],
       [   2,    9,    8,    5,    0,    0,    0,  988,    8,    8],
       [   3,    1,    3,   10,    3,    2,    2,    3,  946,    1],
       [   3,    8,    0,   10,    8,    8,    1,    4,    5,  962]],
      dtype=int64)
    

    【讨论】:

      【解决方案2】:

      这可能是因为您的预测包含所有可能类别的概率。您需要选择概率最高的类,这将导致与 y_test 相同的维度。您可以使用 numpy 中的 argmax() 方法。它的工作原理是这样的:

      import numpy as np
      a = np.array([[0.9,0.1,0],[0.2,0.3,0.5],[0.4,0.6,0]])
      np.argmax(a, axis=0)
      array([0, 2, 1])
      

      您可以使用 sklearn 生成混淆矩阵。你的代码会变成这样

      from sklearn.metrics import confusion_matrix
      import numpy as np
      
      confusion = confusion_matrix(y_test, np.argmax(test_predictions,axis=1))
      

      【讨论】:

      • 是的,但我必须得到与更高概率相关的数字。你的代码只是给了我更高的概率
      • 似乎出于某种原因我必须这样做confusion_matrix(y_test, np.argmax(test_predictions,axis=1))
      • 是的。对不起,我考虑了错误的轴。 axis=1 将减小第一个轴的形状。例如,如果您之前的形状是 (100,10),则缩小后的形状将是 (100,),因此它在第 1 维上运行,保持第 0 维 (axis=0) 固定。
      • 它并没有真正给你最高概率,而是最高概率的索引
      猜你喜欢
      • 2018-10-02
      • 2021-12-03
      • 2018-03-08
      • 2019-10-20
      • 1970-01-01
      • 2015-12-17
      • 2020-10-01
      • 2012-01-20
      • 2019-11-23
      相关资源
      最近更新 更多