Machine Learning · Chapter 8 of 40

Linear Regression

LINEAR REGRESSION fits a straight line `y = mx + b` (or a hyperplane in higher dimensions) to predict a numeric value.

Simple, fast, interpretable — often the first baseline to try.

Example 1 (python)
from sklearn.linear_model import LinearRegression
import numpy as np
X = np.array([[1],[2],[3],[4]])
y = np.array([2,4,6,8])
m = LinearRegression().fit(X, y)
print(m.predict([[5]]))
Output
[10.]

Learns y = 2x.

Example 2 (python)
print('slope:', m.coef_[0], 'intercept:', m.intercept_)
Output
slope: 2.0 intercept: 0.0

Coefficients tell you the learned line.

Key points

  • Predicts a continuous number.
  • Fits by minimising squared error.
  • Fast and interpretable.
  • Assumes a linear relationship.
💡 Note: Always plot your data first — linear regression is useless on strongly non-linear relationships.

📝 Quick Quiz

1. Linear regression predicts:

2. It minimizes:

3. Best used when the relationship is: