【发布时间】:2018-09-19 11:18:11
【问题描述】:
在模型调优中使用交叉验证,我从caret::train 的results 对象中得到不同的错误率,并自己在其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 日创建。
【问题讨论】: