0
votes

I have a classification model predict(model, test.x) to evaluate a model on a data with 11 classes, the result of prediction:

table(predicted_class)
0   1   2   3   5   6   8  10 
7   6  232  11  74  58   1   1 

My test lables (truth) are:

table(test.y)
  0   1   2   3   4   5   6   7   8   9  10 
105  16  78  25  14  74  12   9  23  15  19 

When I want to obtain the confusion matrix with caret package, I have this error message because classes 7 and 9 are not predicted by my model:

caret::confusionMatrix(test.y, predicted_class, mode = "everything")
Error in confusionMatrix.default(test.y, predicted_class,  : 
  the data cannot have more levels than the reference

How can I obtain a confusion matrix when some factor level are missing in prediction: How can I add 0 automatically for predicted_class for missing classes (in this case 4, 7 and 9)

1

1 Answers

3
votes

Make the levels same by joining the factors with union

all_class <- union(predicted_class, test.y)
newtable <- table(factor(predicted_class, all_class), factor(test.y, all_class))
caret::confusionMatrix(newtable)