Stop 07 · Supervised Learning: RegressionTopics · 8/8

Residual Analysis & Regression Assumptions

Part of the Machine Learning Roadmap

In short

An R² of 0.85 tells you the model is decent on average. It doesn't tell you it fails badly on expensive flats, or misses a curve entirely. Residual plots show you where and how the model is wrong.

Residuals are the differences between actual and predicted values. Plotting them reveals patterns the model missed, uneven spread and outliers, and checks assumptions like linearity and constant variance.

The jaadu, with a measuring tape around its neck, pins a loose kurta on a dress form in a small tailor's shop, marking the gaps with chalk.

Think of it like this

The tailor's trial fitting

A tailor doesn't just ask 'does the kurta fit?'. He checks each spot: tight at the shoulders, loose at the waist, short at the sleeves. If it's off by the same small amount everywhere, fine. If it's tight in one place and loose in another, the pattern is wrong. Residuals are those spot checks: actual minus predicted, row by row.

Watch it happen

Residuals vs predicted: healthy or not

Healthy residuals

  • A random cloud centred on zero.
  • Same spread for small and large predictions.
  • No curve, no funnel, few extreme points.

Warning signs

  • A U-shape: the model missed a curve. Try polynomial terms or a tree model.
  • A funnel that widens: errors grow with size. Try log-transforming the target.
  • A few huge residuals: outliers or data errors worth checking.

A good model's mistakes look like random noise. Any visible pattern is information the model has not captured yet.

The big ideas

What you need to know

Residual = actual - predicted

Positive means the model guessed too low, negative means too high. Plot them, don't just average them.

Linearity

If residuals curve against predictions or a feature, the true relationship isn't a straight line.

Constant variance

Errors should be about the same size everywhere. A funnel shape is called heteroscedasticity.

Roughly normal, independent

A histogram or Q-Q plot checks normality. For time data, residuals shouldn't follow each other in runs.

See it in code

Walk through the code

python
1import matplotlib.pyplot as plt2import numpy as np3from scipy import stats4from sklearn.linear_model import LinearRegression5 6rng = np.random.default_rng(3)7area = rng.uniform(400, 2500, 300).reshape(-1, 1)8price = 6000 * area[:, 0] * np.exp(rng.normal(0, 0.15, 300))   # error grows with size9 10model = LinearRegression().fit(area, price)11pred = model.predict(area)12resid = price - pred13 14fig, ax = plt.subplots(1, 2, figsize=(10, 4))15ax[0].scatter(pred, resid, s=8)16ax[0].axhline(0, color="red")17ax[0].set(xlabel="Predicted price (₹)", ylabel="Residual (₹)")18stats.probplot(resid, plot=ax[1])19plt.show()20 21log_model = LinearRegression().fit(area, np.log(price))   # often fixes the funnel

1/5Prices with multiplicative noise: bigger flats have bigger rupee errors.

Flat prices whose errors grow with size give a funnel-shaped residual plot.

Save yourself the pain

Common mistakes

Oops

Reporting only R² or RMSE and skipping the plots.

Do this instead

Always plot residuals vs predicted. Two models with the same RMSE can fail in completely different ways.

Oops

Deleting every point with a large residual.

Do this instead

Investigate first. It may be a typo worth fixing, or a real case the model needs a new feature to explain.

Your mission

Diagnose and fix

~30 min

Quick check

Residuals form a funnel, small for cheap flats and large for expensive ones. What is this called?

Go deeper

  1. 1Plot residuals against predicted values and look for curves or funnel shapes.
  2. 2Draw a histogram of residuals to check whether they centre on zero.
  3. 3Try a log transform of a skewed target and compare the residual plots.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge