lambda
¶
- Available in: GLM
- Hyperparameter: yes
Description¶
To get the best possible model, GLM needs to find the optimal values of the regularization parameters α and λ. When performing regularization, penalties are introduced to the model buidling 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(α,β) 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 liner 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 (LASSO) and ℓ2 (ridge regression) penalties. The following table describes the type of penalized model that results based on the values specifed for the lambda
and alpha
options.
lambda value |
alpha value |
Result |
---|---|---|
lambda == 0 |
alpha = any value |
No regularization. alpha is ignored. |
lambda > 0 |
alpha == 0 |
Ridge Regression |
lambda > 0 |
alpha == 1 |
LASSO |
lambda > 0 |
0 < alpha < 1 |
Elastic Net Penalty |
Example¶
- r
- python
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 = .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 =.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
sortedGrid <- h2o.getGrid("air_grid", sort_by = "auc", decreasing = TRUE)
sortedGrid