如何从R中的confusionMatrix检索整体精度值?

use*_*911 10 r confusion-matrix r-caret

在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
Run Code Online (Sandbox Code Playgroud)

总体统计

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

               Kappa : 0.99          
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Bor*_*lik 22

给定混淆矩阵cm,可以获得整体精度overall.accuracy <- cm$overall['Accuracy']

这是我第一次看到caret包裹,所以我怎么知道这个?

由于您没有提供示例,我搜索了插入符号混淆矩阵示例代码.这是(我只在最后一个语句中添加了赋值):

###################
## 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))
Run Code Online (Sandbox Code Playgroud)

现在,让我们来看看混淆矩阵中的内容:

> 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"
Run Code Online (Sandbox Code Playgroud)

如您所见,该cm对象是一个列表.我们看到各种"byClass"和"整体"统计数据.整体部分通过以下方式获得:

overall <- cm$overall
Run Code Online (Sandbox Code Playgroud)

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

> overall
      Accuracy          Kappa  AccuracyLower  AccuracyUpper   AccuracyNull AccuracyPValue  McnemarPValue 
     0.2666667     -0.1000000      0.1978421      0.3449492      0.3333333      0.9674672      0.9547790 
Run Code Online (Sandbox Code Playgroud)

现在,提取相关值非常简单:

> overall.accuracy <- overall['Accuracy'] 
Run Code Online (Sandbox Code Playgroud)

总结:str是你的朋友.另一个有用的功能是attributes- 它返回给定对象的所有属性.

  • @Emixam23你现在可能已经找到了问题的解决方案,但是对于正在寻找答案的人来说,只需在上面提到的括号前面和后面添加另一个方括号,就像这样`overall.accuracy &lt;-overall[[ ‘准确度’]]` (2认同)