2

Using the iris dataset, a knn-classifier was tuned with iterative search for the purpose of multiple classification. However, an error is generated, when the macro-weighted version of f_meas (as created by metric_tweak) is used in metric_set.

I would appreciate any ideas. Thank you so much for your support!

library(tidyverse)
library(tidymodels)
tidymodels_prefer()

# function
f_meas_weighted <- metric_tweak("f_meas_weighted", f_meas, estimator = "macro_weighted")

# workflow
set.seed(2023)
df <- iris 
splits <- initial_split(df, strata = Species, prop = 4/5)
df_train <- training(splits)
df_test  <- testing(splits)

df_rec <- recipe(Species ~ ., data = df_train) 

knn_model <- nearest_neighbor(neighbors = tune()) %>% 
  set_engine("kknn") %>% 
  set_mode("classification")

df_wflow <- workflow() %>%
  add_model(knn_model) %>%
  add_recipe(df_rec) 

set.seed(2023)
knn_cv <-
  df_wflow %>%
  tune_bayes(
      # error here
    metrics = metric_set(f_meas_weighted),
    resamples = vfold_cv(df_train, strata = "Species", v = 2),
    control = control_bayes(verbose = TRUE, save_pred = TRUE)
  )
❯  Generating a set of 5 initial parameter results
x Fold1: internal:
  Error in `metric_set()`:
  ! Failed to compute `f_meas...
  Caused by error in `f_meas.data.f...
  ! formal argument "estimato...
x Fold2: internal:
  Error in `metric_set()`:
  ! Failed to compute `f_meas...
  Caused by error in `f_meas.data.f...
  ! formal argument "estimato...
✓ Initialization complete

Error in `estimate_tune_results()`:
! All of the models failed. See the .notes column.
Run `rlang::last_error()` to see where the error occurred.
Warning message:
All models failed. Run `show_notes(.Last.tune.result)` for more information. 
✖ Optimization stopped prematurely; returning current results.
  • 1
    `f_meas_weighted <- metric_tweak("f_meas_weighted", f_meas, estimator = "macro_weighted") # workflow knn_model <- nearest_neighbor(neighbors = tune()) |> set_mode("classification") df_wflow <- workflow() |> add_model(knn_model) |> add_formula(Species ~ Sepal.Length) set.seed(2023) knn_cv <- df_wflow |> tune_bayes(# error here metrics = metric_set(f_meas_weighted), resamples = vfold_cv(iris, v = 2))` – Isaiah Jan 08 '23 at 03:54
  • 1
    - a slightly simpler repex. – Isaiah Jan 08 '23 at 03:56
  • 2
    This is currently a bug, I found the culprit. Which is that `metric_tweak()` and `metric_set()` doesn't work together. I have opened an issue and hopefully deal with this shortly: https://github.com/tidymodels/yardstick/issues/351 – EmilHvitfeldt Jan 09 '23 at 19:57

0 Answers0