R中的订单混淆矩阵

时间:2014-03-14 15:16:33

标签: r confusion-matrix

我已经从3个类中的观察及其预测中创建了一个混淆矩阵。

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

当我计算混淆矩阵时,它按字母顺序组织表中的类。这是我的代码。

# 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)

这会产生此输出:

Confusion Matrix and Statistics

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

所以,我想首先减肥,然后正常,最后超重。我试图将订单作为参数传递给矩阵,但没有运气。

编辑:

我尝试重新排序,

conf <- table(Predicted, Observations)

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

conf.reorder <- conf[reorder]

但我得到了,[1] 1 1 0 1 17 1 0 0 4

1 个答案:

答案 0 :(得分:3)

尝试此操作,然后重做代码:

 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)

caret confusion matrix object is a list.

以来,普通矩阵方法可能无效