tidymodels / dials

Tools for creating tuning parameter values
https://dials.tidymodels.org/
Other
111 stars 26 forks source link

Handle named vector in levels argument of grid_regular #122

Closed juliasilge closed 4 years ago

juliasilge commented 4 years ago

Closes #105.

This PR changes how the levels argument handles names; if names exist and match the parameter names, they are matched up before we map across both with map2(). There are also two little tests and some updated docs.

The right number of levels now gets assigned to the right parameter, no matter what order they are in the named vector:

library(tidymodels)
#> ── Attaching packages ───────────────────────────────────────────────────────────────── tidymodels 0.1.0 ──
#> ✓ broom     0.5.6          ✓ recipes   0.1.12    
#> ✓ dials     0.0.6.9000     ✓ rsample   0.0.6     
#> ✓ dplyr     1.0.0          ✓ tibble    3.0.1     
#> ✓ ggplot2   3.3.0          ✓ tune      0.1.0     
#> ✓ infer     0.5.1          ✓ workflows 0.1.1     
#> ✓ parsnip   0.1.1          ✓ yardstick 0.0.6     
#> ✓ 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 ggplot2::margin() masks dials::margin()
#> x recipes::step()   masks stats::step()

rf_spec <- rand_forest(trees = tune(), mtry = tune(), min_n = tune()) %>%
  set_mode("classification") %>%
  set_engine("ranger")

rf_wf <- workflow() %>% 
  add_model(rf_spec)

rf_params <- parameters(rf_wf) %>%
  update(trees = trees(c(100L, 200L))) %>%
  update(mtry = mtry(c(1L, 5L))) %>%
  update(min_n = min_n(c(10L, 20L))) %>%
  finalize()

rf_params$name
#> [1] "mtry"  "trees" "min_n"

levels_vec1 <- c(trees = 3, mtry = 2, min_n = 4)
levels_vec2 <- c(mtry = 2, trees = 3, min_n = 4)
levels_vec3 <- c(min_n = 4, mtry = 2,  trees = 3)

grid_regular(rf_params, levels = levels_vec1)
#> # A tibble: 24 x 3
#>     mtry trees min_n
#>    <int> <int> <int>
#>  1     1   100    10
#>  2     5   100    10
#>  3     1   150    10
#>  4     5   150    10
#>  5     1   200    10
#>  6     5   200    10
#>  7     1   100    13
#>  8     5   100    13
#>  9     1   150    13
#> 10     5   150    13
#> # … with 14 more rows
grid_regular(rf_params, levels = levels_vec2)
#> # A tibble: 24 x 3
#>     mtry trees min_n
#>    <int> <int> <int>
#>  1     1   100    10
#>  2     5   100    10
#>  3     1   150    10
#>  4     5   150    10
#>  5     1   200    10
#>  6     5   200    10
#>  7     1   100    13
#>  8     5   100    13
#>  9     1   150    13
#> 10     5   150    13
#> # … with 14 more rows
grid_regular(rf_params, levels = levels_vec3)
#> # A tibble: 24 x 3
#>     mtry trees min_n
#>    <int> <int> <int>
#>  1     1   100    10
#>  2     5   100    10
#>  3     1   150    10
#>  4     5   150    10
#>  5     1   200    10
#>  6     5   200    10
#>  7     1   100    13
#>  8     5   100    13
#>  9     1   150    13
#> 10     5   150    13
#> # … with 14 more rows

Created on 2020-06-08 by the reprex package (v0.3.0.9001)

juliasilge commented 4 years ago

This now causes an error:

library(dials)
#> Loading required package: scales
p <- parameters(penalty(), mixture(), neighbors())
grid_regular(p, levels = c(2, penalty = 5, 6))
#> Error: Elements of `levels` should either be all named or unnamed, not mixed.

Created on 2020-06-09 by the reprex package (v0.3.0.9001)

github-actions[bot] commented 3 years ago

This pull request has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.