【问题标题】:Why does my sklearn.metrics confusion_matrix output look transposed?为什么我的 sklearn.metrics chaos_matrix 输出看起来是转置的?
【发布时间】:2020-02-10 21:13:17
【问题描述】:

我的理解是混淆矩阵应该在列中显示 TRUE 类,在行中显示 PREDICTED 类。因此,列的总和应该等于 TRUE 系列的 value_counts()。

我在这里提供了一个例子:

from sklearn.metrics import confusion_matrix

pred = [0, 0, 0, 1]
true = [1, 1, 1, 1]

confusion_matrix(true, pred)

为什么这会给我以下输出?当然应该是那个转置?

array([[0, 0],
       [3, 1]], dtype=int64)

【问题讨论】:

    标签: python scikit-learn confusion-matrix


    【解决方案1】:

    使用sklearn可以随心所欲,只需适当更改以下代码

    from sklearn.metrics import ConfusionMatrixDisplay
    import matplotlib.pyplot as plt
    
    fig, ax = plt.subplots(1,1,figsize=(7,4))
    
    ConfusionMatrixDisplay(confusion_matrix(predict,y_test,labels=[1,0]),
                           display_labels=[1,0]).plot(values_format=".0f",ax=ax)
    
    ax.set_xlabel("True Label")
    ax.set_ylabel("Predicted Label")
    plt.show()
    

    【讨论】:

      【解决方案2】:

      混淆可能是因为sklearn 遵循与维基百科文章不同的混淆矩阵轴约定。 所以,回答您的问题:它以特定格式为您提供输出,因为sklearn 期望您以特定方式阅读它。

      这里有两种不同的混淆矩阵的写法:

      【讨论】:

        【解决方案3】:
        猜你喜欢
        • 2022-07-26
        • 1970-01-01
        • 1970-01-01
        • 2013-07-15
        • 1970-01-01
        • 2011-05-28
        • 2021-09-15
        • 2020-12-05
        • 1970-01-01
        相关资源
        最近更新 更多