glmnet with custom tuning grid

Machine Learning with caret in R

Zach Mayer

Data Scientist at DataRobot and co-author of caret

Custom tuning glmnet models

  • 2 tuning parameters: alpha and lambda
  • For single alpha, all values of lambda fit simultaneously
  • Many models for the "price" of one
Machine Learning with caret in R

Example: glmnet tuning

# Make a custom tuning grid
myGrid <- expand.grid(
  alpha = 0:1, 
  lambda = seq(0.0001, 0.1, length = 10)
)
# Fit a model
set.seed(42)
model <- train(
  y ~ ., 
  overfit, 
  method = "glmnet", 
  tuneGrid = myGrid, 
  trControl = myControl
)
# Plot results
plot(model)
Machine Learning with caret in R

Compare models visually

The image shows a line plot of ROC curve versus regularization parameter for two values of the mixing percentage: 0 and 1. The ROC is maximized when the mixing percentage is 1 and the regularization parameter is greater than 0.1.

Machine Learning with caret in R

Full regularization path

plot(model$finalModel)

The image shows a custom line plot for glmnet models. There are many lines of coefficients versus L1 norm. As L1 norm increases, the range of coefficients gets wider.

Machine Learning with caret in R

Let’s practice!

Machine Learning with caret in R

Preparing Video For Download...