我想知道为什么来自'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 回答
'Fold1'
的重采样数据是不在cv_folds[[1]]
中的记录 . 这些记录包含在cv_folds
2-5中 . 这是正确的,因为您正在运行5倍交叉验证 . 测试重新折叠折叠1以在折叠2-5上训练模型 . 在折叠1,5-5等方面对重新取样折叠2进行针对训练的测试 .总结:
Fold1
中的预测是在cv_folds 2-5上训练模型的测试预测 .Edit: based on comment
所有需要的信息都在模型$ pred表中 . 我添加了一些代码来澄清:
基本上,进一步堆叠预测所需的是来自模型$ pred表的
pred
和rowIndex
列 .rowIndex引用原始数据中的行 . 因此,rowIndex 610引用汽车数据集中的记录610 . 您可以比较obs中的数据,即cars数据集中Price列的值 .