【问题标题】:Is there a way to implement a 2x2 confusion matrix for multilabel classifier?有没有办法为多标签分类器实现 2x2 混淆矩阵?
【发布时间】:2022-01-14 02:58:24
【问题描述】:

我有兴趣为多标签分类问题创建一个 2x2 混淆矩阵,它只显示总的假/真阳性/阴性。

我有一段代码可以生成一个完整的混淆矩阵,但是有 98 个标签,几乎看不到任何东西。我不太关心是否有一个完整的矩阵,所以一个只显示上述四个属性的 2x2 是理想的,我只是不确定如何实现它。

这里是代码 sn-p,如果有帮助的话:

predictions_d7 = model_d7.predict(x_test_d7)

y_pred = np.argmax(predictions_d7, axis=1)
y_test = np.argmax(Y_test_d7, axis=1)

print(y_test)
print(y_pred)

cm = confusion_matrix(y_test, y_pred)
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=[label_list)
fig, ax = plt.subplots(figsize=(20,20))
disp.plot(ax=ax, values_format="d", cmap='gray')
disp.im_.colorbar.remove()
print( classification_report(y_test,y_pred))

【问题讨论】:

  • 为了计算真/假正/负,您需要为数据设置正负标签。在这种情况下它们应该是什么?没有它,您可以计算出有多少预测是正确的,有多少是不正确的,但这不会给出一个矩阵。

标签: python scikit-learn


【解决方案1】:

在您希望的情况下获得 2x2 矩阵的原因是恰好有两个标签。你可以把它想象成标签 1 和 2,真假都没有关系。

但是,请尝试添加第三个标签并考虑如何计算“真阳性”。有没有可能?

不,它必须是一个 3x3 矩阵,因为每个类有 3 种可能性,因此总共有 9 种可能性 - 例如:它是 1 类,您预测为 1 类。它是 1 类,但您预测了 2 类,等等开。

也许您应该使用收到的 nxn 混淆矩阵,然后使用一些常用指标来评估您的模型(准确度、精确度、召回率等)。您仍然可以在 n 维度上进行操作。有关说明,请参阅此堆栈交换帖子:https://stats.stackexchange.com/questions/91044/how-to-calculate-precision-and-recall-in-a-3-x-3-confusion-matrix

【讨论】:

    【解决方案2】:

    您可以按如下方式计算 2 x 2 混淆矩阵:

    import numpy as np
    
    def confusion_matrix(y_true, y_pred):
    
        tp = np.logical_and(y_pred == 1, y_true == 1).sum()
        tn = np.logical_and(y_pred == 0, y_true == 0).sum()
        fp = np.logical_and(y_pred == 1, y_true == 0).sum()
        fn = np.logical_and(y_pred == 0, y_true == 1).sum()
    
        return tp, tn, fp, fn
    
    from sklearn.datasets import make_multilabel_classification
    from sklearn.ensemble import RandomForestClassifier
    
    X, y = make_multilabel_classification(random_state=42)
    
    clf = RandomForestClassifier(max_depth=3, random_state=42)
    clf.fit(X, y)
    
    y_pred = clf.predict(X)
    
    tp, tn, fp, fn = confusion_matrix(y, y_pred)
    print(tp, tn, fp, fn)
    # 114 314 7 65
    

    【讨论】:

      猜你喜欢
      • 2021-11-11
      • 2020-06-17
      • 2014-10-19
      • 2018-07-30
      • 2020-01-22
      • 2023-03-05
      • 2020-10-24
      • 2018-11-06
      • 2019-03-01
      相关资源
      最近更新 更多