Foundations to Frontier
Tests
D. Classical Machine Learning › D2 Linear Classifiers

Regularized Logistic Regression (L1, L2, Elastic Net)

Penalised logistic regression adds an L2 (ridge), L1 (lasso) or elastic-net penalty to the log loss. The penalty keeps coefficients finite under separation, reduces overfitting when features are many, and, with L1, selects a sparse subset of features. This page covers the penalised objective, its Bayesian reading as a prior, solving it by proximal gradient descent (ISTA) with soft-thresholding, λ_max and the regularisation path, choosing λ by cross-validation, scikit-learn's C parameter, and how correlated features behave under each penalty. In the exercises you implement L1/L2/elastic-net logistic regression by proximal gradient with an optimality check; then you diagnose a fault from 100 telemetry channels with only 120 labelled passes and compare no penalty, ridge, lasso and elastic net on log loss, AUC and channel selection.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Keep the weights modest

With 100 telemetry channels and only 120 labelled examples, plain logistic regression can find a combination of channels that explains every training label perfectly, by chance. It then becomes absurdly confident and wrong on new data. Regularisation adds a cost for large weights:

  • L2 (ridge) shrinks all weights toward zero;
  • L1 (lasso) pushes most weights to exactly zero, keeping only the channels that really help;
  • elastic net mixes the two.
Regularisation paths: lasso versus ridge
non-zero coefficients: 6 of 8 · current: x1 0.90, x2 -0.93, x3 0.91, x4 -0.00, x5 0.00, x6 0.00, x7 0.29, x8 -0.19
Coloured: the three relevant features (true weights 1.5, −1.2, 0.9); grey: five irrelevant ones. With L1, coefficients leave zero one by one as λ weakens, so at moderate λ only the useful features are active. With L2, every coefficient is non-zero for any λ; noise features merely become small.
Why engineers care

Fault-detection models built from wide telemetry, with few labelled anomalies, are the typical case where regularisation decides between a useful model and a dangerous one. L1 also produces short lists of channels that engineers can inspect and trust.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Which penalty sets many weights exactly to zero?
  2. 2. A larger penalty λ makes the weights…
  3. 3. In the demo, ridge's irrelevant coefficients are…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

One proximal step by hand

The penalised objective is (mean log loss) + λ·‖w‖₁. A proximal-gradient step has two parts:

  1. a gradient step on the log loss: v = w − η·∇;
  2. soft-thresholding: each vⱼ moves toward 0 by ηλ, and becomes exactly 0 if it is closer than that.

Take w = (1.0, 0.1), gradient (0.5, 0.5), step η = 1 and λ = 0.2:

  • the gradient step gives v = (0.5, −0.4);
  • soft-thresholding by 0.2 gives (0.3, −0.2).

With λ = 0.5, the second coordinate (|−0.4| below 0.5) becomes exactly 0, and the first becomes 0.0.

When is everything zero? At w = 0 (intercept fitted), the gradient for feature j is −Σ xᵢⱼ(yᵢ − ȳ)/n. If λ exceeds the largest of these in absolute value (λ_max), no feature is worth switching on.

The steps in plain English:

  1. Standardise the features: penalties assume comparable scales. Never penalise the intercept.
  2. Choose the penalty type (L1 for selection, L2 for stability, elastic net for both).
  3. Build a grid of λ from λ_max down to about λ_max/100. Fit along the path with warm starts.
  4. Pick λ by cross-validated log loss (or AUC).
  5. Refit on all data with that λ. Report the selected features and check them against physics.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Soft-threshold of 3 with threshold 1?
  2. 2. Soft-threshold of −0.5 with threshold 1?
  3. 3. Why standardise before penalising?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

The penalised problem

Objective: minimise (1/n)Σ log-loss(yᵢ, σ(b + xᵢᵀw)) + λ[α‖w‖₁ + ½(1 − α)‖w‖²]. Here α = 0 is ridge, α = 1 is lasso, and values in between give the elastic net.

Solvers:

  • Proximal gradient (ISTA/FISTA): a gradient step on the smooth part, then soft-thresholding by ηλα. Step η = 1/L with L = ¼‖[1, X]‖²/n + λ(1 − α).
  • Coordinate descent with quadratic approximations (glmnet).
  • L-BFGS for pure L2.
  • liblinear / saga in scikit-learn.

Separation. Any λ > 0 with an L2 component makes the problem strongly convex, so a finite unique solution exists even for separated data.

λ_max = ‖Xᵀ(y − ȳ)‖∞/n (for α = 1). This gives a natural starting point for the path.

Bayesian view. L2 is the MAP estimate under Gaussian priors on w. L1 corresponds to Laplace priors.

scikit-learn’s C. scikit-learn minimises C·Σ loss + penalty, so C plays the role of 1/(nλ) (for L2 with the ½ convention). Smaller C means stronger regularisation.

from sklearn.linear_model import LogisticRegressionCV
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
model = make_pipeline(StandardScaler(), LogisticRegressionCV(Cs=20, penalty="elasticnet", solver="saga",
                       l1_ratios=[0.5, 1.0], scoring="neg_log_loss", cv=5, max_iter=5000)).fit(X, y)

Use it when features are many relative to the labelled examples, classes may be separable, or you want a sparse, interpretable channel list. Note that L1 selection is unstable with correlated features (it picks one of a group arbitrarily). Elastic net, or stability selection, helps.

🐍 Exercise 1: penalised logistic regression by proximal gradient
Implement soft-thresholding, elastic-net-penalised logistic regression by proximal gradient descent with an unpenalised intercept, the penalised objective and λ_max; check that λ above λ_max zeroes all coefficients, that the lasso keeps the true features while discarding most others, that no single-coordinate move lowers the objective at the solution, and that a ridge penalty gives a finite fit on separated data.
📝 Quick check (Level 2)
0/3 answered
  1. 1. In scikit-learn, a smaller C means…
  2. 2. The L2 penalty corresponds to a prior that is…
  3. 3. Which solver supports elastic-net logistic regression in scikit-learn?

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Theory

Optimality (KKT) for the lasso. At the solution, |∂ℓ/∂wⱼ| ≤ λ for every zero coefficient, and ∂ℓ/∂wⱼ = −λ·sign(wⱼ) for every non-zero one. This is why λ_max is the largest gradient magnitude at w = 0, and why coefficients enter the path one at a time as λ falls.

Selection consistency. The lasso recovers exactly the true support only under an irrepresentable condition: irrelevant features must not be too correlated with relevant ones (Zhao and Yu, 2006). Otherwise it tends to include correlated decoys. In exercise 2, the lasso included about 10 extra channels, partly the decoys built to mimic true channels.

Grouping. With correlated features, ridge spreads weight across the group, while the lasso picks one more or less arbitrarily. The elastic net keeps the group together (Zou and Hastie, 2005), at the cost of more selected channels.

High dimensions. With p ≫ n, L1-penalised logistic regression achieves prediction error of order √(s log p / n) for s true features. This is why sparse models can work with more channels than examples.

Calibration. Penalties shrink probabilities toward the base rate, which counteracts overconfidence. Strong λ can make a model under-confident; check reliability diagrams (D0.10).

Interview questions.

  • Why not penalise the intercept? It would bias the predicted base rate toward 0.5 for no benefit.
  • How do you choose between L1 and L2? Use L1 when you believe few features matter and you want selection; L2 when many small effects exist or features are strongly correlated; tune α by CV if unsure.
📝 Quick check (Level 3)
0/3 answered
  1. 1. For a zero lasso coefficient at the optimum, the gradient magnitude is…
  2. 2. The lasso's exact support recovery requires…
  3. 3. With p ≫ n and s true features, L1 logistic error scales like…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Diagnosing a fault from 100 channels

History.

  • Le Cessie and van Houwelingen (1992) introduced ridge logistic regression.
  • Tibshirani (1996) introduced the lasso.
  • Zou and Hastie (2005) introduced the elastic net.
  • Friedman, Hastie and Tibshirani (2010) made glmnet’s coordinate descent the standard solver for whole regularisation paths.
  • Penalised logistic regression is now the default baseline for wide data in genomics, text classification and anomaly detection.

Exercise 1. 20 features, of which 3 are true. λ_max = 0.253: at 1.01·λ_max all coefficients are 0, and at 0.9·λ_max the first one enters. At λ = 0.03 the lasso keeps 5 features: the 3 true ones plus 2 noise features. No coordinate move improves the objective. On perfectly separated data, ridge gives a finite slope of 1.46.

Exercise 2: fault detection. Setup: 100 channels, of which 5 are true, and 2 decoys that are 90% correlated with true channels; 120 labelled passes; λ chosen by 3-fold CV; 6 datasets, each scored on 2000 test passes.

Method Test log loss AUC Channels used True channels found (of 5)
Nearly unpenalised 1.935 0.727 100 5
L2 (ridge) 0.588 0.760 100 5
L1 (lasso) 0.503 0.846 15.2 4.8
Elastic net (α = 0.5) 0.524 0.831 26.8 5.0
  • Without a penalty the model is wildly overconfident: its log loss of 1.9 is worse than always predicting the base rate (about 0.69).
  • Ridge fixes the overconfidence but cannot ignore the 95 useless channels, so its AUC barely improves.
  • The lasso, matching the sparse truth, is best on every metric and uses about 15 channels.
  • The elastic net finds all 5 true channels (its grouping keeps true channels alongside their decoys), but uses more channels and is slightly less accurate.
🐍 Exercise 2 (research): sparse fault detection from wide telemetry
Simulate 100 telemetry channels with 5 true fault channels and correlated decoys, 120 labelled passes; compare nearly unpenalised, ridge, lasso and elastic-net logistic regression (λ by 3-fold CV along a warm-started path) on test log loss, AUC, number of channels used and true channels recovered.

Regularised logistic regression in AI, robotics and aerospace

  • Fault detection and isolation: sparse models over wide telemetry, giving short lists of implicated channels.
  • Text and log classification: L1/L2 logistic regression on bag-of-words features of anomaly reports.
  • Neural networks: weight decay is the L2 penalty; L1 encourages sparse, prunable weights.
  • Small-sample qualification data: stable, finite estimates even with separation.

References

  1. S. le Cessie, J. C. van Houwelingen (1992). “Ridge estimators in logistic regression.” JRSS C 41(1).
  2. R. Tibshirani (1996). “Regression shrinkage and selection via the lasso.” JRSS B 58(1).
  3. H. Zou, T. Hastie (2005). “Regularization and variable selection via the elastic net.” JRSS B 67(2).
  4. J. Friedman, T. Hastie, R. Tibshirani (2010). “Regularization paths for generalized linear models via coordinate descent.” Journal of Statistical Software 33(1).
  5. P. Zhao, B. Yu (2006). “On model selection consistency of lasso.” JMLR 7.