【问题标题】:How to update tuning parameter values using the update function in Tidymodels如何使用 Tidymodels 中的更新功能更新调整参数值
【发布时间】:2020-07-26 01:43:34
【问题描述】:

我尝试使用tidymodels 中的update 函数覆盖默认调整值,但无法更新这些值。

例如,在下面的代码中,我想将 min_n 的范围从默认的 2 到 40 更改为 30 到 50。但是,min_n 的值保持在 2 和 40。

library(tidymodels)
#> -- Attaching packages --------------------------------------------------------------------------------------------------------------------------- tidymodels 0.1.0 --
#> v broom     0.7.0      v recipes   0.1.13
#> v dials     0.0.8      v rsample   0.0.7 
#> v dplyr     1.0.0      v tibble    3.0.3 
#> v ggplot2   3.3.2      v tune      0.1.1 
#> v infer     0.5.2      v workflows 0.1.2 
#> v parsnip   0.1.2      v yardstick 0.0.7 
#> v purrr     0.3.4
#> -- Conflicts ------------------------------------------------------------------------------------------------------------------------------ tidymodels_conflicts() --
#> x purrr::discard() masks scales::discard()
#> x dplyr::filter()  masks stats::filter()
#> x dplyr::lag()     masks stats::lag()
#> x recipes::step()  masks stats::step()
rf <- decision_tree(cost_complexity = tune(), tree_depth = tune(), min_n = tune()) %>%
  set_mode("classification") %>%
  set_engine("rpart")

rf_wf <- workflow() %>% 
  add_model(rf) %>%
  add_formula(class ~ .)

param <- rf %>% parameters()
param %>% update(min_n = min_n(range = c(30L, 50L)))
#> Collection of 3 parameters for tuning
#> 
#>               id  parameter type object class
#>  cost_complexity cost_complexity    nparam[+]
#>       tree_depth      tree_depth    nparam[+]
#>            min_n           min_n    nparam[+]

rf_grid <- grid_regular(param, levels = 2)
rf_grid
#> # A tibble: 8 x 3
#>   cost_complexity tree_depth min_n
#>             <dbl>      <int> <int>
#> 1    0.0000000001          1     2
#> 2    0.1                   1     2
#> 3    0.0000000001         15     2
#> 4    0.1                  15     2
#> 5    0.0000000001          1    40
#> 6    0.1                   1    40
#> 7    0.0000000001         15    40
#> 8    0.1                  15    40

reprex package (v0.3.0) 于 2020 年 7 月 26 日创建

【问题讨论】:

    标签: r tidymodels


    【解决方案1】:

    update 方法返回一个新的参数对象——它不会更新您传递的值 supply in-place。你需要这样做

    newparam <- param %>% update(min_n = min_n(range = c(30L, 50L)))
    grid_regular(newparam, levels = 2)
    
    #   cost_complexity tree_depth min_n
    #             <dbl>      <int> <int>
    # 1    0.0000000001          1    30
    # 2    0.1                   1    30
    # 3    0.0000000001         15    30
    # 4    0.1                  15    30
    # 5    0.0000000001          1    50
    # 6    0.1                   1    50
    # 7    0.0000000001         15    50
    # 8    0.1                  15    50
    

    【讨论】:

      猜你喜欢
      • 2021-08-31
      • 2018-12-15
      • 2021-08-24
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-10
      相关资源
      最近更新 更多