简体   繁体   English

R中的阶混淆矩阵

[英]Order confusion matrix in R

I've created a confusion matrix from the observations and its predictions in 3 classes. 我已经根据3类的观察结果及其预测创建了一个混淆矩阵。

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] 1 1 0 1 17 1 0 0 4

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. 由于插入符号混淆矩阵对象是一个列表,因此普通矩阵方法可能无法工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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