Skip to main content

`lambda`

  • Available in: GLM, GAM
  • Hyperparameter: yes

Description​

To get the best possible model, GLM and GAM need to find the optimal values of the regularization parameters α\alpha and λ\lambda. When performing regularization, penalties are introduced to the model building process to avoid overfitting, to reduce variance of the prediction error, and to handle correlated predictors. The two most common penalized models are ridge regression and LASSO (least absolute shrinkage and selection operator). The elastic net combines both penalties. These types of penalties are described in greater detail in the Regularization section in GLM for more information.

The lambda parameter controls the amount of regularization applied to the model. A non-negative value represents a shrinkage parameter, which multiplies P(α,β)P(\alpha, \beta) in the objective. The larger lambda is, the more the coefficients are shrunk toward zero (and each other). When the value is 0, regularization is disabled, and ordinary generalized linear models are fit. The default value for lambda is calculated by H2O using a heuristic based on the training data.

This option also works closely with the alpha parameter, which controls the distribution between the ℓ1\ell_1 (LASSO) and ℓ2\ell_2 (ridge regression) penalties. The following table describes the type of penalized model that results based on the values specified for the lambda and alpha options.

lambda valuealpha valueResult
lambda == 0alpha = any valueNo regularization. alpha is ignored.
lambda > 0alpha == 0Ridge Regression
lambda > 0alpha == 1LASSO
lambda > 00 < alpha < 1Elastic Net Penalty

Example​

library(h2o)
h2o.init()
# import the airlines dataset:
# This dataset is used to classify whether a flight will be delayed 'YES' or not "NO"
# original data can be found at http://www.transtats.bts.gov/
airlines <- h2o.importFile("http://s3.amazonaws.com/h2o-public-test-data/smalldata/airlines/allyears2k_headers.zip")

# convert columns to factors
airlines["Year"] <- as.factor(airlines["Year"])
airlines["Month"] <- as.factor(airlines["Month"])
airlines["DayOfWeek"] <- as.factor(airlines["DayOfWeek"])
airlines["Cancelled"] <- as.factor(airlines["Cancelled"])
airlines['FlightNum'] <- as.factor(airlines['FlightNum'])

# set the predictor names and the response column name
predictors <- c("Origin", "Dest", "Year", "UniqueCarrier", "DayOfWeek", "Month", "Distance", "FlightNum")
response <- "IsDepDelayed"

# split into train and validation
airlines_splits <- h2o.splitFrame(data = airlines, ratios = 0.8)
train <- airlines_splits[[1]]
valid <- airlines_splits[[2]]

# try using the `lambda` parameter:
airlines_glm <- h2o.glm(family = 'binomial', x = predictors, y = response, training_frame = train,
validation_frame = valid, lambda = 0.0001)

# print the AUC for the validation data
print(h2o.auc(airlines_glm, valid = TRUE))

# Example of values to grid over for `lambda`
hyper_params <- list( lambda = c(1, 0.5, 0.1, 0.01, 0.001, 0.0001, 0.00001, 0) )

# this example uses cartesian grid search because the search space is small
# and we want to see the performance of all models. For a larger search space use
# random grid search instead: list(strategy = "RandomDiscrete")
grid <- h2o.grid(x = predictors, y = response, family = 'binomial', training_frame = train, validation_frame = valid,
algorithm = "glm", grid_id = "air_grid", hyper_params = hyper_params,
search_criteria = list(strategy = "Cartesian"))

## Sort the grid models by AUC
sorted_grid <- h2o.getGrid("air_grid", sort_by = "auc", decreasing = TRUE)
sorted_grid

Feedback