tidymodels: f_meas metric_tweak error on metric_set

181 views Asked by At

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.
0

There are 0 answers