Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

step_smote and nearest_neighbor have a tuning parameter with same name and causes an error #149

Closed
rfsaldanha opened this issue Feb 22, 2024 · 3 comments
Labels
Question Question about behaviour of package

Comments

@rfsaldanha
Copy link

Hi!

The functions step_smote from {themis} and nearest_neighbor from {parsnip} have a common tuning parameter called neighbors. When I try to tune both hyperparameters in a workflow, I receive an error about duplicated item. Is there a way to 'rename' a hyperparameter?

Bellow a reprex:

library(tidymodels)
library(themis)
library(palmerpenguins)
#> 
#> Attaching package: 'palmerpenguins'
#> The following object is masked from 'package:modeldata':
#> 
#>     penguins

p_split <- initial_split(data = penguins |> drop_na(), strata = species)

folds <- vfold_cv(data = training(p_split), strata = species)

rec <- recipe(species ~ bill_length_mm + bill_depth_mm, data = training(p_split)) |>
  step_smote(species, over_ratio = tune(), neighbors = tune())

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

knn_spec <-
  nearest_neighbor(
    neighbors = tune(),
    dist_power = tune()
  ) |>
  set_engine("kknn") |>
  set_mode("classification")

wf_set <- workflow_set(
  preproc = list(rec),
  models = list(
    rf = rf_spec,
    knn = knn_spec
  ), 
  cross = TRUE
)

tune_res <- 
  wf_set |>
  workflow_map(
    "tune_grid",
    resamples = folds,
    grid = 10,
    verbose = TRUE
  )
#> i 1 of 2 tuning:     recipe_rf
#> ✔ 1 of 2 tuning:     recipe_rf (9.9s)
#> i 2 of 2 tuning:     recipe_knn
#> ✖ 2 of 2 tuning:     recipe_knn failed with: Error in hardhat::extract_parameter_set_dials(wflow) :   Element `id` should have unique values. Duplicates exist for item(s): 'neighbors'

Created on 2024-02-22 with reprex v2.1.0

@EmilHvitfeldt EmilHvitfeldt added the Question Question about behaviour of package label Feb 23, 2024
@EmilHvitfeldt
Copy link
Member

Hello @rfsaldanha 👋

In cases were we need to tune the same parameter multiple times, like here with neighbors, can we use the id argument of tune() to distinguish them. I'm adding tune("neighbors_smote") below in step_smote() to denote it to be different than the tune()used innearest_neighbor()`

library(tidymodels)
library(themis)
library(palmerpenguins)
#> 
#> Attaching package: 'palmerpenguins'
#> The following object is masked from 'package:modeldata':
#> 
#>     penguins

p_split <- initial_split(data = penguins |> drop_na(), strata = species)

folds <- vfold_cv(data = training(p_split), strata = species)

rec <- recipe(species ~ bill_length_mm + bill_depth_mm, data = training(p_split)) |>
  step_smote(species, over_ratio = tune(), neighbors = tune("neighbors_smote"))

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

knn_spec <-
  nearest_neighbor(
    neighbors = tune(),
    dist_power = tune()
  ) |>
  set_engine("kknn") |>
  set_mode("classification")

wf_set <- workflow_set(
  preproc = list(rec),
  models = list(
    rf = rf_spec,
    knn = knn_spec
  ), 
  cross = TRUE
)

tune_res <- 
  wf_set |>
  workflow_map(
    "tune_grid",
    resamples = folds,
    grid = 10,
    verbose = TRUE
  )
#> i 1 of 2 tuning:     recipe_rf
#> ✔ 1 of 2 tuning:     recipe_rf (9.9s)
#> i 2 of 2 tuning:     recipe_knn
#> ✔ 2 of 2 tuning:     recipe_knn (5.4s)

tune_res
#> # A workflow set/tibble: 2 × 4
#>   wflow_id   info             option    result   
#>   <chr>      <list>           <list>    <list>   
#> 1 recipe_rf  <tibble [1 × 4]> <opts[2]> <tune[+]>
#> 2 recipe_knn <tibble [1 × 4]> <opts[2]> <tune[+]>

@rfsaldanha
Copy link
Author

That's great! Thank you.

Copy link

github-actions bot commented Mar 9, 2024

This issue 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.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question Question about behaviour of package
Projects
None yet
Development

No branches or pull requests

2 participants