7 routes42 sections324 units

Unit 2.2 · about 5 h

137 / 324

Conditioning, basis expansions, ridge, lasso and elastic net

An unpenalized fit trusts every column equally, so one rescaled feature or one duplicated signal can swing the coefficients wildly. Bases and penalties decide what the model may express before more data can save it.

  • derive
  • measure

You can now

  • Derive the ridge closed form and predict how standardization changes the penalized solution.
  • Contrast \(L_1\) and \(L_2\) penalties on correlated features: selection and sparsity versus shrinkage and stability.
  • Measure coefficient paths and validation error across the penalty strength on fixed splits and select it from evidence.

Covers

  • Conditioning, feature scale and coefficient instability
  • Polynomial and interaction bases
  • Ridge, lasso and elastic net regularization

Study, in this order

6 steps

  1. Polynomial fitting and model complexity
    Dive into Deep Learning - linear models slice

    Section 3.6.1.1 Model Complexity, then 3.6.2.1 Polynomial Curve Fitting (with distinct \(x\) values, a polynomial whose degree equals the number of examples fits the training set exactly) and Fig. 3.6.1, the underfitting-overfitting curve

  2. Weight decay and the high-dimensional demo
    Dive into Deep Learning - linear models slice

    Section 3.7: Norms and Weight Decay, then High-Dimensional Linear Regression with \(d = 200\) features on 20 training examples at noise std 0.01

  3. Polynomial and interaction bases, then ridge, lasso and tuning
    An Introduction to Statistical Learning with Python

    Chapter 3, Section 3.3.2 (pp. 94-99) for polynomial and interaction extensions; Chapter 6, Sections 6.2.1 to 6.2.3 (pp. 240-252) for ridge, the lasso and tuning-parameter selection

  4. Regularization lab on the Hitters data
    An Introduction to Statistical Learning with Python

    Ridge Regression and the Lasso sections: solution paths on Hitters Salary with 5-fold cross-validation of the tuning parameter

  5. Ridge, lasso, elastic net and polynomial objectives
    scikit-learn - linear models, discriminant analysis and naive Bayes

    Sections 1.1.2 (Ridge minimizes RSS plus \(\alpha\) times squared norm), 1.1.3 (Lasso minimizes \(\frac{1}{2n}\) RSS plus \(\alpha\) times L1 norm, coordinate descent), 1.1.5 (Elastic-Net objective and the correlated-features guidance) and 1.1.16 (PolynomialFeatures basis expansion)

  6. Polynomial regression and regularized linear models
    Hands-On Machine Learning with Scikit-Learn and PyTorch - classification and linear models

    Book Chapter 4: Polynomial Regression (printed pp. 153-154) and Regularized Linear Models (pp. 159-167). Notebook 04 cells: Polynomial Regression, Ridge Regression, Lasso Regression, Elastic Net and Early Stopping sections, including the scale-and-regularization warning in the chapter Q and A

Focus on

  • Ridge adds \(\alpha\|w\|^2\) to the raw RSS, while Lasso and ElasticNet add their \(\alpha\) penalty to \(\text{RSS}/(2n)\), so \(\alpha = 1\) means very different strengths. Compare paths or CV errors, never raw alphas.
  • Penalties tax raw coefficient size. A feature with small numeric values, such as kilometers instead of meters, needs a larger weight for the same effect, so it pays more penalty and is shrunk harder. Standardize before penalizing and leave the intercept free.
  • Lasso lands on corners: the sklearn guide says it is likely to pick one of several correlated features at random, while elastic net is likely to keep both. D2L's \(d = 200\), \(n = 20\) demo shows weight decay rescuing a fit with more features than samples.

Reproduce closed-book

Derive \(w = (X^\top X + \alpha I)^{-1}X^\top y\) for centered data. Build the correlated demo: default_rng(2), \(n = 100\), \(x_1\) and \(x_2\) standard normal, \(x_3 = x_1 + 0.1\,\text{noise}\), \(y = 2x_1 - 3x_2\) plus standard normal noise, drawn in that order. Show Ridge(alpha=1.0) matches the centered closed form to 1e-14 at [1.33909931, -3.06183881, 0.64520852]. Then show Lasso returns all zeros once \(\alpha\) reaches \(\max|X_c^\top y_c|/n\) = 2.74, the \(x_2\) entry; just below it only \(x_2\) enters (-0.146 at \(\alpha = 2.6\)). Ridge at any finite \(\alpha\) keeps every coefficient nonzero here.

Traps

  • Judging Ridge(alpha=1) against Lasso(alpha=1) as a fair fight. The objectives normalize the loss differently, so match effective shrinkage or validation error instead.
  • Fitting the scaler on all the data, or penalizing the intercept. Fit standardization on train only and keep the offset out of the penalty.
  • Reading lasso zeros as proof the dropped features carry no signal. Across resamples a correlated rival is selected instead, so report selection stability rather than one mask.

Practice

D2L 3.7 Exercise 1: \(\lambda\) sweep on the high-dimensional demo

Exercises, problem 1: vary \(\lambda\) and plot training and validation error; then problem 3 for the \(L_1\) update equations

Keep the \(d = 200\), \(n = 20\) demo fixed, record where validation error bottoms out, and state the update change the L1 penalty forces.

ISLP Lab 6.5.2: Ridge Regression and the Lasso

Ridge Regression and the Lasso sections on the Hitters Salary data with 5-fold CV selection of the tuning parameter

Compare the selected strengths and explain any disagreement on which coefficients survive.

Self-test

  1. Ridge(alpha=1) and Lasso(alpha=1) disagree sharply on standardized data. Is one implementation wrong?

  2. D2L 3.7 fits \(d = 200\) features on 20 examples. Why does plain least squares break, and what does the \(L_2\) term change algebraically?

  3. Two features correlate at 0.999. Lasso keeps one with a large weight while ridge keeps both, shrunken. Why?

  4. With 10 distinct \(x\) values, a degree-9 polynomial fits the training data exactly. Is it the best model?

Completion criterion

Stop when

Your ridge closed form matches sklearn to 1e-12, your \(\lambda\) sweep shows validation error falling then rising, and you can explain any lasso-versus-ridge selection disagreement on correlated features.

Mark passed, then open 2.3 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • An Introduction to Statistical Learning with Python
    James, Witten, Hastie, Tibshirani and Taylorprimarytheory
    Use Chapters 3, 4 and 6 with their Python labs as the first pass: least squares and its diagnostics, logistic regression, LDA, QDA and naive Bayes, basis expansion and regularization paths.
  • Dive into Deep Learning - linear models slice
    D2L authorsrequiredimplementation
    Use 3.1, 3.4, 3.6, 3.7, 4.1, 4.4, 22.7 and 22.9 for linear regression, generalization, weight decay, softmax, maximum likelihood and naive Bayes. Implement with NumPy first and use the PyTorch versions only as a differential reference.
  • scikit-learn - linear models, discriminant analysis and naive Bayes
    scikit-learnreferencereference
    Use the official Linear Models, LDA and QDA, and Naive Bayes guides to verify estimator semantics and selected edge cases after the independent implementation works.
  • Hands-On Machine Learning with Scikit-Learn and PyTorch - classification and linear models
    Aurelien Geron (O'Reilly, 2025)supportimplementation
    Read the Chapter 3 and 4 sections each unit names, then run notebooks 03 and 04 for classification diagnostics, linear and polynomial regression, regularization and logistic regression. Recreate the target mechanism independently before inspecting worked exercise code.

Also in this section

  • NeetCode ML - linear-model closing drill
    NeetCodesupportpractice
    After the section gate, solve Gradient Descent, Sigmoid and ReLU, Softmax, Cross-Entropy Loss, Linear Regression Forward and Linear Regression Training cold. Repeat only failed items from a blank file with changed values.
  • Deep-ML - linear and probabilistic model problems
    Deep-MLsupportpractice
    Use 15 Linear Regression Using Gradient Descent, 106 Train Logistic Regression with Gradient Descent and 140 Bernoulli Naive Bayes Classifier as unit checks after the local implementation passes. Their tests were read in review; each card states what they pin. The local implementation remains the evidence.
  • Tom Mitchell - Generative and Discriminative Classifiers: Naive Bayes and Logistic Regression
    Tom Mitchell, Carnegie Mellon Universityrequiredtheory
    Read Sections 2 (naive Bayes, the smoothed estimates (7) and (9), parameter counts), 3 (Gaussian naive Bayes implies the logistic form, gradient ascent, the \(L_2\) penalty, the \(K\)-class rules) and 4 (generative versus discriminative, Ng and Jordan rates). This is the CS446 reading for Lectures 6-8; rework each derivation on a hand-sized dataset.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels