首页 > 解决方案 > 在 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.RepNFold10.Rep3

我想我可以为每个保留执行此交叉检查重新计算RMSEs (因为这是 s 的决定性指标finalModel),然后将结果与tt.cv$resamples$RMSE. 所以我会看到一行我重新计算RMSEtt.cv$resample$RMSE将相等,这意味着该特定模型被选为我的finalModel.

但是,当我重新计算RMSE每个保留的所有 s 时,我没有看到我的计算和tt.cv$resample$RMSE相等的任何行。您可以在下面看到比较:

RMSERMSEcolumn 具有我从中获得的实际值,tt.cv$resamples$RMSE并且rmse_hocolumn 具有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))

标签: rmachine-learningcross-validationr-caret

解决方案


推荐阅读