【问题标题】:How to do unstratified (shuffled) k-fold cross validation using R?如何使用 R 进行非分层(混洗)k 折交叉验证?
【发布时间】:2021-11-30 13:38:43
【问题描述】:

我想在不使用分层的情况下将我的数据分成 k 个训练折叠(但可以选择使用改组)。如何在 R 中实现这一目标?到目前为止,我发现的所有线程都与分层 k 折叠有关,这不是我想要的。可能与此CrossValidated thread 有关。在 Python 中与此等效的是使用 sklearn.model_selection.KFold

【问题讨论】:

    标签: r machine-learning cross-validation


    【解决方案1】:

    你可以试试我的轻量级包splitTools

    根据你的用例,你会做类似的事情

    library(splitTools)
    
    # invert = TRUE to get out-of-sample folds
    insample_folds <- create_folds(
      1:nrow(iris), k = 10, type = "basic", seed = 1, shuffle = TRUE
    )
    
    # Loop over insample folds to get RMSE per fold
    rmses <- numeric(0)
    for (fold in insample_folds) {
      fit <- lm(Sepal.Length ~ ., data = iris[fold, ])
      valid_errors <- iris$Sepal.Length[-fold] - predict(fit, iris[-fold, ])
      rmses <- c(rmses, sqrt(mean(valid_errors^2)))
    }
    
    mean(rmses) # 0.312
    

    【讨论】:

      猜你喜欢
      • 2017-02-06
      • 2020-02-06
      • 2017-05-04
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 2018-08-29
      • 1970-01-01
      • 2016-01-15
      相关资源
      最近更新 更多