来自插入符号的交叉验证预测被分配到不同的折叠

时间:2018-01-26 09:11:03

标签: r cross-validation r-caret

我想知道为什么来自' Fold1'实际上是我预定义折叠中第二个折叠的预测。我附上一个我的意思的例子。

# load the library
library(caret)
# load the cars dataset
data(cars)
# define folds
cv_folds <- createFolds(cars$Price, k = 5, list = TRUE, returnTrain = TRUE)
# define training control
train_control <- trainControl(method="cv", index = cv_folds, savePredictions = 'final')
# fix the parameters of the algorithm
# train the model
model <- caret::train(Price~., data=cars, trControl=train_control, method="gbm", verbose = F)

model$pred$rowIndex[model$pred$Resample == 'Fold1'] %in% cv_folds[[2]]

1 个答案:

答案 0 :(得分:2)

'Fold1'的重新抽样数据是不在cv_folds[[1]]中的记录。这些记录包含在cv_folds 2-5中。这是正确的,因为您正在运行5倍交叉验证。测试重新折叠折叠1以在折叠2-5上训练模型。在折叠1,5-5等方面对重新取样折叠2进行针对训练的测试。

总结:Fold1中的预测是在cv_folds 2-5上训练模型的测试预测。

修改:根据评论

所有需要的信息都在模型$ pred表中。我添加了一些代码来澄清:

model$pred %>% 
  select(rowIndex, pred, Resample) %>%
  rename(predection = pred, holdout = Resample) %>% 
  mutate(trained_on = case_when(holdout == "Fold1" ~ "Folds 2, 3, 4, 5",
                                holdout == "Fold2" ~ "Folds 1, 3, 4, 5", 
                                holdout == "Fold3" ~ "Folds 1, 2, 4, 5", 
                                holdout == "Fold4" ~ "Folds 1, 2, 3, 5", 
                                holdout == "Fold5" ~ "Folds 1, 2, 3, 4"))

  rowIndex predection holdout       trained_on
1      610   13922.60   Fold2 Folds 1, 3, 4, 5
2      623   38418.83   Fold2 Folds 1, 3, 4, 5
3      604   12383.55   Fold2 Folds 1, 3, 4, 5
4      607   15040.07   Fold2 Folds 1, 3, 4, 5
5       95   33549.40   Fold2 Folds 1, 3, 4, 5
6      624   40357.35   Fold2 Folds 1, 3, 4, 5

基本上,进一步堆叠预测所需的是模型$ pred表中的predrowIndex列。

rowIndex引用原始数据中的行。因此,rowIndex 610引用汽车数据集中的记录610。您可以比较obs中的数据,这是来自汽车数据集的价格列的值。

相关问题