r - 在 Caret / R 中返回 train 函数后,如何找出选择了哪个模型?如何交叉检查?
问题描述
我花了很多时间试图理解train
函数是如何工作的,但我仍然认为我不明白它是如何工作的。我正在训练一个neuralnet
使用train
函数来预测时间表。
当我在训练后绘制模型时,我得到以下网络:
在情节中,它说Error = 0.01643
,我刚刚意识到,每当我train
绘制 my时finalModel
,情节中的Error
值总是恰好是函数返回Error
后我得到的最后一条输出消息的值。train
所以我想知道是否选择了最后一个模型,因为最后一个模型没有最低的RMSE
。我假设这些输出消息实际上是有序的,换句话说,在我的情况下,第一个输出hidden: 8 thresh: 0.01 rep: 1/1 steps: 10304 error: 0.01592 time: 1.75 secs
是 from Fold01.Rep1
,最后一个是 from 。FoldK.RepN
Fold10.Rep3
我想我可以为每个保留执行此交叉检查重新计算RMSE
s (因为这是 s 的决定性指标finalModel
),然后将结果与tt.cv$resamples$RMSE
. 所以我会看到一行我重新计算RMSE
和tt.cv$resample$RMSE
将相等,这意味着该特定模型被选为我的finalModel
.
但是,当我重新计算RMSE
每个保留的所有 s 时,我没有看到我的计算和tt.cv$resample$RMSE
相等的任何行。您可以在下面看到比较:
RMSE
RMSE
column 具有我从中获得的实际值,tt.cv$resamples$RMSE
并且rmse_ho
column 具有RMSE
我使用重新计算的值finalModel
。
您能否指出我是否有任何错误以及如何交叉检查选出的模型?
当我创建这篇文章时,我刚刚意识到如果我计算输出实际上有 31 个模型。我在互联网上的某个地方读到过它,但现在找不到了。
这是代码:
library(caret)
library(neuralnet)
# Create the dataset
tt <- data.frame(multiplier = rep(1:10, times = 10), multiplicand = rep(1:10, each = 10))
tt <- cbind(tt, data.frame(product = tt$multiplier * tt$multiplicand))
# Splitting
indexes <- createDataPartition(tt$product,
times = 1,
p = 0.7,
list = FALSE)
tt.train <- tt[indexes,]
tt.test <- tt[-indexes,]
# Pre-process
preProc <- preProcess(tt, method = c('center', 'scale'))
tt.preProcessed <- predict(preProc, tt)
tt.preProcessed.train <- tt.preProcessed[indexes,]
tt.preProcessed.test <- tt.preProcessed[-indexes,]
# Train
train.control <- trainControl(method = "repeatedcv",
number = 10,
repeats = 3,
savePredictions = TRUE)
tune.grid <- expand.grid(layer1 = 8,
layer2 = 0,
layer3 = 0)
# Setting seed for reproducibility & train
set.seed(12)
tt.cv <- train(product ~ .,
data = tt.preProcessed.train,
method = 'neuralnet',
tuneGrid = tune.grid,
trControl = train.control,
algorithm = 'backprop',
learningrate = 0.005,
stepmax = 100000,
lifesign = 'minimal',
threshold = 0.01)
errors = data.frame(rmse_ho=numeric(), resample=character())
# Re-calculate RMSE values for each hold-out using tt.cv$finalModel
for(i in levels(as.factor(tt.cv$pred$Resample))) {
dframe = tt.cv$pred[tt.cv$pred$Resample == as.character(i),]
hold_outs = tt.preProcessed.train[dframe$rowIndex,]
prediction_hold_outs = predict.train(tt.cv, hold_outs)
rmse_hold_out = RMSE(prediction_hold_outs, hold_outs$product)
errors = rbind(errors, data.frame(rmse_ho = rmse_hold_out,
# sse_all = sse_all,
# sse_train = sse_train_data,
resample = i))
}
# View the comparison
View(cbind( tt.cv$resample[order(tt.cv$resample$Resample),], errors))
解决方案
推荐阅读
- reactjs - 为什么是 '/?' 成功后调用后添加到 URL
- c++ - boost beast WebSocket升级握手失败
- python - 从 dict 键的子集创建命名元组
- java - 打开 Eclipse 时出错
- javascript - 附加组搞砸了 d3 强制布局中的所有内容
- javascript - 访问多个嵌套对象
- mysql - MYSQL 数据库位置 Windows 10 PC
- angular6 - StaticInjectorError 在角度 6 中执行 ssr 时,“instanceof”的右侧不是对象问题
- php - 放置一个变量来调出电子邮件正文
- java - Android 在模块发布中包含依赖项