【问题标题】:Caret package - defining Positive result插入符号包 - 定义正面结果
【发布时间】:2016-01-30 15:00:47
【问题描述】:

在使用 Caret 包进行机器学习时,我对 Caret 默认的“正”结果选择感到震惊,即二元分类问题中结果因素的第一级。

包装上说可以设置为替代级别。任何机构可以帮助我定义积极的结果吗?

谢谢你

【问题讨论】:

  • 在混淆矩阵中,您可以将“正”参数设置为您选择的类别:confusionMatrix(data = preds, reference = actual, positive="YOUR_POSITIVE_CLASS")
  • Caret 的“积极问题”的一般解决方案当然是重新调整因子。这可能会在其他地方引起其他类型的痛苦,但它很容易做到:只需将 DV 提供给 factor() 并使用 levels 订购的参数,例如c("Positive", "Negative")(如果它还不是一个因素,那么应该是 labelslevels 应该是 1:0 或任何将当前正值放在首位的东西)。

标签: r r-caret


【解决方案1】:

看看这个例子。使用confusionMatrix 从插入符号示例扩展了这一点。

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)

str(truth)
Factor w/ 2 levels "abnormal","normal": 2 2 2 2 2 2 2 2 2 2 ...

因为异常是第一级,所以这将是默认的正类

confusionMatrix(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          
      Balanced Accuracy : 0.7616          

       'Positive' Class : abnormal     

要更改为正 class= 正常,只需将其添加到混淆矩阵中。注意与先前输出的差异,差异开始出现在灵敏度和其他计算中。

confusionMatrix(xtab, positive = "normal")

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.6279          
            Specificity : 0.8953          
         Pos Pred Value : 0.6667          
         Neg Pred Value : 0.8783          
             Prevalence : 0.2500          
         Detection Rate : 0.1570          
   Detection Prevalence : 0.2355          
      Balanced Accuracy : 0.7616          

       'Positive' Class : normal 

【讨论】:

  • 感谢优雅的解决方案。
【解决方案2】:

改变正类:

其中一种熟练的方法是重新调整目标变量。

例如: 在威斯康星乳腺癌数据集中,默认的诊断级别是默认正类的基础。诊断的参考水平是:

cancer<-read.csv("breast-cancer-wisconsin.csv")
cancer$Diagnosis<-as.factor(cancer$Diagnosis)
levels(cancer$Diagnosis)
[1] "Benign"    "Malignant"

在执行测试训练拆分和模型拟合之后。得到的混淆矩阵和性能指标是:

Confusion Matrix and Statistics

predicted        Actual
             Benign Malignant
Benign       115         7
Malignant      2        80
                                      
           Accuracy : 0.9559          
             95% CI : (0.9179, 0.9796)
No Information Rate : 0.5735          
P-Value [Acc > NIR] : <2e-16                          
              Kappa : 0.9091                 
 Mcnemar's Test P-Value : 0.1824                  
        Sensitivity : 0.9829          
        Specificity : 0.9195          
     Pos Pred Value : 0.9426          
     Neg Pred Value : 0.9756          
         Prevalence : 0.5735          
     Detection Rate : 0.5637          
Detection Prevalence: 0.5980  
Balanced Accuracy   : 0.9512
'Positive' Class    : Benign 

注意**正类是良性的"

可以使用relevel() 函数将正类更改为“恶性”。 relevel() 改变了变量的引用级别。

cancer$Diagnosis <- relevel(cancer$Diagnosis, ref = "Malignant")
levels(cancer$Diagnosis)
[1] "Malignant" "Benign"

再次执行测试训练拆分和模型拟合后,混淆矩阵性能精度与参考的变化是:

Confusion Matrix and Statistics

   predicted        Actual
               Malignant Benign
  Malignant        80      2
  Benign            7    115
                                      
           Accuracy : 0.9559          
             95% CI : (0.9179, 0.9796)
No Information Rate : 0.5735          
P-Value [Acc > NIR] : <2e-16                                   
          Kappa : 0.9091                               
 Mcnemar's Test P-Value : 0.1824                               
        Sensitivity : 0.9195          
        Specificity : 0.9829          
     Pos Pred Value : 0.9756          
     Neg Pred Value : 0.9426          
         Prevalence : 0.4265          
     Detection Rate : 0.3922          
Detection Prevalence : 0.4020          
Balanced Accuracy : 0.9512                                   
'Positive' Class : Malignant

这里的正类是恶性的

【讨论】:

    猜你喜欢
    • 2016-10-06
    • 1970-01-01
    • 2016-11-22
    • 2017-12-26
    • 2019-05-11
    • 1970-01-01
    • 2018-02-22
    • 2020-04-26
    • 1970-01-01
    相关资源
    最近更新 更多