Linear Regression
Press Next → or use ← → arrow keys
What is Linear Regression?
Linear Regression gives Priya a formula: feed in the size, out comes the price. It finds the single straight line that best explains the relationship between her input (size) and her output (price) — and then uses that line to predict the future.
Linear Regression finds the best-fit straight line that describes the relationship between one or more input features and a continuous target — then uses that line to predict new values.
The Best-Fit Line — In Pictures
The slope (β₁) says how much price changes per extra sq ft. The intercept (β₀) says where the line crosses the y-axis. That's it — the entire model is just two numbers.
The Equation — Simple & Multiple
Every coefficient in a linear model has a plain-English meaning: hold everything else constant, a 1-unit change in xᵢ moves the prediction by βᵢ. That's why regulators, banks and doctors still trust it decades after fancier models arrived.
Residuals — The Gaps We Try To Shrink
A residual is the vertical gap between an actual point and the model's line. We square each residual (positive gaps, big-error penalty), add them up, and then find the line that makes that total as small as possible. That total is the cost function.
Cost Function & The Descent To The Best Line
For small datasets, OLS closed-form jumps to the minimum in one calculation. For massive datasets, gradient descent rolls down the bowl step by step — slower per iteration, but memory-friendly and the only option once your data won't fit in RAM.
Priya's Flats — From Data To Prediction
| Size (sq ft) | Price (₹ L) | Predicted ŷ | Residual (y − ŷ) |
|---|---|---|---|
| 600 | 65 | 65.0 | 0.0 |
| 800 | 85 | 84.5 | +0.5 |
| 1,000 | 105 | 104.0 | +1.0 |
| 1,200 | 120 | 123.5 | −3.5 |
| 1,400 | 145 | 143.0 | +2.0 |
ŷ = 6.5 + 0.0975 · x. For a 1,200 sq ft flat: ŷ = 6.5 + 0.0975 × 1,200 = ₹123.5 lakhs. Priya has a defensible number for her client.
R² — How Much Better Than "Just The Average"?
R² measures the fraction of variance your regression explains beyond the naïve "just guess the mean" baseline. Priya's model: R² = 1 − 17.5 / 3,820 = 0.9954 — it explains 99.5% of price variance. That's excellent for 5 clean data points.
Evaluation Metrics — Speak In The Right Units
| R² Range | Verdict | Typical Domain |
|---|---|---|
| 0.90 – 1.00 | Excellent | Clean physics, controlled experiments |
| 0.70 – 0.90 | Good | Housing, sales forecasting |
| 0.50 – 0.70 | Acceptable | Marketing response, human behaviour |
| 0.00 – 0.50 | Poor | Financial returns, noisy signals |
| Negative | Worse than the mean | Model is broken — rebuild |
The Five Assumptions — Break One, Break The Model
When these assumptions hold, OLS gives you the Best Linear Unbiased Estimator (BLUE) — no other linear method can produce lower variance. Break the assumptions, and you lose that guarantee.
Implementation — Scratch vs Scikit-learn
Two ways to fit the same line: understand the mechanics from scratch, then let a battle-tested library handle the edge cases in production.
# ── OLS from scratch ────────────────────────── x_bar = sum(x) / n y_bar = sum(y) / n beta1 = (sum((xi - x_bar) * (yi - y_bar) for xi, yi in zip(x, y)) / sum((xi - x_bar) ** 2 for xi in x)) beta0 = y_bar - beta1 * x_bar # ── The same, with scikit-learn ─────────────── from sklearn.linear_model import LinearRegression model = LinearRegression() model.fit(X, y) y_pred = model.predict(X) print(model.coef_, model.intercept_)
Writing OLS from scratch teaches you what the model is doing. After that, always reach for
sklearn.linear_model.LinearRegression or statsmodels.OLS —
they handle numerical stability, missing values and diagnostics for free.
Common Pitfalls — What Bites Beginners
Where Linear Regression Still Wins
Linear regression is the baseline every real ML project starts from. If a neural network can't beat a properly tuned linear model by a meaningful margin, the complexity isn't earning its keep — ship the line.
Golden Rules — 1 to 5
Golden Rules — 6 to 10
Linear Regression — Two Numbers, Endless Uses
You now understand the equation, the cost function, the assumptions, the metrics and the traps. Everything that follows — logistic regression, ridge, lasso, GLMs, even neural networks — is a variation on the theme you just learned.
Learn Logistic Regression (classification cousin), then Ridge & Lasso (regularised variants), then Polynomial & GLMs for non-linear extensions. Practice on Kaggle's House Prices dataset — it's Priya's problem at scale.
📈 End of tutorial · Press ← to review, or click Restart