1. LPU MCA Semester 1
  2. CAB107
  3. Unit 4

CAB107 · Unit 4: Machine learning fundamentals

Linear and polynomial regression

10 practice questions · in the ETE syllabus

Key points

  • Coefficient = change in y per unit change in x
  • Polynomial regression is linear in its coefficients
  • Very high degree causes overfitting (high variance)
  • Zero training error does not mean a good model

Linear regression models a continuous target as a linear function of the inputs, for example y = 3x + 2. The coefficient 3 is the change in y for a one-unit increase in x, and the intercept 2 is the value of y when x is zero. The coefficients are usually found by minimising the sum of squared errors (ordinary least squares).

Polynomial regression adds powers of the input, such as x and x squared, to fit curves. It is still a linear model because it is linear in its coefficients, even though it uses powers of the input.

from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
X2 = PolynomialFeatures(degree=2).fit_transform(X)
model = LinearRegression().fit(X2, y)

Raising the degree increases flexibility. If it is pushed far beyond what the data supports, the curve fits the training points closely but generalises badly: overfitting, a high-variance problem. A degree-9 polynomial fitted to 10 points can reach zero training error yet a very large test error. Too low a degree gives underfitting, a high-bias problem. Regularisation or a lower degree reduces overfitting.

Exam trap: zero training error is a warning sign, not a success. Judge a model by its error on unseen data.

Try two questions

Question 1 · Tier 3, Apply

For x = 1, 2, 3, 4, 5 and y = 2, 4, 5, 4, 5, what is the least-squares slope?

  1. 0.8
  2. 1.0
  3. 0.6
  4. 0.4
Show the answer

C. Sxy = 6 and Sxx = 10, so the slope is 0.6.

Why not A: 0.8 does not result from Sxy / Sxx.

Why not B: A slope of 1.0 would fit y = x, which the data do not follow.

Why not D: 0.4 does not result from Sxy / Sxx.

Question 2 · Tier 2, Understand

Why is polynomial regression still called a linear model?

  1. It is linear in its coefficients, even though it uses powers of the input
  2. It always fits a straight line to the data, since the powers of x cancel out
  3. It uses no coefficients at all, only fixed powers of the input values and constants
  4. It can be applied only to a single data point at a time, not to a whole table
Show the answer

A. The model is a weighted sum of terms such as x and x squared, so it is linear in the weights.

Why not B: Powers of x give a curve, not a straight line.

Why not C: Polynomial regression has coefficients on each power.

Why not D: It needs many data points.