Linear regression predicts a number as a weighted sum of input features. It is simple, fast and surprisingly useful.
How It Works
The model assumes the target is roughly a straight-line function of the features: price ≈ b0 + b1 × size + b2 × bedrooms. Training finds the coefficients (b0, b1, b2…) that minimise the squared differences between predictions and actual values — the method of least squares.
Reading the Coefficients
Each coefficient says how much the prediction changes when that feature increases by one unit, holding the others fixed. That makes linear models easy to explain, provided features are on sensible scales and not strongly correlated with each other.
Assumptions to Check
- The relationship is roughly linear.
- Errors are independent and have similar spread across predictions.
- Features aren't highly collinear (which makes coefficients unstable).
- Outliers aren't dominating the fit.
Plotting residuals (actual minus predicted) against predictions reveals most problems.
Extending It
- Add transformed features (logs, squares, interactions) to capture curves.
- Use regularised versions — ridge and lasso — to prevent overfitting with many features.
When to Use It
Use linear regression as a baseline for any numeric prediction, and as a final model when explainability matters more than squeezing out the last bit of accuracy.
from sklearn.linear_model import LinearRegression
model = LinearRegression().fit(X_train, y_train)
print(model.coef_, model.intercept_)