Reputation: 8930
I am using the tidymodels workflow_map()
, and inspecting the resulting "workflow set/tibble" I see many parameters leading to the same fit (same rmse, same rsq). (how) can I remove combinations of hyperparameters that lead to the same fit within the "workflow set/tibble"?
I guess this implies two questions:
stacks::add_candidates()
seems to do that, but it is unclear to me whether its output can be converted back to a "workflow set/tibble"?Here's the code, where the output tune_results
contains 7 configurations that are identical, and I would like to remove them from tune_results
.
library(tidymodels)
library(dplyr)
set.seed(42)
data("mtcars")
mtcars <- mtcars %>%
mutate(across(where(is.character), as.factor))
# Define the split
set.seed(123)
data_split <- initial_split(mtcars, prop = 0.8)
train_data <- training(data_split)
test_data <- testing(data_split)
# Create the recipe
mtcars_recipe <- recipe(mpg ~ ., data = train_data)
lasso_model <- linear_reg(penalty = tune(), mixture = 1) %>%
set_engine("glmnet")
wf_set <- workflow_set(
preproc = list(lasso = mtcars_recipe),
models = list(lasso = lasso_model))
set.seed(123)
cv_folds <- vfold_cv(train_data, v = 5)
# Create a tuning grid for penalty
penalty_grid <- grid_regular(penalty(range = c(-4, 0)), levels = 30)
# Tune the model
set.seed(123)
tune_results <- wf_set %>%
workflow_map(
"tune_grid",
resamples = cv_folds,
grid = penalty_grid,
metrics = metric_set(rmse, rsq),
control = control_grid(save_pred = TRUE, save_workflow = TRUE))
tune_results
#> # A workflow set/tibble: 1 × 4
#> wflow_id info option result
#> <chr> <list> <list> <list>
#> 1 lasso_lasso <tibble [1 × 4]> <opts[4]> <tune[+]>
stacks::stacks() %>%
# add candidate members
stacks::add_candidates(tune_results)
#> Warning: Predictions from 7 candidates were identical to those from existing candidates
#> and were removed from the data stack.
#> # A data stack with 1 model definition and 23 candidate members:
#> # lasso_lasso: 23 model configurations
#> # Outcome: mpg (numeric)
Created on 2024-08-27 with reprex v2.1.1
Upvotes: 2
Views: 32