Caret 包 - 定义 Positive 结果

Caret package - defining Positive result

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

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

谢谢你

看看这个例子。使用 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 ...

因为异常是第一级,所以这会是默认的正值class

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 中即可。注意与之前输出的差异,差异开始出现在灵敏度和其他计算中。

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 

改变正数Class:

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

例如: 在乳腺癌 Wisconsin 数据集中,Diagnosis 的默认级别是默认 Positive Class 的基础。 Diagnosis的参考水平是:

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

执行测试训练拆分和模型后 fit.The 产生的混淆矩阵和性能指标为:

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 

需要注意的是**阳性Class是良性的

要将阳性 Class 更改为“恶性”,可以使用 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

此处阳性class是恶性