【问题标题】:How to invert the confusion matrix in Sklearn `plot_confusion_matrix` function? [closed]如何反转 Sklearn `plot_confusion_matrix` 函数中的混淆矩阵? [关闭]
【发布时间】:2021-08-17 06:58:22
【问题描述】:

使用sklearn.metrics.plot_confusion_matrix 函数时,它会绘制一个混淆矩阵,其中实际值作为行,预测值作为列。但是,我看到的所有其他来源都将矩阵设置为实际值作为列并预测为行。坦率地说,这种方法似乎更好,而 Sklearn 的方法确实令人困惑

如果绘图以这种格式显示矩阵,那就太好了。我将如何实现这一目标?

这个thread 接近回答,但它没有直接回答。

【问题讨论】:

标签: python pandas machine-learning scikit-learn classification


【解决方案1】:

似乎 Sklearn 提供了另一个函数来显示混淆矩阵。它不采用估计器和验证集,而是直接接受矩阵并绘制它:

import numpy as np
from sklearn.metrics import ConfusionMatrixDisplay
from sklearn.metrics import confusion_matrix

# Create the CM
cm = confusion_matrix(y_true, y_pred)
# Flip it using Numpy and feed it to the display function
cmp = ConfusionMatrixDisplay(cm, display_labels=['label_1', 'label_2'])

plt.xlabel('Actual')
plt.ylabel('Predicted')

cmp.plot();

它还具有display_labels 参数以在绘图前接受自定义类标签。当我们为函数提供一个倒置矩阵时,添加自定义轴标签很重要。这是函数的文档:

https://scikit-learn.org/stable/modules/generated/sklearn.metrics.ConfusionMatrixDisplay.html

【讨论】:

    猜你喜欢
    • 2020-07-15
    • 2019-10-15
    • 2019-06-05
    • 2021-09-29
    • 2021-10-25
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    相关资源
    最近更新 更多