【问题标题】:caret package confusion matrix define positive case with multiple classes插入符号包混淆矩阵定义具有多个类的正例
【发布时间】:2019-05-11 21:54:28
【问题描述】:

来自 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.

这听起来像是可以在多类问题中定义一个正例,从而得到一个经典的二元混淆矩阵,其中正(已定义类)与负(所有其他类)。但是,当在多类数据上使用正属性时,它不会改变混淆矩阵的输出。

# 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

我只是误解了文档还是真的有办法获得二进制矩阵? 我知道,我可以自己产生所需的输出,但如果有机会偷懒,我会接受它。

【问题讨论】:

    标签: r classification r-caret


    【解决方案1】:

    看起来像是一个误解。碰巧当有两个以上的类时,positive 不会在任何地方使用。首先caret:::confusionMatrix.default 被要求办理一些“手续”,然后我们去caret:::confusionMatrix.table。当有两个类时,positive 会被多次使用,但在 if 情况之外什么都没有。

    正如您所说,手动实现这一点并不难。为了快速浏览,您可以简单地使用

    table(data.frame(data == "A"))
    #         modeled
    # measured FALSE TRUE
    #    FALSE    30   40
    #    TRUE      0   30
    

    其中ATRUE 对应于正类,FALSE 对应于其他所有类。

    【讨论】:

      猜你喜欢
      • 2013-11-21
      • 1970-01-01
      • 2018-09-07
      • 2017-03-20
      • 2019-08-01
      • 2021-04-16
      • 1970-01-01
      • 2014-07-16
      • 1970-01-01
      相关资源
      最近更新 更多