R语言中的四折嵌套重复交叉验证

我需要进行四折嵌套重复交叉验证来训练模型。我编写了以下代码,其中包含了内部交叉验证,但现在我正在努力创建外部验证。

fitControl <- trainControl(## 10-fold CV                           method = "repeatedcv",                           number = 10,                           ## repeated five times                           repeats = 5,                           savePredictions = TRUE,                           classProbs = TRUE,                           summaryFunction = twoClassSummary)model_SVM_P <- train(Group ~ ., data = training_set,                  method = "svmPoly",                  trControl = fitControl,                 verbose = FALSE,                 tuneLength = 5)

我尝试解决这个问题:

ntrain=length(training_set)    train.ext=createFolds(training_set,k=4,returnTrain=TRUE)test.ext=lapply(train.ext,function(x) (1:ntrain)[-x])for (i in 1:4){    model_SVM_P <- train(Group ~ ., data = training_set[train.ext[[i]]],                  method = "svmRadial",                  trControl = fitControl,                 verbose = FALSE,                 tuneLength = 5)     }

但这并没有奏效。我该如何进行这个外部循环呢?


回答:

rsample包已经在nested_cv()函数中实现了外部循环,详见文档

要评估由nested_cv训练的模型,请查看这个示例,它展示了“重负荷”工作的具体位置:

# `object` is an `rsplit` object in `results$inner_resamples` summarize_tune_results <- function(object) {  # Return row-bound tibble that has the 25 bootstrap results  map_df(object$splits, tune_over_cost) %>%    # For each value of the tuning parameter, compute the     # average RMSE which is the inner bootstrap estimate.     group_by(cost) %>%    summarize(mean_RMSE = mean(RMSE, na.rm = TRUE),              n = length(RMSE),              .groups = "drop")}tuning_results <- map(results$inner_resamples, summarize_tune_results)

这段代码对每个超参数和训练数据的每个分割(或折叠)应用tune_over_cost函数,这里称为“评估数据”。

请查看示例以获取更多有用的代码,包括并行处理。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注