CommencerCommencer gratuitement

Explore lasso regression penalty values

In the previous exercise, you completed all the code to scale the target and predictor variables. You will use the train data and lasso_recipe to build a workflow to train a lasso regression model and explore the effects of different penalty values. As you adjust the penalty and retrain the model, pay attention to the number of non-zero variables that are left in the model. You will be observing how lasso regression performs feature selection.

The tidyverse and tidymodels packages have been loaded for you.

Cet exercice fait partie du cours

Dimensionality Reduction in R

Afficher le cours

Instructions

  • Train a lasso regression workflow with a penalty of 0.001 and display the model coefficients that are greater than zero.
  • Re-train a lasso regression workflow with a penalty of 0.01 and display the model coefficients that are greater than zero.
  • Re-train a lasso regression workflow with a penalty of 0.1 and display the model coefficients that are greater than zero.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Train workflow model with penalty = 0.001 and view model variables
lasso_model <- linear_reg(___ = ___, mixture = 1, engine = "___")
lasso_workflow <- workflow(preprocessor = lasso_recipe, spec = ___)
tidy(lasso_workflow %>% fit(train)) %>% filter(___ > ___)

# Train the workflow model with penalty = 0.01 and view model variables
lasso_model <- ___(___ = ___, ___ = ___, ___ = "___")
lasso_workflow <- workflow(preprocessor = ___, spec = ___)
tidy(lasso_workflow %>% fit(train)) %>% filter(___ > ___)

# Train the workflow model with penalty = 0.1 and view model variables
lasso_model <- ___(___ = ___, ___ = ___, ___ = "___")
lasso_workflow <- ___ %>% ___(___)
tidy(___ %>% ___(___)) %>% ___(___ > ___)
Modifier et exécuter le code