I5 · Machine Learning Foundations

Linear Regression

Linear regression is a supervised learning algorithm that predicts a number by fitting a straight line (or a flat plane, with many features) through the data. It chooses the line that minimises the average squared difference between predictions and true values.

12 min Intermediate

What you need: The earlier topics of this module.

Definition

Linear regression is a supervised learning algorithm that predicts a number by fitting a straight line (or a flat plane, with many features) through the data. It chooses the line that minimises the average squared difference between predictions and true values.

In simple words

Plot flat sizes against prices, and the dots roughly form a rising band. Draw the straight line that passes as close as possible to all the dots. To price a new flat, find its size on that line and read off the price. The whole job of the algorithm is to find the best line.

Why it matters

It's the simplest useful model, it's fast, and you can read exactly what it learned. It's usually the first baseline for any number-prediction problem. It's also the base of logistic regression and of neural networks, since a neuron is linear regression plus an activation.

How it works

  1. Pick a line with a slope w and an intercept b.
  2. Measure the error: how far each point is from the line, squared and averaged.
  3. Adjust w and b to reduce the error, with gradient descent or the direct formula.

Assumptions

  • The relationship is roughly a straight line.
  • Errors are independent of each other, with similar spread throughout.
  • Features aren't near-duplicates of each other, or coefficients become unstable.

The maths

The model
The prediction
The slope: how much y changes when x rises by 1
The intercept: the prediction when x is 0
The loss: mean squared error
Best slope and intercept, for one feature
Gradient descent updates
The learning rate

Points: (1, 2), (2, 4), (3, 5). The means are x̄ = 2 and ȳ = 3.67.

Σ(x−x̄)(y−ȳ) = (−1)(−1.67) + (0)(0.33) + (1)(1.33) = 3
Σ(x−x̄)²     = 1 + 0 + 1 = 2
w = 3 / 2 = 1.5
b = 3.67 − 1.5 × 2 = 0.67

The line: ŷ = 1.5x + 0.67
Prediction for x = 4: ŷ = 6.67

In code

First from scratch, using the formula above, then with scikit-learn. Both find the same line.

linear_regression.py
import numpy as np
from sklearn.linear_model import LinearRegression

x = np.array([1, 2, 3])
y = np.array([2, 4, 5])

w = np.sum((x - x.mean()) * (y - y.mean())) / np.sum((x - x.mean()) ** 2)
b = y.mean() - w * x.mean()
print(f"From scratch: y = {w:.2f}x + {b:.2f}")

model = LinearRegression().fit(x.reshape(-1, 1), y)
print(f"scikit-learn: y = {model.coef_[0]:.2f}x + {model.intercept_:.2f}")
print("Prediction for x = 4:", round(model.predict([[4]])[0], 2))

Output

From scratch: y = 1.50x + 0.67
scikit-learn: y = 1.50x + 0.67
Prediction for x = 4: 6.67

Common mistakes

  • Using it on a clearly curved relationship. Plot the data first.
  • Reading a coefficient as cause and effect.
  • Predicting far outside the range of the training data.

Check yourself

Question 1

1. In ŷ = 3x + 10, what does 3 mean?

Question 2

2. Why square the errors?

Question 3

3. ŷ = 2x + 1. What's the prediction at x = 5?

Summary

  • It fits the straight line ŷ = wx + b.
  • It minimises the mean squared error.
  • Use the formula or gradient descent, and use it as your first baseline.

Read it. Now try it yourself.

Open the interactive activity

Finished reading?

Sources: scikit-learn user guide · Google Machine Learning Crash Course