Unit 2 of 8 · about 5 hours · derive, measure

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.

You'll be able to

  • Derive the ridge closed form and predict how standardization changes the penalized solution.
  • Contrast and 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 instabilityPolynomial and interaction basesRidge, lasso and elastic net regularization
1

Read

In this order, only the parts listed

0 of 6 done

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

    Section 3.6.1.1 Model Complexity, then 3.6.2.1 Polynomial Curve Fitting (with distinct 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 demoDive into Deep Learning - linear models slice

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

  3. Polynomial and interaction bases, then ridge, lasso and tuningAn 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 dataAn 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 objectivesscikit-learn - linear models, discriminant analysis and naive Bayes

    Sections 1.1.2 (Ridge minimizes RSS plus times squared norm), 1.1.3 (Lasso minimizes RSS plus 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 modelsHands-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

2

Focus on

  • Ridge adds to the raw RSS, while Lasso and ElasticNet add their penalty to , so 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 , demo shows weight decay rescuing a fit with more features than samples.
Common mistakes · 3
  • 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.
3

Try it yourself

Closed-book, before checking anything

Derive for centered data. Build the correlated demo: default_rng(2), , and standard normal, , 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 reaches = 2.74, the entry; just below it only enters (-0.146 at ). Ridge at any finite keeps every coefficient nonzero here.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

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

    No. Ridge adds to the raw RSS, while Lasso adds to , so the same number applies very different pressure. Sweep a path for each and compare by validation error.

  2. 2.D2L 3.7 fits features on 20 examples. Why does plain least squares break, and what does the term change algebraically?Answer

    With 200 features and 20 rows, has rank at most 20, so infinitely many weight vectors fit training exactly. Adding makes invertible, so the solution is unique, and it trades some training fit for a smaller norm; as goes to 0 it tends to the minimum-norm interpolant.

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

    When and are nearly equal, the loss depends almost only on . For a fixed sum with both weights positive, the norm is the same for every split, so the lasso is nearly indifferent and small differences in the data tip it to a corner with one weight at zero. The norm is smallest at an equal split, so ridge shares the weight.

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

    No. Zero training error does not imply low validation error: the exact fit can swing wildly between and beyond the points. That gap is what the underfitting-overfitting curve diagnoses.

5

Practice

  • D2L 3.7 Exercise 1: sweep on the high-dimensional demo

    Exercises, problem 1: vary and plot training and validation error; then problem 3 for the update equations

    Keep the , 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.

You're done when

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

Only when you can do this without notes.

Leads to

Notes

Resources · 4 for this unit, 3 more in the section

Also in this section

  • NeetCode ML - linear-model closing drillNeetCode · support

    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 problemsDeep-ML · support

    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 RegressionTom Mitchell, Carnegie Mellon University · required

    Read Sections 2 (naive Bayes, the smoothed estimates (7) and (9), parameter counts), 3 (Gaussian naive Bayes implies the logistic form, gradient ascent, the penalty, the -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.