【问题标题】:Out-of-fold vs training error in caret插入符号中的折叠与训练错误
【发布时间】:2018-09-19 11:18:11
【问题描述】:

在模型调优中使用交叉验证,我从caret::trainresults 对象中得到不同的错误率,并自己在其pred 对象上计算错误。我想了解它们为何不同,以及理想情况下如何使用折叠错误率进行模型选择、绘制模型性能等。

pred 对象包含非折叠预测。文档非常清楚trainControl(..., savePredictions = "final") 保存了最佳超参数值的非折叠预测:“一个指示应该保存每个重新采样的保留预测的指标......“最终”保存预测最佳调谐参数。” (保留“所有”预测然后过滤到最佳调整值并不能解决问题。)

train 文档说results 对象是“训练错误率的数据框......”我不确定这是什么意思,但最佳行的值始终与指标不同根据pred 计算。为什么它们不同,我怎样才能让它们对齐?

d <- data.frame(y = rnorm(50))
d$x1 <- rnorm(50, d$y)
d$x2 <- rnorm(50, d$y)
train_control <- caret::trainControl(method = "cv",
                                     number = 4,
                                     search = "random",
                                     savePredictions = "final")
m <- caret::train(x = d[, -1],
                     y = d$y,
                     method = "ranger",
                     trControl = train_control,
                     tuneLength = 3)
#> Loading required package: lattice
#> Loading required package: ggplot2
m
#> Random Forest 
#> 
#> 50 samples
#>  2 predictor
#> 
#> No pre-processing
#> Resampling: Cross-Validated (4 fold) 
#> Summary of sample sizes: 38, 36, 38, 38 
#> Resampling results across tuning parameters:
#> 
#>   min.node.size  mtry  splitrule   RMSE       Rsquared   MAE      
#>   1              2     maxstat     0.5981673  0.6724245  0.4993722
#>   3              1     extratrees  0.5861116  0.7010012  0.4938035
#>   4              2     maxstat     0.6017491  0.6661093  0.4999057
#> 
#> RMSE was used to select the optimal model using the smallest value.
#> The final values used for the model were mtry = 1, splitrule =
#>  extratrees and min.node.size = 3.
MLmetrics::RMSE(m$pred$pred, m$pred$obs)
#> [1] 0.609202
MLmetrics::R2_Score(m$pred$pred, m$pred$obs)
#> [1] 0.642394

reprex package (v0.2.0) 于 2018 年 4 月 9 日创建。

【问题讨论】:

    标签: r r-caret


    【解决方案1】:

    我得到不同的结果。这显然是一个随机过程。

    MLmetrics::RMSE(m$pred$pred, m$pred$obs)
    [1] 0.5824464
    > MLmetrics::R2_Score(m$pred$pred, m$pred$obs)
    [1] 0.5271595
    

    如果您希望随机(更准确地说是可重现的伪随机过程,请在调用之前立即使用 set.seed。

    【讨论】:

    • 问题是关于模型训练集内指标的一致性,而不是训练运行之间的一致性。
    • ranger 有一个 seed 选项会有所帮助(但这里没有人设置)
    【解决方案2】:

    交叉验证的 RMSE 不是按照您显示的方式计算的,而是针对每个折叠计算,然后取平均值。完整示例:

    set.seed(1)
    d <- data.frame(y = rnorm(50))
    d$x1 <- rnorm(50, d$y)
    d$x2 <- rnorm(50, d$y)
    train_control <- caret::trainControl(method = "cv",
                                         number = 4,
                                         search = "random",
                                         savePredictions = "final")
    set.seed(1)
    m <- caret::train(x = d[, -1],
                      y = d$y,
                      method = "ranger",
                      trControl = train_control,
                      tuneLength = 3)
    #output
    Random Forest 
    
    50 samples
     2 predictor
    
    No pre-processing
    Resampling: Cross-Validated (4 fold) 
    Summary of sample sizes: 37, 38, 37, 38 
    Resampling results across tuning parameters:
    
      min.node.size  mtry  splitrule   RMSE       Rsquared   MAE      
       8             1     extratrees  0.6106390  0.4360609  0.4926629
      12             2     extratrees  0.6156636  0.4294237  0.4954481
      19             2     variance    0.6472539  0.3889372  0.5217369
    
    RMSE was used to select the optimal model using the smallest value.
    The final values used for the model were mtry = 1, splitrule = extratrees and min.node.size = 8.
    

    最佳模型的 RMSE 是 0.6106390

    现在计算每个折叠的 RMSE 和平均值:

    m$pred %>%
      group_by(Resample) %>%
      mutate(rmse = caret::RMSE(pred, obs)) %>%
      summarise(mean = mean(rmse)) %>%
      pull(mean) %>%
      mean
    #output
    0.610639
    
    m$pred %>%
      group_by(Resample) %>%
      mutate(rmse = MLmetrics::RMSE(pred, obs)) %>%
      summarise(mean = mean(rmse)) %>%
      pull(mean) %>%
      mean
    #output
    0.610639
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-24
      • 2017-03-09
      • 2017-03-01
      • 2020-10-01
      • 2015-09-17
      • 1970-01-01
      • 2015-09-04
      相关资源
      最近更新 更多