In 1886, Francis Galton studied the heights of parents and their children.
He noticed something: tall parents tend to have tall children — but shorter
than themselves. Short parents tend to have taller children — but shorter
than average. Heights "regress toward the mean."
He drew a line through his data. That line became the most important tool
in the history of data analysis.
01 — What Linear Regression Does
Linear regression answers one question: given some input features, what number do I predict as output? It finds the best straight line (or hyperplane in multiple dimensions) through your data that minimizes prediction error.
Examples: predict house price from size + bedrooms. Predict a student's exam score from study hours. Predict tomorrow's temperature from today's. Any time your output is a continuous number, linear regression is your first tool to reach for.
Imagine plotting every house sale in a city as a dot on graph paper — horizontal axis is size in m², vertical axis is price. Linear regression draws the single straight line that comes closest to all those dots simultaneously. Once you have that line, predicting the price of a new house is as simple as finding its size on the x-axis and reading off the y-axis.
02 — The Math (Made Friendly)
Before the formula — what is the model actually doing? It makes a weighted sum of your inputs, then adds a baseline. "Weighted sum" means: multiply each feature by how important it is, then add everything up. That's it.
Concretely — predicting an exam score from hours studied:
predicted score = (5.2 × hours_studied) + 30
The 5.2 is the weight — each extra hour of study adds 5.2 points. The 30 is the bias — the baseline score someone gets with zero hours studied. Training is the process of finding these numbers automatically from data.
In general notation with n features, the formula is:
ŷ = w1x1 + w2x2 + ... + wnxn + b
Where ŷ (y-hat) is the prediction, x values are your input features, w values are the learned weights (one per feature), and b is the bias.
Training finds the weights and bias that minimize MSE (Mean Squared Error) — the average of squared differences between predictions and true values. Squaring does two things: it makes all errors positive (a $10k under-prediction and $10k over-prediction don't cancel out), and it punishes large mistakes much more than small ones.
There are two ways to find these optimal values:
w = (XᵀX)⁻¹Xᵀy. Computes the exact best weights in one step — no iterations, no learning rate to tune. Fast and exact for datasets that fit in memory. This is what sklearn's LinearRegression() uses internally.03 — What R² Tells You
R² (R-squared, or the coefficient of determination) answers one question: how much better is my model than just predicting the average every time?
If the only thing you knew about exam scores was their average — say 65 — and you predicted 65 for every student, that's your dumb baseline. R² measures how much your model beats that baseline, as a proportion from 0 to 1.
R² = 0.7 is outstanding when predicting stock prices (highly random by nature), but would be considered poor for predicting the boiling point of water (a nearly deterministic physical relationship). Never interpret R² in isolation — compare it against the baseline difficulty of your problem and the standards in your domain.
04 — Code: From Scratch vs sklearn
Two approaches to fit the same model. The from-scratch version uses gradient descent so you can see exactly what's happening inside. The sklearn version uses OLS — the direct formula, faster and exact. Both converge to the same answer.
import numpy as np
# Dataset: hours studied → exam score
X = np.array([1,2,3,4,5,6,7,8], dtype=float)
y = np.array([45,50,58,61,68,74,79,84], dtype=float)
# ── From scratch: gradient descent ──────────────────────────────────
w, b, lr = 0.0, 0.0, 0.01
for _ in range(5000):
pred = w * X + b
dw = np.mean(2 * (pred - y) * X) # gradient of loss w.r.t. w
db = np.mean(2 * (pred - y)) # gradient of loss w.r.t. b
w -= lr * dw
b -= lr * db
print(f"Scratch → w={w:.2f}, b={b:.2f}")
# ── sklearn: OLS (exact formula, one step) ───────────────────────────
from sklearn.linear_model import LinearRegression
model = LinearRegression().fit(X.reshape(-1,1), y)
print(f"sklearn → w={model.coef_[0]:.2f}, b={model.intercept_:.2f}")
print(f"R² = {model.score(X.reshape(-1,1), y):.3f}")
print(f"9 hrs → {model.predict([[9]])[0]:.1f} predicted score")
w = 5.24 — each extra hour of study adds ~5.24 points to the predicted score. This is the slope of the line: how steeply the prediction rises as the input increases.
b = 39.30 — a student who studied zero hours is predicted to score ~39 points. This is the y-intercept: the baseline the model starts from before accounting for study time.
R² = 0.996 — the model explains 99.6% of the variation in scores. Near-perfect — which makes sense, because this dataset was designed to be almost perfectly linear.
9 hrs → 86.5 — for a new student the model has never seen, studying 9 hours, the predicted score is 86.5.
05 — When Linear Regression Breaks
Linear regression makes specific assumptions. When the data violates them, the model misbehaves — sometimes silently. Here are the four most common failure modes and what to do about each.
The output layer of almost every neural network is a linear layer. The classification head of a vision model, the projection layer of a transformer, the value head in reinforcement learning — all linear transformations. Linear regression isn't a historical relic. It's the atomic unit that everything else is built from.
06 — A Thought to Sit With
MSE treats all errors equally by size — a $10,000 mistake on a $100,000 house and a $10,000 mistake on a $10,000,000 mansion are penalized identically, even though one is a 10% error and the other is 0.1%.
When might minimizing absolute error be the wrong objective? Who is harmed when the loss function doesn't reflect real-world costs — and what would you optimize instead?
07 — Coming Up Next
Tomorrow — Day 5: Logistic Regression. Linear regression's sibling for classification. We add one elegant twist — a sigmoid function — and suddenly our model outputs probabilities. It's 70 years old and still runs in production at Google and Amazon.
08 — Go Deeper
▶ StatQuest: Linear Regression, Clearly Explained
YouTube · 27 min · The clearest visual walkthrough of OLS, R², and the assumptions that exists. Watch the residual plots section especially.
▶ Kaggle: Intro to Machine Learning
Free interactive course · Write and run real regression code on real datasets directly in your browser — the fastest path from reading to doing.
▶ Coursera: Deep Learning Specialization — Andrew Ng
5-course specialization · Covers the mathematics of regression, gradient descent, and optimization with exceptional clarity. Audit free. The best structured path from intuition to rigorous understanding.
- What it does — Fits the best straight line through data to predict a continuous number.
- The formula — ŷ = w1x1 + ... + wnxn + b. A weighted sum of features plus a baseline.
- w (weight) — How much each feature shifts the prediction. One per input feature. Learned from data.
- b (bias) — The baseline prediction when all inputs are zero. The y-intercept.
- MSE — Loss function. Squares prediction errors, penalizing large mistakes more than small ones.
- OLS — The exact closed-form solution. Computes optimal weights in one step. What sklearn uses.
- R² — How much variance the model explains. 1.0 = perfect. 0.0 = no better than the mean. Negative = worse than mean.
- Multicollinearity — Correlated features destabilize weights. Check correlations and remove duplicates before training.
- When to use — Target is a continuous number; relationship is approximately linear.
- When not to use — Curved relationships, extreme outliers, categorical targets, or highly correlated features without preprocessing.
- What does R² mean and what values indicate a good model?
R² measures the proportion of variance in the target explained by the model. Values near 1.0 are good; near 0.0 means the model is no better than predicting the mean; negative means it is worse than the mean. Context matters — R²=0.7 is excellent for some domains, poor for others. - What is OLS and when would you use gradient descent instead?
OLS (Ordinary Least Squares) computes the exact optimal weights in a single step via w = (XᵀX)⁻¹Xᵀy. It is fast, exact, and requires no tuning — preferred for small-to-medium datasets. Gradient descent is iterative and slower but scales better to very large datasets where matrix inversion becomes prohibitively expensive. - Why is MSE preferred over MAE in many cases?
MSE penalizes large errors more heavily due to squaring and is differentiable everywhere — which gradient descent requires. MAE is more robust to outliers and easier to interpret, but not differentiable at zero, which complicates optimization. - What are the main assumptions of linear regression?
Linearity (the relationship between features and target is linear), independence of errors, homoscedasticity (constant error variance across the range of inputs), approximate normality of residuals, and no severe multicollinearity between features. - How does regularization (Ridge / Lasso) help linear regression?
Regularization adds a penalty to the loss function that discourages large weights, reducing overfitting. Ridge (L2) shrinks all weights proportionally toward zero. Lasso (L1) can drive individual weights to exactly zero, performing automatic feature selection — useful when you suspect many features are irrelevant.
No attachments for this topic yet.