有没有工具/ R包可用于计算混淆矩阵的准确性和精度?
公式和数据结构在这里。
有没有工具/ R包可用于计算混淆矩阵的准确性和精度?
公式和数据结构在这里。
是的,您可以使用Caret package在R中计算混淆矩阵中的准确度和精度。
以下是示例:
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
所以这里是你想要的一切。
@Harsh Trivedi
byClass可以帮助你从摘要中提取出准确率和召回率。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值),那么接下来就是它。
f_measure <- 2 * ((precision * recall) / (precision + recall))
我还发现了这个方便的在线计算器,可以用来进行审查。 http://www.marcovanetti.com/pages/cfmatrix/?noc=2
-bg
confusionMatrix(..., mode = "everything")
,请参见下面的答案。 - undefinedconfusionMatrix()
输出,并意识到您可以直接从result$ByClass
输出中获取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
中的平衡准确率
result$byClass
,使用confusionMatrix
中的mode
参数更加方便,可以直接打印出F1分数、召回率和精确度。confusionMatrix(..., mode = "sens_spec")
,会显示灵敏度和特异度。要显示F1分数、召回率和精确度,使用mode = "prec_recall"
。要显示所有指标,使用mode = "everything"
。library(caret)
x <- matrix(c(231, 27, 32, 54), nrow = 2)
x
#> [,1] [,2]
#> [1,] 231 32
#> [2,] 27 54
confusionMatrix(x, mode = "everything")
#> Confusion Matrix and Statistics
#>
#> A B
#> A 231 32
#> B 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
#> Precision : 0.8783
#> Recall : 0.8953
#> F1 : 0.8868
#> Prevalence : 0.7500
#> Detection Rate : 0.6715
#> Detection Prevalence : 0.7645
#> Balanced Accuracy : 0.7616
#>
#> 'Positive' Class : A
如果有人遇到和我一样的问题,caret
中的confusionMatrix()
方法确实会给出敏感性/特异性。 但是,如果它被提供了一个train
类型的对象,它将运行另一种方法confusionMatrix.train()
,该方法没有这些信息。
解决方案是手动从train
对象中提供data
和reference
(即分别为$pred$pred$
和$pred$obs
),并将其提供给confusionMatrix()
方法。