2

我正在尝试在 tidymodels 中使用 workflow_set() 函数来评估一批模型。我知道可以修改某些模型规范以更改搜索范围,例如,鉴于此规范:

spec_lin <- linear_reg( penalty = tune(), 
                    mixture = tune()  ) %>%
set_engine('glmnet')

我可以使用以下方法修改范围:

rec_base <- recipe( price ~ feat_1) %>% 
  step_novel(feat_1) %>% 
  step_other(feat_1,threshold=.2 ) %>%
  step_dummy(feat_1)

rec_adv_param <- rec_base %>% 
  parameters() %>% 
  update ( mixture = mixture(c(0.1,0.01)) )

我的尝试是做同样的事情,但使用配方中的参数。例如:

rec_tuned <- recipe( price ~ feat_1) %>% 
  step_novel(feat_1) %>% 
  step_other(feat_1,threshold=tune() ) %>%
  step_dummy(feat_1)

其次是

rec_adv_param <- rec_tuned %>% 
  parameters() %>% 
  update ( threshold = threshold(c(0.1,0.2)) )

但是,当我尝试在 workflow_set() 定义中使用它时,如果我使用类似的东西

wf_set  <- workflow_set(recipes, models, cross = TRUE ) 
  option_add(param_info = rec_adv_param, id = "rec_tuned_spec_lin") 

结局“wf_set”失去了他原来的调整参数,已经改变了

threshold = threshold(c(0.1,0.2)

有没有办法在所有 workflow_set 模型中添加配方的参数规范?

谢谢

4

1 回答 1

1

如果您离开option_add(),您可以通过 为单个工作流添加配方参数,也可以为所有工作流添加参数。当您调整或拟合重新采样的数据时,将使用这些选项。idid = NULL

例如,如果我们想尝试 0 到 20 个 PCA 组件(而不是默认的):

library(tidymodels)
#> Registered S3 method overwritten by 'tune':
#>   method                   from   
#>   required_pkgs.model_spec parsnip
data(Chicago)
data("chi_features_set")

time_val_split <-
   sliding_period(
      Chicago,
      date,
      "month",
      lookback = 38,
      assess_stop = 1
   )

## notice that there are no options; defaults will be used
chi_features_set
#> # A workflow set/tibble: 3 × 4
#>   wflow_id         info             option    result    
#>   <chr>            <list>           <list>    <list>    
#> 1 date_lm          <tibble [1 × 4]> <opts[0]> <list [0]>
#> 2 plus_holidays_lm <tibble [1 × 4]> <opts[0]> <list [0]>
#> 3 plus_pca_lm      <tibble [1 × 4]> <opts[0]> <list [0]>

## make new params
pca_param <-
   parameters(num_comp()) %>%
   update(num_comp = num_comp(c(0, 20)))

## add new params to workflowset like this:
chi_features_set %>%
   option_add(param_info = pca_param, id = "plus_pca_lm")
#> # A workflow set/tibble: 3 × 4
#>   wflow_id         info             option    result    
#>   <chr>            <list>           <list>    <list>    
#> 1 date_lm          <tibble [1 × 4]> <opts[0]> <list [0]>
#> 2 plus_holidays_lm <tibble [1 × 4]> <opts[0]> <list [0]>
#> 3 plus_pca_lm      <tibble [1 × 4]> <opts[1]> <list [0]>

## now these new parameters can be used by `workflow_map()`:
chi_features_set %>%
   option_add(param_info = pca_param, id = "plus_pca_lm") %>%
   workflow_map(resamples = time_val_split, grid = 21, seed = 1)

#> # A workflow set/tibble: 3 × 4
#>   wflow_id         info             option    result   
#>   <chr>            <list>           <list>    <list>   
#> 1 date_lm          <tibble [1 × 4]> <opts[2]> <rsmp[+]>
#> 2 plus_holidays_lm <tibble [1 × 4]> <opts[2]> <rsmp[+]>
#> 3 plus_pca_lm      <tibble [1 × 4]> <opts[3]> <tune[+]>

reprex 包于 2021-07-30 创建 (v2.0.0 )

于 2021-07-30T23:35:22.103 回答