7 routes42 sections324 units

Unit 2.1 · about 4 h

136 / 324

Least squares: residuals, normal equations and stable solvers

Least squares is the baseline every later model is compared against. Its two failure modes, singular designs and outlier sensitivity, recur in every fitting method after it.

  • derive
  • implement

You can now

  • Derive the normal equations from the squared-error objective and state when the solution is unique.
  • Implement least squares three ways (normal equations, SVD least squares, minibatch gradient descent) with a gradient check, and match them on well-conditioned data.
  • Explain why squared loss punishes large residuals quadratically and when that sensitivity becomes a liability.

Covers

  • Linear regression, residuals and squared versus absolute loss
  • Normal equations, stable solvers and gradient descent

Study, in this order

6 steps

  1. Linear regression: model, squared loss, analytic solution and minibatch SGD
    Dive into Deep Learning - linear models slice

    Sections 3.1.1.1 to 3.1.1.4 (model, squared-error loss with the \(\frac{1}{2}\) factor, analytic solution, minibatch SGD update) and 3.1.3 (Gaussian noise makes squared loss maximum likelihood)

  2. Linear regression from scratch
    Dive into Deep Learning - linear models slice

    Sections 3.4: Defining the Model, Defining the Loss Function, Defining the Optimization Algorithm, Training

    Depth · D2L shows framework tabs only; there is no plain NumPy tab. Implement in NumPy first, as the section use note asks, and read the PyTorch tab only as a differential reference.

  3. Simple and multiple linear regression
    An Introduction to Statistical Learning with Python

    Chapter 3, Sections 3.1 to 3.2 (pp. 70-90): the RSS objective, least squares coefficient estimates and the multiple-regression setup

  4. Linear regression lab
    An Introduction to Statistical Learning with Python

    Simple Linear Regression and Multiple Linear Regression sections on the Boston data: fit with statsmodels OLS and read coefficients, standard errors and \(R^2\)

  5. Ordinary least squares semantics and solver
    scikit-learn - linear models, discriminant analysis and naive Bayes

    Section 1.1.1 including 1.1.1.2: LinearRegression minimizes the residual sum of squares, stores coef_ and intercept_, and solves through the SVD of \(X\) at \(O(n p^2)\) cost

  6. Normal equation and batch gradient descent
    Hands-On Machine Learning with Scikit-Learn and PyTorch - classification and linear models

    Book Chapter 4: The Normal Equation (printed pp. 138-141) and Batch Gradient Descent (pp. 145-148). Notebook 04 cells: The Normal Equation section (theta_best with the explicit inverse, then the np.linalg.lstsq and pseudoinverse cross-checks) and Gradient Descent (the batch loop: gradient 2/m X_b'(X_b theta - y), eta 0.1, 1000 epochs from a random start)

Focus on

  • D2L scales its per-example loss by \(\frac{1}{2}\) while HoML differentiates the plain mean of squares with a \(\frac{2}{m}\) gradient. The minimizer is identical but the largest stable learning rate differs by that constant.
  • \(X^\top Xw = X^\top y\) has a unique solution only when \(X\) has full column rank. With duplicate or nearly collinear columns the fitted values stay well determined but the coefficients do not: in the 1e-4 demo below both solvers return slopes near 229 and -228, and only their sum, 1.012, is pinned by the data.
  • The course notebooks show inv(X'X)@X'y to teach the formula; sklearn never forms that inverse and solves through the SVD instead.

Reproduce closed-book

From \(\|y - Xw\|^2\), derive \(X^\top Xw = X^\top y\) on paper. Then build the near-collinear demo in NumPy: default_rng(1), \(n = 200\), \(x_1\) standard normal, \(x_2 = x_1 + 10^{-4}\) times standard normal noise, an intercept column, \(y = 3 + 2x_1 - x_2 + 0.1\,\text{noise}\), drawn in that order. Show \(\operatorname{cond}(X)\) = 2.08e4 and \(\operatorname{cond}(X^\top X)\) = 4.34e8, its square. The explicit inverse and lstsq agree to about 1e-5, but both return about [3.002, 229.2, -228.2], not [3, 2, -1]. Then rebuild with default_rng(0), \(n = 60\), \(x_2 = x_1 + 10^{-8}\,\text{noise}\) and \(y = 3 + 2x_1 + 0.5\,\text{noise}\): \(\operatorname{cond}(X)\) = 1.8e8 and \(\operatorname{cond}(X^\top X)\) = 3.25e15, and the explicit inverse stops at residual norm 3.8314 while lstsq reaches the minimum, 3.7978.

Traps

  • Shipping inv(X'X)@X'y as the solver. Forming \(X^\top X\) squares the condition number, so a design with \(\operatorname{cond}(X)\) near 1e8 makes \(X^\top X\) numerically singular near \(1/\epsilon\) in float64 while lstsq still minimizes the residual.
  • Hand-rolling a bias column while the differential test keeps fit_intercept=True. sklearn centers the columns, so your ones column becomes all zeros: it gets coefficient 0 and intercept_ takes the offset. Predictions agree but a coefficient-by-coefficient comparison fails. Drop the column or pass fit_intercept=False.
  • Reading the second Deep-ML 15 test as the exact solution. It expects [2.9699, 2.0078], the gradient-descent iterate after 1000 steps at lr 0.05; the exact minimizer is [3, 2]. Zero initialization and the exact update decide it.

Practice

Deep-ML 15: Linear Regression Using Gradient Descent

Gradient descent on the \(\frac{1}{2m}\) mean squared error from zero initialization. Test 1 expects [1, 2]; test 2 pins the 1000-step iterate [2.9699, 2.0078] at lr 0.05 rather than the exact [3, 2]

Pass both tests, then show your lstsq solution agrees with test 1 and explain why it cannot match test 2 exactly.

NeetCode ML: Linear Regression Forward and Linear Regression Training

Math Foundations pair: implement the forward pass, then full training cold

After the section gate, compare the learned weights against your lstsq fit on the same data before moving on.

Self-test

  1. D2L multiplies its loss by \(\frac{1}{2}\) and HoML does not. Why is the minimizer unchanged but the stable learning rate different?

  2. Two columns of \(X\) are identical. What happens to \(X^\top X\), the coefficients and the predictions?

  3. \(\operatorname{cond}(X)\) is 2.08e4. What is \(\operatorname{cond}(X^\top X)\), and at what point does the explicit inverse break down?

  4. One label is corrupted upward by 100. Why does the squared-loss fit move far, and which single change to the loss limits the damage?

Completion criterion

Stop when

Your normal-equation and lstsq fits agree to 1e-8 on well-conditioned data, minibatch SGD with a decaying step lands within 1e-3 of them (a constant step leaves it rattling about 1e-2 away), a float64 gradient check passes, and you can say for each Deep-ML 15 test whether it pins the optimum or the optimization path.

Mark passed, then open 2.2 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