R语言 计算混淆矩阵的准确度和精确度

x6492ojm  于 9个月前  发布在  其他
关注(0)|答案(5)|浏览(150)

是否有任何工具/ R包可用于计算混淆矩阵的准确度和精密度?
公式和数据结构为here

3qpi33ja

3qpi33ja1#

是的,你可以用confusion matrix在R中计算准确度和精度。它使用Caret package
下面是一个例子:

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

你要的东西都在这了。

t1rydlwq

t1rydlwq2#

@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度量 *,所以就这样吧。

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

我还发现了这个方便的在线计算器的健全检查。http://www.marcovanetti.com/pages/cfmatrix/?noc=2
-bg

km0tfn4u

km0tfn4u3#

如果有人和我有同样的问题,caret中的confusionMatrix()方法确实给予灵敏度/特异性。然而,如果它被馈送一个train类型的对象,它将运行一个不同的方法,confusionMatrix.train(),它没有没有这个信息。
解决方案是从train对象(即reference对象)手动馈送datareference$pred$pred$$pred$obs)到confusionMatrix()方法。

vh0rcniy

vh0rcniy4#

如果有人在看:多亏了BGA的上述回答,我更清楚地了解了如何读取confusionMatrix()输出,并意识到您可以从result$ByClass输出中获得F1的F测量值。

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的平衡精度

fae0ux8s

fae0ux8s5#

比自己计算F1分数或使用result$byClass方便得多,您可以使用**confusionMatrix**中的mode参数来打印F1分数,召回率和精度。
默认值为confusionMatrix(..., mode = "sens_spec"),显示灵敏度和特异性。使用mode = "prec_recall"显示F1分数、召回率和精确率。要显示所有这些,请使用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

相关问题