Rtidymodels:如何使用workflow_map()? 我试图将结果从tune_grid()传递到tune_bayes()的参数初始内容中。当直接调用tune_bayes()时(请参见?tune_bayes中的示例)。但是,我看不出我是如何ca的...

问题描述 投票:0回答:0
的参数

tune_bayes()

。直接调用
tune_bayes()
(请参见“ tune_bayes”中的示例)。但是,我看不出如何使用
initial
时如何通过
workflow_map()
参数?我收到错误消息:
eRror in
check_initial()
: 呢

initial
应该是一个正整数或[tune_grid()]

的结果

this错误消息是非常可以理解的,因为
workflow_map()
的输出是一个
workflowset

,即将包含在列中的tibble。
因此,如何通过从

result

可复制的示例: tune_grid()
用Rreprexv2.1.1

于2025-03-19创建 您需要使用

tune_grid <- workflow_map("tune_grid")
将每个结果都放入工作流集的
workflow_map("tune_bayes", initial =tune_grid)
列中。

这里是一些代码,最后发生了主要更改:

library(tidymodels) # Load and prepare data ames_data <- ames[,sapply(ames, class) %in% c("integer", "numeric")] # Define a recipe recipe <- recipe(Sale_Price ~ ., data = ames_data) %>% step_normalize(all_predictors()) # Define models lasso_model <- linear_reg(penalty = tune(), mixture = 1) %>% set_engine("glmnet") rf_model <- rand_forest(min_n = tune(), trees = 500) %>% set_engine("ranger") %>% set_mode("regression") # Create workflows lasso_wf <- workflow() %>% add_model(lasso_model) %>% add_recipe(recipe) rf_wf <- workflow() %>% add_model(rf_model) %>% add_recipe(recipe) cross_val <- vfold_cv(ames_data, v = 5) tune_grid <- workflow_set( preproc = list(recipe), models = list(lasso = lasso_model, rf = rf_model)) %>% workflow_map("tune_grid", resamples = cross_val, grid = 25) tune_grid #> # A workflow set/tibble: 2 × 4 #> wflow_id info option result #> <chr> <list> <list> <list> #> 1 recipe_lasso <tibble [1 × 4]> <opts[2]> <tune[+]> #> 2 recipe_rf <tibble [1 × 4]> <opts[2]> <tune[+]> ## now bayes tune_bayes <- workflow_set( preproc = list(recipe), models = list(lasso = lasso_model, rf = rf_model) ) %>% workflow_map("tune_bayes", resamples = cross_val, initial =tune_grid) tune_bayes$result[[1]] #> [1] "Error in check_initial(initial, pset = param_info, wflow = object, resamples = resamples, : \n `initial` should be a positive integer or the results of [tune_grid()]\n" #> attr(,"class") #> [1] "try-error" #> attr(,"condition") #> <error/rlang_error> #> Error in `check_initial()`: #> ! `initial` should be a positive integer or the results of [tune_grid()]
    

r tidymodels
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.