计算混淆矩阵的准确率和精度

17

有没有工具/ R包可用于计算混淆矩阵的准确性和精度?

公式和数据结构在这里


可能重复: https://dev59.com/0Ww15IYBdhLWcg3wbbJx - agstudy
1
那个帖子讨论了如何创建混淆矩阵。我的问题是在混淆矩阵的基础上计算准确率和精确度。 - Ajay Singh
1
我找到了一个R包,可以帮助做到这一点。http://cran.r-project.org/web/packages/caret/caret.pdf - Ajay Singh
5个回答

33

是的,您可以使用Caret package在R中计算混淆矩阵中的准确度和精度。

以下是示例:

lvs <- c("normal", "abnormal")
truth <- factor(rep(lvs, times = c(86, 258)),
                levels = rev(lvs))
pred <- factor(
               c(
                 rep(lvs, times = c(54, 32)),
                 rep(lvs, times = c(27, 231))),               
               levels = rev(lvs))

xtab <- table(pred, truth)
# load Caret package for computing Confusion matrix
library(caret) 
confusionMatrix(xtab)

xtab的混淆矩阵如下:

Confusion Matrix and Statistics

          truth
pred       abnormal normal
  abnormal      231     32
  normal         27     54

               Accuracy : 0.8285
                 95% CI : (0.7844, 0.8668)
    No Information Rate : 0.75
    P-Value [Acc > NIR] : 0.0003097

                  Kappa : 0.5336
 Mcnemar's Test P-Value : 0.6025370

            Sensitivity : 0.8953
            Specificity : 0.6279
         Pos Pred Value : 0.8783
         Neg Pred Value : 0.6667
             Prevalence : 0.7500
         Detection Rate : 0.6715
   Detection Prevalence : 0.7645

       'Positive' Class : abnormal

所以这里是你想要的一切。


4
如何通过混淆矩阵(confusion matrix)的结果来编程计算准确率(precision)和召回率(recall)? - Harsh Trivedi
1
谢谢Nishu。我想要补充一点额外的信息。confusionMatrix(xtab)依赖于“e1071”软件包,因此可能需要安装此软件包。 - Rupesh
@Rupesh:是的,那是必需的。 - Nishu Tayal

14

@Harsh Trivedi

byClass可以帮助你从摘要中提取出准确率召回率。PPV是准确率。敏感性是召回率。https://en.wikipedia.org/wiki/Precision_and_recall

library(caret)

result <- confusionMatrix(prediction, truth)
precision <- result$byClass['Pos Pred Value']    
recall <- result$byClass['Sensitivity']

我想你希望提取精确率和召回率来计算 f-measure(F值),那么接下来就是它。

f_measure <- 2 * ((precision * recall) / (precision + recall))

我还发现了这个方便的在线计算器,可以用来进行审查。 http://www.marcovanetti.com/pages/cfmatrix/?noc=2

-bg


不需要自己计算,只需使用confusionMatrix(..., mode = "everything"),请参见下面的答案。 - undefined

0
如果有其他人在寻找:感谢BGA上面的答案,我更清楚如何阅读confusionMatrix()输出,并意识到您可以直接从result$ByClass输出中获取F1分数。
 result$byClass
         Sensitivity          Specificity       Pos Pred Value       Neg Pred Value 
           0.9337442            0.8130531            0.8776249            0.8952497 
           Precision               Recall                   F1           Prevalence 
           0.8776249            0.9337442            0.9048152            0.5894641 
      Detection Rate Detection Prevalence    Balanced Accuracy 
           0.5504087            0.6271571            0.8733987 

使用与上面评论中相同的公式计算下面的f_measure,结果为0.9048152。

您还可以从results$overall获取准确性。

result$overall
      Accuracy          Kappa  AccuracyLower  AccuracyUpper   AccuracyNull AccuracyPValue 
  8.841962e-01   7.573509e-01   8.743763e-01   8.935033e-01   5.894641e-01   0.000000e+00 
 McnemarPValue 
  2.745521e-13

或者使用results中的平衡准确率


0
比起自己计算F1分数或使用result$byClass,使用confusionMatrix中的mode参数更加方便,可以直接打印出F1分数、召回率和精确度。
默认情况下,使用confusionMatrix(..., mode = "sens_spec"),会显示灵敏度和特异度。要显示F1分数、召回率和精确度,使用mode = "prec_recall"。要显示所有指标,使用mode = "everything"
library(caret)

x <- matrix(c(231, 27, 32, 54), nrow = 2)
x
#>      [,1] [,2]
#> [1,]  231   32
#> [2,]   27   54

confusionMatrix(x, mode = "everything")
#> Confusion Matrix and Statistics
#> 
#>     A   B
#> A 231  32
#> B  27  54
#> 
#>                Accuracy : 0.8285
#>                  95% CI : (0.7844, 0.8668)
#>     No Information Rate : 0.75
#>     P-Value [Acc > NIR] : 0.0003097
#> 
#>                   Kappa : 0.5336
#> 
#>  Mcnemar's Test P-Value : 0.6025370
#> 
#>             Sensitivity : 0.8953
#>             Specificity : 0.6279
#>          Pos Pred Value : 0.8783
#>          Neg Pred Value : 0.6667
#>               Precision : 0.8783
#>                  Recall : 0.8953
#>                      F1 : 0.8868
#>              Prevalence : 0.7500
#>          Detection Rate : 0.6715
#>    Detection Prevalence : 0.7645
#>       Balanced Accuracy : 0.7616
#> 
#>        'Positive' Class : A

0

如果有人遇到和我一样的问题,caret中的confusionMatrix()方法确实会给出敏感性/特异性。 但是,如果它被提供了一个train类型的对象,它将运行另一种方法confusionMatrix.train(),该方法没有这些信息。

解决方案是手动从train对象中提供datareference(即分别为$pred$pred$$pred$obs),并将其提供给confusionMatrix()方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接