SKLearner Home | About | Contact | Examples

Configure Lasso "alpha" Parameter

The alpha parameter in scikit-learn’s Lasso controls the strength of the L1 regularization applied to the linear regression model.

Lasso (Least Absolute Shrinkage and Selection Operator) is a linear regression method that uses L1 regularization to enforce sparsity in the model coefficients. This means it can effectively select features by shrinking some coefficients to zero, thus improving the model’s generalization.

The alpha parameter determines the extent of regularization. A higher alpha value increases the regularization effect, leading to more coefficients being reduced to zero. This can help with feature selection but might also lead to underfitting if set too high.

The default value for alpha is 1.0.

In practice, values between 0.1 and 10 are commonly used, depending on the dataset and specific problem.

from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Lasso
from sklearn.metrics import r2_score

# Generate synthetic dataset
X, y = make_regression(n_samples=1000, n_features=20, n_informative=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 alpha values
alpha_values = [0.1, 1.0, 10.0]
r2_scores = []

for alpha in alpha_values:
    lasso = Lasso(alpha=alpha, random_state=42)
    lasso.fit(X_train, y_train)
    y_pred = lasso.predict(X_test)
    r2 = r2_score(y_test, y_pred)
    r2_scores.append(r2)
    print(f"alpha={alpha}, R^2 Score: {r2:.3f}")

Running the example gives an output like:

alpha=0.1, R^2 Score: 1.000
alpha=1.0, R^2 Score: 1.000
alpha=10.0, R^2 Score: 0.976

The key steps in this example are:

  1. Generate a synthetic regression dataset with informative features and noise
  2. Split the data into train and test sets
  3. Train Lasso models with different alpha values
  4. Evaluate the R^2 score of each model on the test set

Some tips and heuristics for setting alpha:

Issues to consider:



See Also