Course materials · Introduction to Machine Learning

Linear Regression

English translation

Learning objectives

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

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

import numpy as np
import matplotlib.pyplot as plt
x=np.arange(6.);y=np.array([1,3.2,4.8,7.1,8.9,11.2]);X=np.c_[x,np.ones_like(x)]
coef=np.linalg.lstsq(X,y,rcond=None)[0]
print(coef)

Record results and self-check

Model:

w:

b:

MSE:

Prediction for x=6x=6:

Points to check

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

y2=y.copy();y2[-1]=18

Record results and self-check

Training data:

w·b:

MSE:

Prediction at x=6:

Points to check

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

# Use the preceding activity's data and functions to check the specified new examples.

Record results and self-check

New input x:

Model predictions for the original data:

Model predictions for the modified data:

Inside the observed range or not; difference between the two predictions:

Points to check