Order confusion matrix in R

谁说胖子不能爱 提交于 2020-05-12 06:55:00

问题


I've created a confusion matrix from the observations and its predictions in 3 classes.

classes=c("Underweight", "Normal", "Overweight")

When I compute the confusion matrix, it organizes the classes in the table alphabetical. Here is my code.

# Confusion matrix
Observations <- bmi_classification(cross.m$bmi)
Predicted <- bmi_classification(cross.m$cvpred)

conf <- table(Predicted, Observations)

library(caret) 
f.conf <- confusionMatrix(conf)
print(f.conf)

This produces this output:

Confusion Matrix and Statistics

             Observations
Predicted     Normal Overweight Underweight
  Normal          17          0           1
  Overweight       1          4           0
  Underweight      1          0           1

So, I would like it to first Underweight, then normal and finally Overweight. I've tried to pass the order to the matrix as an argument but no luck with that.

EDIT:

I tried reordering it,

conf <- table(Predicted, Observations)

reorder = matrix(c(9, 7, 8, 3, 1, 2, 6, 4, 5), nrow=3, ncol=3)

conf.reorder <- conf[reorder]

but I'm getting, [1] 1 1 0 1 17 1 0 0 4


回答1:


Try this then redo your code:

 cross.m$Observations <- factor( cross.m$Observations, 
                              levels=c("Underweight","Normal","Overweight") )
 cross.m$Predicted<- factor( cross.m$Predicted, 
                              levels=c("Underweight","Normal","Overweight") )
conf <- table(Predicted, Observations)

library(caret) 
f.conf <- confusionMatrix(conf)
print(f.conf)

Ordinary matrix methods would probably not work since a caret confusion matrix object is a list.



来源:https://stackoverflow.com/questions/22409010/order-confusion-matrix-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!