代码之家  ›  专栏  ›  技术社区  ›  abu

从插入符号到指定给不同折叠的交叉验证预测

  •  0
  • abu  · 技术社区  · 7 年前

    我想知道为什么来自“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 回复  |  直到 7 年前
        1
  •  2
  •   phiver    7 年前

    的重采样数据 'Fold1' 是不在中的记录 cv_folds[[1]] . 这些记录包含在 cv_folds 2-5. 这是正确的,因为您正在运行5倍交叉验证。对褶皱1进行重采样,并对褶皱2-5上的模型进行训练。对褶皱2进行重采样,并对褶皱1、3-5等进行训练。

    总结:中的预测 Fold1 是在cv\u褶皱2-5上训练模型的测试预测。

    编辑:基于注释

    所有需要的信息都在model$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 rowIndex model$pred表中的列。

    rowIndex引用原始数据中的行。所以rowIndex 610指的是cars数据集中的记录610。您可以比较obs中的数据,obs是cars数据集中价格列的值。