简体   繁体   中英

Confusion matrix when all classes are not predicted with caret package in R

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM