Course materials · Introduction to Machine Learning

Linear Regression

English translation

Examine predictions outside the observed range and the effect of an outlier on the regression equation.

Before practice

01 Does a small training error guarantee predictions far outside the observed range?

Description

Predictions outside the data range assume unobserved relationships and must be judged separately.

02 How much can one outlier change the slope?

Description

A point with a large residual contributes greatly to squared error and can change the slope and intercept.

Practice

Preview practice notebookDownload practice notebook

1. Straight-Line Models and Error

Starting conditions

The data are x=[012345]x=\begin{bmatrix}0&1&2&3&4&5\end{bmatrix}, y=[13.24.87.18.911.2]y=\begin{bmatrix}1&3.2&4.8&7.1&8.9&11.2\end{bmatrix}. The model is y_hat=w*x+b.

Procedure

  1. Calculate predictions, residuals, and MSE for w=2,b=1b=1.
  2. Compare w,b obtained using np.linalg.lstsq with the baseline model in the same table.

Results to retain

Items to record and points to check
Model
w
b
MSE
Prediction for x=6x=6

Points to check

  • Did you keep a consistent sign convention for the residual y_hat-y?

2. The Influence of One Sample

Starting conditions

Create data with the last y changed to 18 and compare changes in coefficients, MSE, and predictions at x=6x=6.

Procedure

  1. Keep x unchanged and create a copy with only the last y changed to 18. Apply np.linalg.lstsq separately to the original and modified data.
  2. Record the two models' w,bw,b, MSE on their respective training data, and predictions at x=6x=6 in two rows. Plot the original points, modified points, and both prediction lines on the same axes.
  3. Using the table and plot, explain how changing one sample affected the coefficients and predictions for new inputs.

Results to retain

Items to record and points to check
Training data
w·b
MSE
Prediction at x=6

Points to check

  • Did you preserve the fitting method and all samples except the one changed?

3. Checking Other Inputs

Starting conditions

Plot predictions at x∈{6,8,10}x\in\{6,8,10\} and distinguish them from predictions within the observed range.

Procedure

  1. Use the two models trained above to calculate predictions for x∈{6,8,10}x\in\{6,8,10\}. Record both models' predictions in the same table.
  2. Draw prediction lines over a range covering the original observations and the three new inputs. Use different symbols for observed points and new predictions. For the new inputs, record predictions and whether they lie within the observed range, rather than ground truth values.
  3. Explain how the difference between the two models' predictions changes farther outside the observed range.

Results to retain

Items to record and points to check
New input x
Model predictions for the original data
Model predictions for modified data
Inside the observed range or not; difference between the two predictions

Points to check

  • Did you avoid interpreting predictions for new inputs without ground truth as actual errors?