Work with your assigned partner to fit linear regression to this MPG dataset using gradient descent. Only use the horsepower feature.
We are going building up to implement gradient descent for Linear Regression but first we need to do a few steps:
Make predictions for all the vehicles (rows) in the dataset.
To do this, y_hat = w_1 x + w_0 where x is the vector of vehicle horsepower.
After this, measure the error (residuals):
res = y - y_hat.
Start with weights of one, then update the weights manually by using the gradient descent update:
\( w_{\text{new}} = w_{\text{old}} - \alpha \nabla(w) \)
Where the gradient (first derivatives) are
\( \nabla(w_0) = \frac{-2}{n} \sum\limits_{i=1}^{n} [y_i - w_0 - w_1 x_i] = \frac{-2}{n} \sum\limits_{i=1}^{n} [y_i - \hat{y}_i] = \frac{-2}{n} \sum\limits_{i=1}^{n} res_i \)
\( \nabla(w_1) = \frac{-2}{n} \sum\limits_{i=1}^{n} x_i [y_i - w_0 - w_1 x_i] = \frac{-2}{n} \sum\limits_{i=1}^{n} x_i [y_i - \hat{y}_i] = \frac{-2}{n} \sum\limits_{i=1}^{n} x_i res_i \)
To implement this, try np.mean().
Test this code by running the program to calculate the gradient, then manually update the weights (variables), and repeat.
Replace the manually fitting process with a while loop. Try different termination conditions for the loop e.g. fixed number of updates, or if the weights do not change much from the previous iteration.
This page was last modified on 2026-08-19 at 20:15:10.
George Fox University · 414 N Meridian St · Newberg, Oregon 97132 · 503-538-8383
Copyright © 2018–2026 George Fox University. All rights reserved.