Key Concepts
Code cheat-sheet
import numpy as np
from sklearn.linear_model import RidgeCV, LassoCV
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
alphas = np.logspace(-4, 4, 50)
ridge = Pipeline([
("scaler", StandardScaler()),
("ridge", RidgeCV(alphas=alphas, cv=5)),
]).fit(X_train, y_train)
lasso = Pipeline([
("scaler", StandardScaler()),
("lasso", LassoCV(alphas=alphas, cv=5, max_iter=10_000)),
]).fit(X_train, y_train)
print("Ridge α*:", ridge.named_steps["ridge"].alpha_)
print("Lasso α*:", lasso.named_steps["lasso"].alpha_)# Pull ranked coefficients out of a fitted regularised model
import pandas as pd
coefs = lasso.named_steps["lasso"].coef_
names = X_train.columns
ranked = (
pd.DataFrame({"feature": names, "coef": coefs})
.assign(abs_coef=lambda d: d.coef.abs())
.sort_values("abs_coef", ascending=False)
)
print(ranked.head(10))
print("zeroed out:", (coefs == 0).sum(), "/", len(coefs))Math
Ridge as constrained OLS — minimise the residual, stay inside an L2 ball of radius √s.
LASSO swaps the ball for a diamond. Same optimisation idea, very different geometry at the boundary.
Equivalent Bayesian priors. The Laplace prior's spike at zero is why L1 produces exact zeros.
Pick λ as the minimiser of cross-validated loss. Empirical, not theoretical — and that's fine.
Self-check
Gotchas
Standardise before everything. All three views — analytical, geometric, probabilistic — assume coefficients live on a comparable scale. Without standardisation the penalty is biased toward features that happen to be measured in small units, and the geometric picture no longer applies.
Don't read absolute coefficient magnitudes as effect sizes. After standardisation, a coefficient of 0.5 means "a one-standard- deviation move in this feature shifts y by half a standard deviation of y" — not "0.5 dollars". Translate back if you need real-world units.
Same α across Ridge and LASSO ≠ same regularisation strength. The penalties have different units. Don't compare them by setting α equal — compare them at their respective CV optima.
LASSO's chosen features are not stable. Among correlated predictors LASSO picks one almost at random. Refit on a slightly different sample and the "important" feature changes. If you need stable rankings, use stability-selection or Elastic Net.
The U-curve can be flat. Sometimes CV error barely moves over a wide α range. That's a signal that regularisation isn't doing much work here — either the model is well-specified already, or features are weak. Either way, don't over-interpret the chosen α.