插入符包混淆矩阵定义多个 类 的正例
caret package confusion matrix define positive case with multiple classes
来自 caret::confusionMatrix 的文档:
positive: an optional character string for the factor level that
corresponds to a "positive" result (if that makes sense for your
data). If there are only two factor levels, the first level will
be used as the "positive" result.
这听起来像是可以在多 class 问题中定义一个正案例,从而得到一个 classic 二进制混淆矩阵,其中正(定义 class)与否定(所有其他 classes)。然而,当在 multiclass 数据上使用 positive 属性时,它不会改变 confusionMatrix.
的输出
# generate fake data
data = data.frame(measured=as.factor(rep(c('A', 'B', 'C'), c(30,40,30))),
modeled=as.factor(rep(c('A', 'B', 'C', 'A'), c(30,10,20,40))))
# get confusion matrix
matrix = caret::confusionMatrix(data$modeled, dat$measured, positive='A')
给予
Confusion Matrix and Statistics
Reference
Prediction A B C
A 30 10 30
B 0 10 0
C 0 20 0
Overall Statistics
Accuracy : 0.4
95% CI : (0.3033, 0.5028)
No Information Rate : 0.4
P-Value [Acc > NIR] : 0.5379
Kappa : 0.1304
Mcnemar's Test P-Value : 5.878e-13
Statistics by Class:
Class: A Class: B Class: C
Sensitivity 1.0000 0.2500 0.0000
Specificity 0.4286 1.0000 0.7143
Pos Pred Value 0.4286 1.0000 0.0000
Neg Pred Value 1.0000 0.6667 0.6250
Prevalence 0.3000 0.4000 0.3000
Detection Rate 0.3000 0.1000 0.0000
Detection Prevalence 0.7000 0.1000 0.2000
Balanced Accuracy 0.7143 0.6250 0.3571
我只是误解了文档,还是真的有办法获得二进制矩阵?
我知道,我可以自己产生所需的输出,但如果有机会偷懒,我会抓住它。
看起来像是误读。碰巧当 class 超过两个时, positive
没有在任何地方使用。首先 caret:::confusionMatrix.default
被调用一些 "formalities" 然后我们去 caret:::confusionMatrix.table
。当有两个 class 时,positive
被多次使用,但在 if
情况之外没有任何东西。
如您所说,手动实现并不难。为了快速浏览,您可以简单地使用
table(data.frame(data == "A"))
# modeled
# measured FALSE TRUE
# FALSE 30 40
# TRUE 0 30
其中 A
和 TRUE
对应正数 class,FALSE
对应其他一切。
来自 caret::confusionMatrix 的文档:
positive: an optional character string for the factor level that
corresponds to a "positive" result (if that makes sense for your
data). If there are only two factor levels, the first level will
be used as the "positive" result.
这听起来像是可以在多 class 问题中定义一个正案例,从而得到一个 classic 二进制混淆矩阵,其中正(定义 class)与否定(所有其他 classes)。然而,当在 multiclass 数据上使用 positive 属性时,它不会改变 confusionMatrix.
的输出# generate fake data
data = data.frame(measured=as.factor(rep(c('A', 'B', 'C'), c(30,40,30))),
modeled=as.factor(rep(c('A', 'B', 'C', 'A'), c(30,10,20,40))))
# get confusion matrix
matrix = caret::confusionMatrix(data$modeled, dat$measured, positive='A')
给予
Confusion Matrix and Statistics
Reference
Prediction A B C
A 30 10 30
B 0 10 0
C 0 20 0
Overall Statistics
Accuracy : 0.4
95% CI : (0.3033, 0.5028)
No Information Rate : 0.4
P-Value [Acc > NIR] : 0.5379
Kappa : 0.1304
Mcnemar's Test P-Value : 5.878e-13
Statistics by Class:
Class: A Class: B Class: C
Sensitivity 1.0000 0.2500 0.0000
Specificity 0.4286 1.0000 0.7143
Pos Pred Value 0.4286 1.0000 0.0000
Neg Pred Value 1.0000 0.6667 0.6250
Prevalence 0.3000 0.4000 0.3000
Detection Rate 0.3000 0.1000 0.0000
Detection Prevalence 0.7000 0.1000 0.2000
Balanced Accuracy 0.7143 0.6250 0.3571
我只是误解了文档,还是真的有办法获得二进制矩阵? 我知道,我可以自己产生所需的输出,但如果有机会偷懒,我会抓住它。
看起来像是误读。碰巧当 class 超过两个时, positive
没有在任何地方使用。首先 caret:::confusionMatrix.default
被调用一些 "formalities" 然后我们去 caret:::confusionMatrix.table
。当有两个 class 时,positive
被多次使用,但在 if
情况之外没有任何东西。
如您所说,手动实现并不难。为了快速浏览,您可以简单地使用
table(data.frame(data == "A"))
# modeled
# measured FALSE TRUE
# FALSE 30 40
# TRUE 0 30
其中 A
和 TRUE
对应正数 class,FALSE
对应其他一切。