【问题标题】:Writing a confusion matrix function taking positive class as an input编写一个以正类为输入的混淆矩阵函数
【发布时间】:2021-12-26 04:48:29
【问题描述】:

我想创建一个混淆矩阵,而不依赖于任何包。我有两个列表(预测值和实际值),我想将它们与正类的指标一起输入到函数中。

例如,当1是正类时:

predicted_lst = [1, 0, 1, 0, 0]
actual_lst = [1, 0, 0, 1, 1]

我的函数目前看起来是这样的,但是效率很低:

def confusion_matrix(predicted, actual, pos_class):
    
    TP = 0
    TN = 0 
    FP = 0
    FN = 0
    
    for i in range(len(actual)):
        if actual[i] == pos_class and predicted[i] == pos_class:
            TP +=1
        elif actual[i] == pos_class and predicted[i] != pos_class:
            FN +=1
        elif actual[i] != pos_class and predicted[i] == pos_class:
            FP +=1
        else:
            TN +=1
    return TP, FP, TN, FN

我的问题是,有没有更有效的方法来编写这段代码?我看到了these posts,但他们没有像我希望的那样将正类作为函数输入。我也不想使用任何包(包括 numpy)

【问题讨论】:

    标签: python machine-learning confusion-matrix


    【解决方案1】:

    您可以编写函数,假设1 是正类,如果0 是正类,只需相应地更改返回值的顺序即可容纳pos_class 参数。

    在循环内,您可以放弃FPFN 的增量计算,因为这些可以从循环外的真实值推导出来:

    def confusion_matrix(predicted, actual, pos_class):
        
        TP = 0
        TN = 0 
        
        for pred, act in zip(predicted, actual):
            if pred == act:
                if act == 0:
                    TN += 1
                else:
                    TP += 1
        
        positive = sum(predicted)
        negative = len(predicted) - positive        
        FP = positive - TP 
        FN = negative - TN
        
        if pos_class == 1:
            return TP, FP, TN, FN
        else:
            return TN, FN, TP, FP
    

    【讨论】:

      【解决方案2】:

      请参阅下面的几个替代解决方案。

      选项 1:

      def confmat_1(actual, predicted, positive, negative):
      
          tn = len([x for x in zip(predicted, actual) if x[0] == negative and x[1] == negative])
          fp = len([x for x in zip(predicted, actual) if x[0] == positive and x[1] == negative])
          fn = len([x for x in zip(predicted, actual) if x[0] == negative and x[1] == positive])
          tp = len([x for x in zip(predicted, actual) if x[0] == positive and x[1] == positive])
      
          return tn, fp, fn, tp
      

      选项 2:

      def confmat_2(actual, predicted, positive, negative):
      
          tn = 0
          fp = 0
          fn = 0
          tp = 0
      
          for x in zip(predicted, actual):
      
              if x[0] == negative and x[1] == negative:
                  tn += 1
      
              elif x[0] == positive and x[1] == negative:
                  fp += 1
      
              elif x[0] == negative and x[1] == positive:
                  fn += 1
      
              else:
                  tp += 1
      
          return tn, fp, fn, tp
      

      例子:

      from sklearn.metrics import confusion_matrix
      
      actual = [1, 0, 0, 1, 1]
      predicted = [1, 0, 1, 0, 0]
      
      # Option 1
      tn, fp, fn, tp = confmat_1(actual, predicted, positive=1, negative=0)
      print(tn, fp, fn, tp)
      # 1 1 2 1
      
      # Option 2
      tn, fp, fn, tp = confmat_2(actual, predicted, positive=1, negative=0)
      print(tn, fp, fn, tp)
      # 1 1 2 1
      
      # Scikit-learn
      tn, fp, fn, tp = confusion_matrix(actual, predicted).ravel()
      print(tn, fp, fn, tp)
      # 1 1 2 1
      

      【讨论】:

        猜你喜欢
        • 2018-02-20
        • 1970-01-01
        • 2011-01-10
        • 2021-07-02
        • 1970-01-01
        • 1970-01-01
        • 2022-12-08
        • 2017-02-18
        • 2017-03-20
        相关资源
        最近更新 更多