首页 文章

检索插入符号中的保持折叠的预测

提问于
浏览
1

我想知道如何恢复交叉验证预测 . 我有兴趣手动构建堆叠模型(like here in point 3.2.1),我需要模型的每个保持折叠的预测 . 我附上一个简短的例子 .

# load the library
library(caret)
# load the iris dataset
data(cars)
# define folds
cv_folds <- createFolds(cars$Price, k = 5, list = 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)
# looking at predictions
model$pred

# verifying the number of observations
nrow(model$pred[model$pred$Resample == "Fold1",])
nrow(cars)

我想知道在折叠1-4上估计模型和在折叠5等上评估的预测是什么 . 看看 model$pred 似乎没有给我我需要的东西 .

1 回答

  • 2

    当使用 createFolds 函数创建的折叠在插入符号中执行CV时,默认使用列车索引 . 所以当你这样做时:

    cv_folds <- createFolds(cars$Price, k = 5, list = TRUE)
    

    你收到了火车套装折叠

    lengths(cv_folds)
    #output
    Fold1 Fold2 Fold3 Fold4 Fold5 
      161   160   161   160   162
    

    每个包含20%的数据

    然后你在trainControl中指定了这些折叠:

    train_control <- trainControl(method="cv", index = cv_folds, savePredictions = 'final')
    

    trainControl 的帮助下:

    index - 包含每个重新采样迭代的元素的列表 . 每个列表元素是一个整数向量,对应于在该迭代中用于训练的行 . indexOut - 一个列表(与索引长度相同),指示每个重采样保留哪些数据(作为整数) . 如果为NULL,则使用索引中未包含的唯一样本集 .

    因此,每次模型构建在160行上并在其余部分上进行验证 . 这就是为什么

    nrow(model$pred[model$pred$Resample == "Fold1",])
    

    返回643

    你应该做的是:

    cv_folds <- createFolds(cars$Price, k = 5, list = TRUE, returnTrain = TRUE)
    

    现在:

    lengths(cv_folds)
    #output
    Fold1 Fold2 Fold3 Fold4 Fold5 
      644   643   642   644   643
    

    并在训练模型后:

    nrow(model$pred[model$pred$Resample == "Fold1",])
    #output
    160
    

相关问题