Tuning the penalty
Confident that Lasso is a sensible approach to reducing the number of features of your model while maintaining acceptable performance, you want to tune the model by choosing the best penalty value. A basic recipe
along with the train
and test
splits are loaded in your environment.
This exercise is part of the course
Feature Engineering in R
Exercise instructions
- Set up your model so that the penalty is tuned automatically.
- Configure a penalty grid with 30 levels.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Set up your model so that the penalty is tuned automatically
model_lasso_tuned <- logistic_reg() %>% set_engine("glmnet") %>%
set_args(mixture = 1, ___ = ___)
workflow_lasso_tuned <- workflow() %>%
add_model(model_lasso_tuned) %>%
add_recipe(recipe)
# Configure a penalty grid with 30 levels
penalty_grid <- grid_regular(penalty(range = c(-3, 1)), ___ = ___)
tune_output <- tune_grid(workflow_lasso_tuned,
resamples = vfold_cv(train, v = 5),
metrics = metric_set(roc_auc),grid = penalty_grid)
autoplot(tune_output)