【问题标题】:Sensitivity/Specificity in Random Forest output随机森林输出的敏感性/特异性
【发布时间】:2021-09-18 04:51:09
【问题描述】:

我在 R 中创建了一个随机森林模型。我的结果变量是“保留”,其中 1=retained 和 0=left,我在实际数据中遇到了案例不平衡的问题(0 比 1 多),这已在我的训练数据集的混淆矩阵中显示。根据我的手动计算,Sensitivity 应该是 0.05,Specificity 应该是 0.67,这和 case 不平衡问题是一致的。但是,输出中的数字完全不同。以下是控制台中的代码和输出(rf 是我的随机森林模型):

retain_p <- rf %>% 
  predict(newdata = testing)

table(
  actualclass = testing$retain,
  predictedclass = retain_p
) %>% 
  confusionMatrix() %>% 
  print()
Confusion Matrix and Statistics

           predictedclass
actualclass    0    1
          0 1870   36
          1  911   47
                                          
               Accuracy : 0.6693          
                 95% CI : (0.6518, 0.6866)
    No Information Rate : 0.971           
    P-Value [Acc > NIR] : 1               
                                          
                  Kappa : 0.039           
                                          
 Mcnemar's Test P-Value : <2e-16          
                                          
            Sensitivity : 0.67242         
            Specificity : 0.56627         
         Pos Pred Value : 0.98111         
         Neg Pred Value : 0.04906         
             Prevalence : 0.97102         
         Detection Rate : 0.65293         
   Detection Prevalence : 0.66550         
      Balanced Accuracy : 0.61934         
                                          
       'Positive' Class : 0 

【问题讨论】:

    标签: r random-forest


    【解决方案1】:

    在confusionMatrix命令中,你必须指定什么值进入“正”。

    retain_p <- rf %>% 
      predict(newdata = testing)
    
    table(
      actualclass = testing$retain,
      predictedclass = retain_p
    ) %>% 
      confusionMatrix(positive='1') %>% 
      print()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-21
      • 2021-05-09
      • 2021-08-29
      • 2020-05-26
      • 2019-02-26
      • 2016-06-24
      • 2013-02-06
      • 2015-05-12
      相关资源
      最近更新 更多