如何解释 R 中的混淆矩阵

How to interpret confusion matrix in R

我正在处理混淆矩阵并且对输出有非常基本的了解。然而,由于我不熟悉使用它和 R,所以细节解释常常使它听起来更复杂。我有以下输出,我只是想知道是否可以向我解释一下

假设这是你的混淆矩阵:

tab = structure(list(A = c(2298L, 264L, 5L, 1L, 0L, 0L, 0L), B = c(174L, 
3245L, 193L, 1L, 0L, 0L, 0L), C = c(63L, 301L, 2958L, 39L, 2L, 
0L, 0L), D = c(29L, 65L, 399L, 1074L, 32L, 0L, 0L), E = c(26L, 
16L, 61L, 236L, 249L, 8L, 0L), F = c(12L, 3L, 15L, 33L, 97L, 
21L, 0L), G = c(3L, 3L, 4L, 6L, 30L, 11L, 0L)), class = "data.frame", row.names = c("A", 
"B", "C", "D", "E", "F", "G"))
  1. Matrix 中的 TP、TN、FP 和 FN 是什么?

您需要按照每个标签进行操作,例如对于 class A,这些术语在对 A 的预测方面是有意义的。

A_confusion_matrix = cbind(c(x[1,1],sum(x[-1,1])),c(sum(x[1,-1]),sum(x[2:7,2:7])))

     [,1] [,2]
[1,] 2298  307
[2,]  270 9102

上面的计算方式基本上是将所有预测和参考都错误地混为一谈,而不是 A。

而这些数字代表:

A_confusion_matrix[1,1] is number that are predicted A and truly A -> TP

A_confusion_matrix[1,2] is the number that are predicted A but not A -> FP

A_confusion_matrix[2,1] is the number that are not predicted A but A -> FN

A_confusion_matrix[2,2] is the number that are not predicted A and not A -> TN

例如,您可以从此处计算 A 的灵敏度,即 TP/(TP+FN) = 2298/(2298+270) = 0.8948598

  1. 河童代表什么?

它是 cohen's kappa,基本上是衡量您的预测与随机猜测/分配相比有多好的指标。

  1. accuracy 和 kappa 有什么区别?

从上面的公式可以看出,当你的数据集不平衡时,它会产生巨大的差异。例如,如果 90% 的标签是一个 class,如果模型预测所有内容都是 class,那么您将获得 90% 的准确率。但是,如果您使用 cohen 的 kappa,开始时 p 预期为 0.9,您需要做得更好才能显示良好的分数。