Linear Regression
Source: Concepts adapted from Ace the Data Science Interview — Chapter 7 (“Machine Learning”) by Kevin Huo & Nick Singh. Rewritten in original wording for this course. Coding labs use scikit-learn / NumPy on Piston.
Linear regression is the workhorse continuous-target model in interviews: clear math, readable coefficients, and a gateway to regularization, GLMs, and “why did this coefficient flip sign?” debugging. This lesson covers the model, OLS, assumptions, metrics, multicollinearity, and regularized cousins — plus quizzes, slider explorers (same FormulaExplorer pattern as Statistics / Math Behind ML), and Python coding drills after each topic (OLS, metrics, sklearn).
The learn-programming Piston runtime installs scikit-learn (import as sklearn) via api/src/learn_runtimes.json, along with NumPy / SciPy / pandas. That same package covers other classical algos later (logistic regression, trees, k-NN, SVM, clustering, pipelines). Restart / re-ensure the API after pulling so pip libs refresh.
The model
We model a continuous response as a linear function of features plus noise:
In matrix form (with a column of ones in for the intercept). Simple regression has one ; multiple regression has many.
Interpretation: is the change in expected for a one-unit increase in , holding other features fixed (ceteris paribus). That “holding fixed” story breaks down under multicollinearity or omitted-variable bias — interview gold.
Linear model basics
1 / 4In ŷ = β₀ + β₁x, β₁ is…
Pick an answer to see if you are right.
Compute: predictions & slopes
1 / 5A line through (1, 0) and (5, 4) has slope (y₂−y₁)/(x₂−x₁). What is the slope? Round to 2 decimals.
Enter a number (at most 2 decimal places).
Ordinary least squares (OLS)
OLS chooses to minimize squared residuals:
When is invertible, the closed form is:
(Quant / research loops sometimes ask you to derive this via .) Numerically, libraries use QR / SVD rather than forming naively.
Geometrically, is the projection of onto the column space of . Residuals are orthogonal to those columns.
OLS
1 / 4OLS minimizes…
Pick an answer to see if you are right.
Practice: OLS
NumPy / pure Python — easy → hard. After Check, compare Expected vs Received in the results table.
Assumptions (interview checklist)
Classic “LINE”-style assumptions for inference / BLUE-style stories:
- Linearity — is linear in the parameters (or transform features)
- Independence — errors independent (watch time series / clustered data)
- Homoscedasticity — constant error variance (else WLS / robust SEs)
- Normality of errors — mainly for exact / inference; prediction can still work without it
- No perfect multicollinearity — full column rank
- Exogeneity — errors uncorrelated with predictors (causal claims need more)
Interviewers love: name the assumptions, say what breaks, and what you do (plots, transforms, robust methods, different model).
Assumptions
1 / 4Homoscedasticity means…
Pick an answer to see if you are right.
Fit metrics
| Metric | Formula idea | Notes |
|---|---|---|
| MSE | Same units² as | |
| RMSE | Same units as | |
| MAE | More robust to outliers than MSE | |
| R² | Fraction of variance “explained”; can mislead |
Always compare metrics on held-out data (or CV). High train R² + weak test R² → overfit.
Regression metrics
1 / 4RMSE relates to MSE by…
Pick an answer to see if you are right.
Compute: MSE, RMSE, R²
1 / 5Residuals e = [1, -1, 2, 0]. MSE = (1/n)∑eᵢ². What is MSE? Round to 2 decimals.
Enter a number (at most 2 decimal places).
Practice: fit metrics
Implement MAE, MSE/RMSE, R², then the full pack — easy → hard.
Multicollinearity and regularization
Multicollinearity: predictors move together → unstable, hard-to-interpret s (large SEs, sign flips). Checks: correlation matrix, VIFs, condition number. Fixes: drop / combine features, domain constraints, ridge.
Ridge (L2) and lasso (L1) add penalties (see Math Behind ML): shrink coefficients; lasso can zero some out. Use when is large, features correlated, or you want sparsity.
Multicollinearity & regularized regression
1 / 4Strong multicollinearity typically…
Pick an answer to see if you are right.
Practice: scikit-learn
Use the Piston sklearn stack for fit/predict and a ridge shrink check.
Click Check on any exercise: the results panel lists every test in a table with Expected and Received side by side so mismatches are obvious.
Quick synthesis
| Topic | Sound bite |
|---|---|
| Model | ; read as a partial effect |
| OLS | Minimize ; when invertible |
| Assumptions | LINE + rank + exogeneity for clean inference |
| Metrics | MAE / MSE / RMSE / R² — always check held-out |
| Collinearity | Unstable β; ridge / drop features / combine |
| Code | Drills after OLS, after metrics, and after regularization (sklearn) |
Next model-family lessons: Classification fundamentals and Classification models. The same Piston sklearn stack covers logistic regression, trees, and more.