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
Read
In this order, only the parts listed
0 of 6 done
- 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
- 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
- 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
- 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
- Ridge, lasso, elastic net and polynomial objectivesscikit-learn - linear models, discriminant analysis and naive Bayes
Sections 1.1.2 (
Ridgeminimizes RSS plus times squared norm), 1.1.3 (Lassominimizes RSS plus times L1 norm, coordinate descent), 1.1.5 (Elastic-Net objective and the correlated-features guidance) and 1.1.16 (PolynomialFeaturesbasis expansion) - 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
Focus on
Ridgeadds to the raw RSS, whileLassoandElasticNetadd 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.
Lassolands 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)againstLasso(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.
Try it yourself
Closed-book, before checking anything
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.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.
Ridge(alpha=1)andLasso(alpha=1)disagree sharply on standardized data. Is one implementation wrong?AnswerNo.
Ridgeadds to the raw RSS, whileLassoadds to , so the same number applies very different pressure. Sweep a path for each and compare by validation error.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.Two features correlate at 0.999.
Lassokeeps one with a large weight while ridge keeps both, shrunken. Why?AnswerWhen 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.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.
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
- An Introduction to Statistical Learning with PythonJames, Witten, Hastie, Tibshirani and Taylor · primary
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 sliceD2L authors · required
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 Bayesscikit-learn · reference
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 modelsAurelien Geron (O'Reilly, 2025) · support
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 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.