从混乱中获取"Accuracy (average)" R 中的矩阵



我有以下混淆矩阵:

confusionMatrix(test_gbm)
Cross-Validated (60 fold) Confusion Matrix 
(entries are percentual average cell counts across resamples)
Prediction    A    B    C    D    E
Reference A 28.0  0.6  0.0  0.0  0.0
          B  0.3 18.3  0.6  0.1  0.2
          C  0.1  0.5 16.6  0.5  0.2
          D  0.0  0.0  0.2 15.7  0.2
          E  0.0  0.0  0.0  0.1 17.8
      Accuracy (average) : 0.9635

我想以编程方式提取准确性。

这是str

数据
List of 4
 $ table: table [1:5, 1:5] 27.9941 0.2905 0.1019 0.0408 0.0102 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ Prediction: chr [1:5] "A" "B" "C" "D" ...
  .. ..$ Reference : chr [1:5] "A" "B" "C" "D" ...
 $ norm : chr "overall"
 $ B    : int 60
 $ text : chr "Cross-Validated (60 fold) Confusion Matrix"
  - attr(*, "class")= chr [1:2] "confusionMatrix.train" "confusionMatrix.train.formula"

我似乎无法弄清楚这是存储在哪里的。

在这种情况下,精度不会存储在confusionMatrix对象中,而是根据混淆矩阵本身动态计算的。不过,您也可以自己计算精度,因为它只是混淆矩阵对角线元素的总和。

假设您将混淆矩阵存储在名为 cm 的对象中。那么精度是:

sum(diag(cm$table))/100

或者,在不存储混淆矩阵对象的情况下:

sum(diag(confusionMatrix(test_gbm)$table))/100

最新更新