代码之家  ›  专栏  ›  技术社区  ›  sixtytrees

在SKU矩阵中包含命中率为零的行和列

  •  0
  • sixtytrees  · 技术社区  · 5 年前

    我想把几个混淆矩阵的结果合并到一个矩阵中。我的预测值和基本真值可以是0、1、2。 只要存在所有值,我就可以将值相加。但是,当一些值根本不存在时,行和列的数量会发生变化。因此,我无法将这样的数组添加到结果数组中。如何强制混淆矩阵包含零样本的行和列?

    import numpy as np
    from sklearn.metrics import confusion_matrix
    
    combined_confusion_matrix = np.zeros((3,3))
    gt1 = np.array([0,1,2,0,1,2])
    pred1 = np.array([2,1,2,0,1,2])
    cm1 = confusion_matrix(gt1, pred1)  # [[1 0 1][0 2 0][0 0 2]]
    combined_confusion_matrix +=  cm1
    gt2 = np.array([0,0,2,2])
    pred2 = np.array([0,2,2,2])
    cm2 = confusion_matrix(gt2, pred2)  # Got [[1 1][0 2]], Desired [[1 0 1][0 0 0][0 0 2]]
    combined_confusion_matrix += cm2  # Error due to different dimensions
    
    0 回复  |  直到 5 年前
        1
  •  0
  •   sixtytrees    5 年前

    这对我很有用:

    my_bins = [0, 1, 2]
    cm2 = confusion_matrix(gt2, pred2, labels=my_bins)