ElasticNet
π― Purpose¶
This QuickRef provides a clean walkthrough for using ElasticNet Regression, which blends Ridge and Lasso to balance shrinkage and sparsity in high-dimensional linear models.
π¦ 1. When to Use ElasticNet¶
| Scenario | Why ElasticNet Works |
|---|---|
| Many correlated features | Combines L2 shrinkage (Ridge) with L1 selection (Lasso) |
| Lasso is too aggressive | ElasticNet allows partial feature retention |
| Need compromise between shrink + drop | ElasticNet balances both tendencies |
| Want tunable regularization behavior | Adjust l1_ratio for control |
βοΈ 2. How It Works¶
- Uses a mix of L1 + L2 penalties:
$$ \text{Loss} = RSS + \alpha \left( \rho \sum |w_i| + (1-\rho) \sum w_i^2 \right) $$
alpha: controls overall regularization strengthl1_ratio: determines Ridge vs Lasso behavior (0 = Ridge, 1 = Lasso)
π οΈ 3. Fitting ElasticNet in sklearn¶
from sklearn.linear_model import ElasticNet
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
model = make_pipeline(StandardScaler(), ElasticNet(alpha=0.1, l1_ratio=0.5))
model.fit(X_train, y_train)
βοΈ Always scale features before fitting
π 4. Cross-Validation & Tuning¶
from sklearn.linear_model import ElasticNetCV
model = ElasticNetCV(l1_ratio=[.1, .5, .9], cv=5).fit(X, y)
| Param | Effect |
|---|---|
alpha β |
Increases shrinkage overall |
l1_ratio β |
More feature selection (Lasso-like) |
l1_ratio β |
More coefficient shrinkage (Ridge-like) |
π 5. Output Interpretation¶
| Coefficients | Interpretation |
|---|---|
| = 0 | Feature dropped (L1 effect) |
| β 0 (small) | Retained with shrinkage |
| Mix of zero and nonzero | Balanced model (Ridge + Lasso) |
βοΈ Use RΒ², MAE, RMSE for evaluation
β Modeling Checklist¶
- [ ] Features scaled prior to fitting
- [ ]
alphaandl1_ratiotuned with CV - [ ] Zeroed features reviewed for model implications
- [ ] Compared vs Ridge and Lasso performance
π‘ Tip¶
βElasticNet is your safety net β when Ridge keeps too much and Lasso cuts too deep.β