XGBoosting Home | About | Contact | Examples

Configure XGBoost "lambda" Parameter

The lambda parameter in XGBoost controls the L2 regularization term on weights. By adjusting lambda, you can influence the model’s complexity and its ability to generalize.

import xgboost as xgb
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

# Generate synthetic data
X, y = make_classification(n_samples=1000, n_features=20, n_informative=2, n_redundant=10, random_state=42)

# Split the dataset into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Convert data to DMatrix object
dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test, label=y_test)

# Set up the parameters for XGBoost
params = {
    'objective': 'binary:logistic',
    'lambda': 0.5,
    'eval_metric': 'logloss'
}

# Train the model
model = xgb.train(params, dtrain, num_boost_round=100)

# Make predictions
predictions = model.predict(dtest)

The lambda parameter has the alias reg_lambda used in the scikit-learn API.

For example:

from xgboost import XGBClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

# Generate synthetic data
X, y = make_classification(n_samples=1000, n_features=20, n_informative=2, n_redundant=10, random_state=42)

# Split the dataset into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize the XGBoost classifier with a lambda value
model = XGBClassifier(reg_lambda=0.5, eval_metric='logloss')

# Fit the model
model.fit(X_train, y_train)

# Make predictions
predictions = model.predict(X_test)

Understanding the “lambda” Parameter

The lambda parameter, also known as reg_lambda, determines the strength of the L2 regularization term on the weights in the XGBoost model. It is a regularization parameter that can help prevent overfitting by adding a penalty term to the objective function, which discourages large weights. lambda accepts non-negative values, and the default value in XGBoost is 1.

Choosing the Right “lambda” Value

The value of lambda affects the model’s complexity and its propensity to overfit:

When setting lambda, consider the trade-off between model complexity and performance:

Practical Tips



See Also