Search code examples
pythonpandasscikit-learnconfusion-matrix

Construct confusion matrix with missing data


I am constructing a confusion matrix with 4 classes (1.0, 2.0, 3.0, 4.0)

However, in some cases, the expected and predicted arrays both only have 3 classes:

>> expected
array([ 3.,  2.,  4.])
>> predicted
array([4.0, 2.0, 3.0])

So, the resulting confusion matrix only has 3x3 matrix:

from sklearn.metrics import confusion_matrix
confusion_matrix(expected, predicted)

array([[1, 0, 0],
       [0, 1, 0],
       [0, 0, 1]])

How can I still make a 4x4 confusion matrix in this case?


Solution

  • Yes you can. If you use the labels keyword argument, you can print a confusion matrix for all your classes.

    >>>confusion_matrix(expected,predicted,labels=[1.,2.,3.,4.])
    array([[0, 0, 0, 0],
           [0, 1, 0, 0],
           [0, 0, 0, 1],
           [0, 0, 1, 0]])
    

    A helpful link is confusion_matrix docs