【问题标题】:How to Generate Confusion Matrix on HOLD OUT sample in "caret-xgbDART"?如何在“caret-xgbDART”中的 HOLD OUT 样本上生成混淆矩阵?
【发布时间】:2019-03-14 11:12:20
【问题描述】:

我正在使用“xgbDART”方法来训练我在caret 中提供的模型。采样方法是“repeatedcv”。

是否可以生成内部保留样本的混淆矩阵?我认为像“rf”算法那样打印最终模型会生成它,但它不会。任何建议都会有所帮助。

【问题讨论】:

    标签: r r-caret


    【解决方案1】:

    要在插入符号训练后获得混淆矩阵,只需在生成的火车 object 上调用 caret::confusionMatrix。以下是声纳数据的示例:

    library(mlbench)
    library(caret)
    library(xgboost)
    data(Sonar)
    ctrl <- trainControl(method = "repeatedcv", 
                         number = 2,
                         repeats = 2)
    
    
    grid <- expand.grid(max_depth = 5,
                        nrounds = 500,
                        eta =  .01,
                        colsample_bytree = 0.7,
                        gamma = 0.1,
                        min_child_weight = 1,
                        subsample = .6,
                        rate_drop = c(.1, .3),
                        skip_drop = c(.1, .3))
    
    
    fit.dart <- train(Class ~ .,
                      data =  Sonar, 
                      method = "xgbDART", 
                      metric = "Accuracy",
                      trControl = ctrl, 
                      tuneGrid = grid)
    
    confusionMatrix(fit.dart)
    #output
    Cross-Validated (2 fold, repeated 2 times) Confusion Matrix 
    
    (entries are percentual average cell counts across resamples)
    
              Reference
    Prediction    M    R
             M 44.5 13.7
             R  8.9 32.9
    
     Accuracy (average) : 0.774
    

    为了创建自定义混淆矩阵(例如,使用自定义阈值且无需对重新采样进行平均,可以在trainControl 中设置classProbs = TRUEsavePredictions = TRUE

    现在例如使用 0.3 的截止阈值和合并的保留数据:

    confusionMatrix(fit.dart$pred$obs,
                    factor(ifelse(fit.dart$pred$R > 0.3, "R", "M"), levels = c("M", "R")))
    #output
    Confusion Matrix and Statistics
    
              Reference
    Prediction   M   R
             M 106 116
             R   8 186
    
                   Accuracy : 0.7019          
                     95% CI : (0.6554, 0.7455)
        No Information Rate : 0.726           
        P-Value [Acc > NIR] : 0.8753          
    
                      Kappa : 0.4214          
     Mcnemar's Test P-Value : <2e-16          
    
                Sensitivity : 0.9298          
                Specificity : 0.6159          
             Pos Pred Value : 0.4775          
             Neg Pred Value : 0.9588          
                 Prevalence : 0.2740          
             Detection Rate : 0.2548          
       Detection Prevalence : 0.5337          
          Balanced Accuracy : 0.7729          
    
           'Positive' Class : M   
    

    【讨论】:

    • 您好@missuse,非常感谢您的建议。在“交叉验证”中,算法在内部保留了训练数据的一倍用于内部验证,对吗?我正在谈论那个。就像在随机森林中一样,如果你 print(model$finalModel) 你得到它。如果可能的话,我试图生成类似的矩阵。
    • 在 k 折交叉验证 (cv) 中,训练集被分成 k 个部分,并且对于每个部分,在没有它的情况下执行一个训练实例并对其进行评估。这样,所有训练数据都用于模型未见过的数据的训练和评估。在重复的 k-fold cv 中,这会重复几次以减少由于特定数据拆分导致的偏差,这些数据拆分有时可能是乐观的,有时是悲观的。如果您正在寻找特定的输出,最好使用可重现的示例和您的期望来更新问题。
    猜你喜欢
    • 2021-04-12
    • 2018-03-30
    • 2021-04-15
    • 2017-10-21
    • 1970-01-01
    • 2019-01-12
    • 2016-02-05
    • 2021-07-29
    • 2016-07-18
    相关资源
    最近更新 更多