Sparse regression
Select useful predictors while fitting a regression model.
Scikit-Learn · Class
ElasticNet is linear regression with combined L1 and L2 regularization. Use alpha to control overall penalty strength and l1_ratio to control the L1/L2 balance.
ElasticNet fits a linear regression model while penalizing both the absolute and squared coefficient magnitudes. The alpha value controls overall regularization, and l1_ratio controls the balance between L1 sparsity and L2 shrinkage.
ElasticNet(alpha=1.0, l1_ratio=0.5, fit_intercept=True, precompute=False, max_iter=1000, copy_X=True, tol=0.0001, warm_start=False, positive=False, random_state=None, selection='cyclic')| Name | Type | Description |
|---|---|---|
alpha | float, default=1.0 | Constant that multiplies the combined regularization terms. |
l1_ratio | float, default=0.5 | ElasticNet mixing parameter between 0 and 1. |
fit_intercept | bool, default=True | Whether to calculate the intercept for the model. |
precompute | bool or array-like, default=False | Whether to use a precomputed Gram matrix. |
max_iter | int, default=1000 | Maximum number of iterations. |
copy_X | bool, default=True | Whether to copy X before fitting. |
tol | float, default=0.0001 | Tolerance for optimization stopping. |
warm_start | bool, default=False | Whether to reuse the previous solution as initialization. |
positive | bool, default=False | Whether coefficients must be positive. |
random_state | int, RandomState instance or None, default=None | Seed or random-state object used by randomized selection. |
selection | {'cyclic', 'random'}, default='cyclic' | Coordinate update strategy: cyclic or random. |
A fitted ElasticNet estimator; predict returns the model's predictions for supplied samples.
from sklearn.datasets import make_regression
from sklearn.linear_model import ElasticNet
X, y = make_regression(n_features=2, random_state=0)
model = ElasticNet(alpha=0.1, l1_ratio=0.5, random_state=0)
model.fit(X, y)
predictions = model.predict(X[:3])
print(predictions)This creates a small regression dataset, fits ElasticNet with mixed L1/L2 regularization, and predicts three samples.
Select useful predictors while fitting a regression model.
Handle correlated predictors more stably than a purely L1-regularized model.
Tune regularization strength and penalty balance externally, for example with cross-validation.
ElasticNet minimizes squared prediction error plus a weighted combination of L1 and L2 penalties. alpha sets the total regularization strength, while l1_ratio allocates it between L1 and L2.
Follow-up: What happens at the endpoints? With l1_ratio=0 the penalty is L2, and with l1_ratio=1 it is L1; intermediate values combine both.
fit_intercept determines whether the estimator calculates an intercept. The fitted coefficients are stored in coef_, and the intercept is stored in intercept_.
Follow-up: How can predictions be generated after fitting? Call predict(X), which returns the model output for the supplied samples.
selection='cyclic' updates coefficients in a fixed order, while selection='random' chooses a coefficient randomly at each iteration. Random selection can converge faster when tol is relaxed.
Follow-up: How can reproducibility be controlled for random selection? Set random_state to an integer so the random coordinate choices are repeatable.
sklearn.linear_model.ElasticNetCVsklearn.linear_model.Lassosklearn.linear_model.Ridge