How to write a confusion matrix in Python?

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

    Here is a simple implementation that handles an unequal number of classes in the predicted and actual labels (see examples 3 and 4). I hope this helps!

    For folks just learning this, here's a quick review. The labels for the columns indicate the predicted class, and the labels for the rows indicate the correct class. In example 1, we have [3 1] on the top row. Again, rows indicate truth, so this means that the correct label is "0" and there are 4 examples with ground truth label of "0". Columns indicate predictions, so we have 3/4 of the samples correctly labeled as "0", but 1/4 was incorrectly labeled as a "1".

    def confusion_matrix(actual, predicted):
        classes       = np.unique(np.concatenate((actual,predicted)))
        confusion_mtx = np.empty((len(classes),len(classes)),dtype=np.int)
        for i,a in enumerate(classes):
            for j,p in enumerate(classes):
                confusion_mtx[i,j] = np.where((actual==a)*(predicted==p))[0].shape[0]
        return confusion_mtx
    

    Example 1:

    actual    = np.array([1,1,1,1,0,0,0,0])
    predicted = np.array([1,1,1,1,0,0,0,1])
    confusion_matrix(actual,predicted)
    
       0  1
    0  3  1
    1  0  4
    

    Example 2:

    actual    = np.array(["a","a","a","a","b","b","b","b"])
    predicted = np.array(["a","a","a","a","b","b","b","a"])
    confusion_matrix(actual,predicted)
    
       0  1
    0  4  0
    1  1  3
    

    Example 3:

    actual    = np.array(["a","a","a","a","b","b","b","b"])
    predicted = np.array(["a","a","a","a","b","b","b","z"]) # <-- notice the 3rd class, "z"
    confusion_matrix(actual,predicted)
    
       0  1  2
    0  4  0  0
    1  0  3  1
    2  0  0  0
    

    Example 4:

    actual    = np.array(["a","a","a","x","x","b","b","b"]) # <-- notice the 4th class, "x"
    predicted = np.array(["a","a","a","a","b","b","b","z"])
    confusion_matrix(actual,predicted)
    
       0  1  2  3
    0  3  0  0  0
    1  0  2  0  1
    2  1  1  0  0
    3  0  0  0  0
    

提交回复
热议问题