【问题标题】:calculate accuracy and precision of confusion matrix in R计算R中混淆矩阵的准确度和精度
【发布时间】:2012-11-12 22:34:37
【问题描述】:

是否有任何工具/R 包可用于计算 R 中混淆矩阵的准确度和精度?

The formula and data structure are here

【问题讨论】:

标签: r sentiment-analysis confusion-matrix


【解决方案1】:

如果其他人正在寻找:感谢 BGA 上面的回答,我更清楚地了解了如何读取 confusionMatrix() 输出,并意识到您可以直接从 result$ByClass 输出中获取 F 度量作为 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的平衡精度

【讨论】:

    【解决方案2】:

    如果有人遇到和我一样的问题,caret 中的方法confusionMatrix() 确实提供了敏感性/特异性。 然而,如果它被输入一个 train 类型的对象,它将运行一个不同的方法,confusionMatrix.train(),它没有有这个信息。

    解决方案是将data 和reference 手动从train 对象(即分别为$pred$pred$ 和$pred$obs)提供给confusionMatrix() 方法。

    【讨论】:

      【解决方案3】:

      @Harsh Trivedi

      byClass 允许您从摘要中提取precision 和recall。 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_measure <- 2 * ((precision * recall) / (precision + recall))
      

      我还发现了这个方便的在线计算器,用于进行完整性检查。 http://www.marcovanetti.com/pages/cfmatrix/?noc=2

      -bg

      【讨论】:

        【解决方案4】:

        是的,您可以使用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
        

        这里有你想要的一切。

        【讨论】:

        • 在获得confusionMatrix(xtab)的结果后如何以编程方式查找精度和召回率?
        • 谢谢尼修。我只想添加一个额外的信息。混淆矩阵(xtab)依赖于“e1071”包,所以可能需要安装这个包。
        • @Rupesh:是的,这是必需的。
        猜你喜欢
        • 2017-04-05
        • 1970-01-01
        • 2013-12-02
        • 1970-01-01
        • 2022-12-01
        • 2016-08-13
        • 2018-12-06
        • 2021-05-17
        • 2020-08-29
        相关资源
        最近更新 更多