Module lessons (3/5)
Linear regression with numpy
The design matrix and the normal equation, checked against a reference.
What you'll learn in this lesson
- Build a design matrix with a column of ones for the intercept.
- Solve linear regression exactly with the normal equation.
- Check your solution against an independent reference.
Overview
For linear regression with a squared-error loss, the bottom of the valley has a closed-form formula. You do not need to descend step by step: you can compute the best parameters directly. Gradient descent is still worth knowing, because most other models have no such shortcut.
To handle the intercept elegantly, add a column of ones to the features. Then the prediction is a single dot product, ŷ = X θ, where θ holds the intercept first and then the slope:
Sections covered in this lesson
- 1The hook: sometimes you can skip the walk
- 2The design matrix
- 3The normal equation
- 4Comparing Closed-Form and Iterative Solvers
- 5Practice
- 6Check your understanding
Unlock "Linear regression with numpy" and the full Machine Learning: from first principles to practice curriculum
The design matrix and the normal equation, checked against a reference.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Machine Learning: from first principles to practice plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.