python - 从 Keras 多类模型中获取混淆矩阵

标签 python keras scikit-learn multiclass-classification

<分区>

我正在使用 Keras 构建多类模型。

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, callbacks=[checkpoint], validation_data=(X_test, y_test))  # starts training

这是我的测试数据的样子(它是文本数据)。

X_test
Out[25]: 
array([[621, 139, 549, ...,   0,   0,   0],
       [621, 139, 543, ...,   0,   0,   0]])

y_test
Out[26]: 
array([[0, 0, 1],
       [0, 1, 0]])

生成预测后...

predictions = model.predict(X_test)
predictions
Out[27]: 
array([[ 0.29071924,  0.2483743 ,  0.46090645],
       [ 0.29566404,  0.45295066,  0.25138539]], dtype=float32)

我执行了以下操作以获得混淆矩阵。

y_pred = (predictions > 0.5)

confusion_matrix(y_test, y_pred)
Traceback (most recent call last):

  File "<ipython-input-38-430e012b2078>", line 1, in <module>
    confusion_matrix(y_test, y_pred)

  File "/Users/abrahammathew/anaconda3/lib/python3.6/site-packages/sklearn/metrics/classification.py", line 252, in confusion_matrix
    raise ValueError("%s is not supported" % y_type)

ValueError: multilabel-indicator is not supported

但是,我遇到了上述错误。

在 Keras 中构建多类神经网络时如何获得混淆矩阵?

最佳答案

您对 confusion_matrix 的输入必须是一个 int 数组,而不是一个热编码。

matrix = metrics.confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1))

关于python - 从 Keras 多类模型中获取混淆矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50920908/

相关文章:

python - TensorFlow ValueError : Cannot feed value of shape (64, 64, 3) for Tensor u'Placeholder : 0', which has shape ' (? , 64, 64, 3)'

machine-learning - 如何在神经网络的输出中执行诸如国际象棋走棋合法性之类的规则?

python - 混淆矩阵-Sklearn 0.22 数字格式错误

python - 如何找到每个客户的相似地址数量?

python - 在python中将月份名称更改为日期

python - 是否有 Tensorflow Object Detection API 的发布版本?

python - 如何单击 python selenium 中 web 元素列表中的第二个元素?

python - 为什么 `tf.pad` 填充参数需要额外的增量来提高准确性?

r - 如何避免在 R 中使用 keras 使用预训练/外部模型进行图像分类

python - 在 Pandas 中循环 MAPE 函数会抛出错误