【问题标题】:How to retrieve overall accuracy value from confusionMatrix in R?如何从 R 中的混淆矩阵中检索整体准确度值?
【发布时间】:2014-08-12 11:49:30
【问题描述】:

在 R 插入符号库中,如果我得到一个像下面这样的混淆矩阵,是否有办法检索整体准确度 0.992?我无法得到这个单一的值,因为我需要存储这个值并将其用于以后的处理。这可能吗?

 Prediction    A    B    C    D    E
          A 1114    2    0    0    0
          B    9  745    5    0    0
          C    0    6  674    4    0
          D    0    0    3  640    0
          E    0    0    2    1  718

总体统计

            Accuracy : 0.992         
              95% CI : (0.989, 0.994)
 No Information Rate : 0.286         
 P-Value [Acc > NIR] : <2e-16        

               Kappa : 0.99          

Mcnemar 检验 P 值:NA

按类别统计:

                     Class: A Class: B Class: C Class: D Class: E
 Sensitivity             0.992    0.989    0.985    0.992    1.000
 Specificity             0.999    0.996    0.997    0.999    0.999
 Pos Pred Value          0.998    0.982    0.985    0.995    0.996
 Neg Pred Value          0.997    0.997    0.997    0.998    1.000
 Prevalence              0.286    0.192    0.174    0.164    0.183
 Detection Rate          0.284    0.190    0.172    0.163    0.183
 Detection Prevalence    0.284    0.193    0.174    0.164    0.184
 Balanced Accuracy       0.996    0.992    0.991    0.996    1.000

【问题讨论】:

    标签: r r-caret confusion-matrix


    【解决方案1】:

    给定一个混淆矩阵cm,整体准确率由overall.accuracy &lt;- cm$overall['Accuracy']得到

    第一次看到caret这个包,我怎么知道的?

    由于您没有提供示例,我搜索了example code for caret confusion matrices。在这里(我只在最后一条语句中添加了赋值):

    ###################
    ## 3 class example
    
    confusionMatrix(iris$Species, sample(iris$Species))
    
    newPrior <- c(.05, .8, .15)
    names(newPrior) <- levels(iris$Species)
    
    cm <- confusionMatrix(iris$Species, sample(iris$Species))
    

    现在,让我们看看混淆矩阵中有什么:

    > str(cm)
    List of 5
     $ positive: NULL
     $ table   : 'table' int [1:3, 1:3] 13 18 19 20 13 17 17 19 14
      ..- attr(*, "dimnames")=List of 2
      .. ..$ Prediction: chr [1:3] "setosa" "versicolor" "virginica"
      .. ..$ Reference : chr [1:3] "setosa" "versicolor" "virginica"
     $ overall : Named num [1:7] 0.267 -0.1 0.198 0.345 0.333 ...
      ..- attr(*, "names")= chr [1:7] "Accuracy" "Kappa" "AccuracyLower" "AccuracyUpper" ...
     $ byClass : num [1:3, 1:8] 0.26 0.26 0.28 0.63 0.63 0.64 0.26 0.26 0.28 0.63 ...
      ..- attr(*, "dimnames")=List of 2
      .. ..$ : chr [1:3] "Class: setosa" "Class: versicolor" "Class: virginica"
      .. ..$ : chr [1:8] "Sensitivity" "Specificity" "Pos Pred Value" "Neg Pred Value" ...
     $ dots    : list()
     - attr(*, "class")= chr "confusionMatrix"
    

    如您所见,cm 对象是一个列表。我们看到各种“按类别”和“总体”统计数据。整体部分通过以下方式获得:

    overall <- cm$overall
    

    这给了我们一个带有字符串索引的数字向量:

    > overall
          Accuracy          Kappa  AccuracyLower  AccuracyUpper   AccuracyNull AccuracyPValue  McnemarPValue 
         0.2666667     -0.1000000      0.1978421      0.3449492      0.3333333      0.9674672      0.9547790 
    

    现在,提取相关值很简单:

    > overall.accuracy <- overall['Accuracy'] 
    

    总结:str 是你的朋友。另一个有用的函数是attributes——它返回给定对象的所有属性。

    【讨论】:

    • 不错的答案,但准确返回一个字符串和一个值,我怎样才能访问该值?我的意思是双重
    • @Emixam23 您现在可能已经找到了问题的解决方案,但是对于正在寻找答案的人,只需在上述括号的前后添加另一个方括号,就像这样@ 987654333@
    猜你喜欢
    • 2016-08-13
    • 2012-11-12
    • 2021-05-17
    • 2020-02-25
    • 2013-12-02
    • 2018-01-25
    • 2018-08-23
    • 2018-04-16
    • 1970-01-01
    相关资源
    最近更新 更多