【问题标题】:Is it possible to retrieve false positive and false negative from confusion matrix in R?是否可以从 R 中的混淆矩阵中检索误报和误报?
【发布时间】:2017-07-24 20:10:14
【问题描述】:

我使用 R 生成了一个混淆矩阵,如下所示。

是否可以从此矩阵中检索 61 的假负值并将其分配给 R 中的变量? $byClass 似乎不适用于这种情况。 谢谢。

Confusion Matrix and Statistics

              Reference
    Prediction   no  yes
           no  9889   61
           yes    6   44

               Accuracy : 0.9933          
                 95% CI : (0.9915, 0.9948)
    No Information Rate : 0.9895          
    P-Value [Acc > NIR] : 4.444e-05       

                  Kappa : 0.5648          
 Mcnemar's Test P-Value : 4.191e-11       

            Sensitivity : 0.9994          
            Specificity : 0.4190          
         Pos Pred Value : 0.9939          
         Neg Pred Value : 0.8800          
             Prevalence : 0.9895          
         Detection Rate : 0.9889          
   Detection Prevalence : 0.9950          
      Balanced Accuracy : 0.7092          

       'Positive' Class : no     

【问题讨论】:

标签: r r-caret confusion-matrix


【解决方案1】:

您尚未提供可重现的示例或在您的代码中加载任何包,但看起来您正在使用来自caret 包的confusionMatrix。这是一个通用示例:

library(caret)

# Fake data
dat = data.frame(measured=rep(0:1, c(40,60)), modeled=rep(c(0:1,0:1), c(30,10,20,40)))

# Generate confusion matrix
cm = confusionMatrix(dat$modeled, dat$measured, positive="1")

cm
Confusion Matrix and Statistics

          Reference
Prediction  0  1
         0 30 20
         1 10 40

               Accuracy : 0.7             
                 95% CI : (0.6002, 0.7876)
    No Information Rate : 0.6             
    P-Value [Acc > NIR] : 0.02478         

                  Kappa : 0.4             
 Mcnemar's Test P-Value : 0.10035         

            Sensitivity : 0.6667          
            Specificity : 0.7500          
         Pos Pred Value : 0.8000          
         Neg Pred Value : 0.6000          
             Prevalence : 0.6000          
         Detection Rate : 0.4000          
   Detection Prevalence : 0.5000          
      Balanced Accuracy : 0.7083          

       'Positive' Class : 1

cm其实是一个列表,我们来看看它包含什么:

str(cm)

List of 6
 $ positive: chr "1"
 $ table   : 'table' int [1:2, 1:2] 30 10 20 40
  ..- attr(*, "dimnames")=List of 2
  .. ..$ Prediction: chr [1:2] "0" "1"
  .. ..$ Reference : chr [1:2] "0" "1"
 $ overall : Named num [1:7] 0.7 0.4 0.6 0.788 0.6 ...
  ..- attr(*, "names")= chr [1:7] "Accuracy" "Kappa" "AccuracyLower" "AccuracyUpper" ...
 $ byClass : Named num [1:11] 0.667 0.75 0.8 0.6 0.8 ...
  ..- attr(*, "names")= chr [1:11] "Sensitivity" "Specificity" "Pos Pred Value" "Neg Pred Value" ...
 $ mode    : chr "sens_spec"
 $ dots    : list()
 - attr(*, "class")= chr "confusionMatrix"

看起来cm$table 有实际的混淆矩阵:

cm$table
          Reference
Prediction  0  1
         0 30 20
         1 10 40

所以误报的计数是:

cm$table[2,1]
[1] 10

【讨论】:

  • 谢谢。这正是我想要的。
猜你喜欢
  • 1970-01-01
  • 2021-12-12
  • 2020-08-21
  • 2018-10-10
  • 2019-07-26
  • 2017-10-17
  • 2021-07-29
  • 2019-05-26
  • 2020-10-20
相关资源
最近更新 更多