SKLearner Home | About | Contact | Examples

Configure ElasticNet "l1_ratio" Parameter

The l1_ratio parameter in scikit-learn’s ElasticNet controls the balance between L1 and L2 regularization in the model.

ElasticNet is a linear regression model that combines both L1 (Lasso) and L2 (Ridge) regularization methods. The l1_ratio parameter determines how much weight is given to L1 regularization versus L2 regularization.

The l1_ratio parameter ranges from 0 to 1, where 0 corresponds to pure L2 regularization, and 1 corresponds to pure L1 regularization.

The default value for l1_ratio is 0.5, which gives equal weight to both L1 and L2 regularization. Commonly used values are 0.1, 0.5, and 0.9.

from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.linear_model import ElasticNet
from sklearn.metrics import mean_squared_error

# Generate synthetic dataset
X, y = make_regression(n_samples=1000, n_features=10, noise=0.1, random_state=42)

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

# Train with different l1_ratio values
l1_ratios = [0.1, 0.5, 0.9]
mses = []

for l1_ratio in l1_ratios:
    model = ElasticNet(l1_ratio=l1_ratio, random_state=42)
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)
    mse = mean_squared_error(y_test, y_pred)
    mses.append(mse)
    print(f"l1_ratio={l1_ratio}, MSE: {mse:.3f}")

Running the example gives an output like:

l1_ratio=0.1, MSE: 4032.808
l1_ratio=0.5, MSE: 2090.250
l1_ratio=0.9, MSE: 208.728

The key steps in this example are:

  1. Generate a synthetic regression dataset with noise to simulate real-world data.
  2. Split the data into training and testing sets to evaluate model generalization.
  3. Train ElasticNet models with different l1_ratio values to observe the effect on performance.
  4. Evaluate the mean squared error (MSE) of each model to determine the optimal l1_ratio.

Some tips and heuristics for setting l1_ratio:

Issues to consider:



See Also