问题
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