Regression predicts a continuous value. We choose a form for the function, then find the parameters that make the predictions as close as possible to the training targets.

Linear Regression

Simple linear regression models the target as a straight line, ŷ = w x + b, where w is the slope and b the intercept. With several features it becomes ŷ = w₁x₁ + w₂x₂ + ... + b, called multiple linear regression. The parameters are found by least squares: choose w and b to minimise the sum of squared errors between ŷ and y. For one feature the solution is w = Σ(x − x̄)(y − ȳ) / Σ(x − x̄)² and b = ȳ − w x̄. The fit is measured by the mean squared error (MSE) and by R², the fraction of the variation in y explained by the model.

Non-Linear Regression

Many relationships are curved: the stopping distance of a car grows with the square of its speed. A straight line then underfits. In polynomial regression we add powers of the feature, for example ŷ = w₂x² + w₁x + b. This is still linear in the parameters, so the same least-squares method applies after creating the new features x and x². Other non-linear forms include exponential and logarithmic models. The degree must be chosen with care: too low underfits and too high overfits.

Definition: Least squares. A method of fitting a model by choosing the parameters that minimise the sum of the squared differences between predicted and actual target values.

Table 3.1: Linear and polynomial regression

Aspect Linear regression Polynomial regression
Model y = wx + b y = w₂x² + w₁x + b
Shape Straight line Curve
Risk Underfits curved data Overfits at high degree
Fitting Least squares Least squares on powers of x

Example 3.1: Line against curve

Problem: Data: x = 1, 2, 3, 4 and y = 1, 4, 9, 16 (that is, y = x²). Fit a line, find its MSE and its prediction at x = 5, and compare with a quadratic fit.

Solution:

  1. x̄ = 2.5 and ȳ = 7.5. Σ(x − x̄)(y − ȳ) = 9.75 + 1.75 + 0.75 + 12.75 = 25 and Σ(x − x̄)² = 5, so w = 5 and b = 7.5 − 12.5 = −5.
  2. The line is ŷ = 5x − 5. Predictions for x = 1 to 4 are 0, 5, 10, 15; the errors are 1, −1, −1, 1, so MSE = (1 + 1 + 1 + 1) / 4 = 1.
  3. At x = 5 the line predicts 20, but the true value is 25.
  4. A quadratic ŷ = x² fits all four points exactly (MSE 0) and predicts 25.

Result: The line has MSE 1 and predicts 20; the quadratic has MSE 0 and predicts 25.

import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
x = np.array([[1], [2], [3], [4]])
y = np.array([1, 4, 9, 16])
lin = LinearRegression().fit(x, y)
mse = np.mean((lin.predict(x) - y) ** 2)
print("line: w", lin.coef_[0], "b", round(lin.intercept_, 1),
      "MSE", round(mse, 3), "at 5:", round(lin.predict([[5]])[0], 1))
P = PolynomialFeatures(2, include_bias=False)
poly = LinearRegression().fit(P.fit_transform(x), y)
err = np.mean((poly.predict(P.transform(x)) - y) ** 2)
print("quadratic MSE", round(err, 6), "at 5:",
      round(poly.predict(P.transform([[5]]))[0], 1))

Output

line: w 5.0 b -5.0 MSE 1.0 at 5: 20.0
quadratic MSE 0.0 at 5: 25.0

Common mistakes

  • Using a straight line for clearly curved data.
  • Choosing a very high polynomial degree and mistaking a perfect training fit for a good model.
  • Extrapolating far beyond the range of the training data.
  • Forgetting that least squares is sensitive to outliers.

Key takeaways

  • Linear model: ŷ = wx + b with w = Sxy / Sxx and b = ȳ − w x̄.
  • Least squares minimises the sum of squared errors.
  • Polynomial regression is linear in the parameters.
  • Degree too low underfits, too high overfits.

Practice questions

  1. What does least squares minimise?
  2. Is polynomial regression a linear model?
  3. Name a measure of fit for regression.

Answers to the practice questions

  1. The sum of squared differences between predicted and actual values.
  2. Yes, linear in its parameters, after adding powers of x.
  3. Mean squared error (or R²).

Sources

  • Official BCA syllabus Introduction to Machine Learning, course code 26BCA404DSE21, Unit III