How to write a confusion matrix in Python?

前端 未结 14 1984
太阳男子
太阳男子 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:28

    If you don't want scikit-learn to do the work for you...

        import numpy
        actual = numpy.array(actual)
        predicted = numpy.array(predicted)
    
        # calculate the confusion matrix; labels is numpy array of classification labels
        cm = numpy.zeros((len(labels), len(labels)))
        for a, p in zip(actual, predicted):
            cm[a][p] += 1
    
        # also get the accuracy easily with numpy
        accuracy = (actual == predicted).sum() / float(len(actual))
    

    Or take a look at a more complete implementation here in NLTK.

提交回复
热议问题