Data Science · Chapter 29 of 43
Linear Regression
LINEAR REGRESSION fits a straight line (or hyperplane) to predict a numeric target.
Simple, fast, interpretable — a great baseline for regression problems.
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.0Model coefficients.
Key points
- Predicts continuous values.
- Fits by minimising squared error.
- Fast and interpretable.
- Assumes an (approx.) linear relationship.
💡 Note: Always look at residuals — patterns in residuals mean linear regression is the wrong model.
