How to write a confusion matrix in Python?

前端 未结 14 2000
太阳男子
太阳男子 2020-12-04 06:48

I wrote a confusion matrix calculation code in Python:

def conf_mat(prob_arr, input_arr):
        # confusion matrix
        conf_arr = [[0, 0], [0, 0]]

            


        
14条回答
  •  我在风中等你
    2020-12-04 07:21

    You can make your code more concise and (sometimes) to run faster using numpy. For example, in two-classes case your function can be rewritten as (see mply.acc()):

    def accuracy(actual, predicted):
        """accuracy = (tp + tn) / ts
    
        , where:    
    
            ts - Total Samples
            tp - True Positives
            tn - True Negatives
        """
        return (actual == predicted).sum() / float(len(actual))
    

    , where:

    actual    = (numpy.array(input_arr) == 2)
    predicted = (numpy.array(prob_arr) < 0.5)
    

提交回复
热议问题