Regression Intuition
Read a little, play a little. No scary maths, and no rush.
Predicting numbers, starting with lines
House prices, delivery demand, customer tenure — whenever the answer is a number, you're doing regression. The simplest version draws the best line through scattered points: given square footage, the line guesses price. A line in one dimension becomes a plane in two, a hyperplane beyond — same idea, more slopes.
from sklearn.linear_model import LinearRegression
model = LinearRegression().fit(Xtr, ytr)
print("R²:", model.score(Xte, yte))
Under the hood it minimizes squared error — big misses hurt quadratically, so the line chases the bulk of the points while outliers tug it around (a preview of why data cleaning matters).
Reading R² honestly
R² = 1.0 means perfect prediction; 0 means "as good as always guessing the average." Real models live between: 0.85 on house prices is excellent, 0.3 on stock returns might still be tradeable. Context sets the bar, never the number alone. And note what's not the metric: accuracy % belongs to categories — applying it to numbers is a category error beginners make once.
The line, with real numbers
Suppose a model fitted on apartment sales learns: price = 40,000 + 2,500 × floor area in square meters. Read it like a sentence. The starting point is 40,000, and each extra square meter adds 2,500. A 100 m² apartment is predicted at 40,000 + 250,000 = 290,000. If it actually sold for 310,000, the residual, the gap between real and predicted, is 20,000. Training chooses the starting point and the slope that make the squared residuals as small as possible across all the sales.
Error metrics in plain terms
- MAE (mean absolute error): the average size of a miss, in the target's own units. "We are off by about 18,000 on a typical sale" is the easiest result to explain to anyone.
- RMSE (root mean squared error): also in the target's units, but it weighs large misses more heavily. If RMSE is much larger than MAE, a few predictions are badly wrong.
- R²: the share of the variation in prices that the model explains, as described above.
Report MAE to people making decisions, and use all three when comparing models.
Check the residuals
Plot each prediction against its residual. A shapeless cloud around zero is what you want. A curve means the relationship is not a straight line. A fan that widens with larger values means errors grow with the price, which often calls for predicting the logarithm of the price instead.
When lines aren't enough
Curved relationships (price vs distance isn't a straight line) defeat linear models — the classic underfit. That's your cue for trees and forests (next chapters), which bend with the data. Start linear anyway: it's fast, interpretable ("each bedroom adds $40K"), and the baseline every fancier model must beat.
"Linear" can still bend
A linear model is linear in its weights, not necessarily in the original columns. Add a column for area squared, or for the logarithm of distance, and a linear model can follow a curve. It is a useful middle step before reaching for trees, and it keeps the model easy to explain.
Remember this
- Numbers out → regression. R² near 1 good, near 0 baseline.
- Linear first for speed and interpretability; trees when lines underfit.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
Related chapters
Spotted a mistake or want a topic covered? Report an issue