Linear Regression
Linear regression predicts a number as a weighted sum of features. It’s simple, fast and interpretable, and it introduces every idea that bigger models reuse.
The model
For all examples at once: . Each weight is the change in the prediction when feature increases by one unit, holding the others fixed.
Example: price (₹ lakh) = 0.05 × area (sq.ft) + 4 × bedrooms − 0.3 × age + 10. A 1,000 sq.ft, 2-bedroom, 10-year-old flat → 50 + 8 − 3 + 10 = ₹65 lakh.
The loss: mean squared error
Squaring punishes big errors heavily and makes the loss smooth and convex: one global minimum. (It’s also the maximum-likelihood loss under Gaussian noise.)
Solving it
Closed form (normal equation): set the gradient to zero:
It’s exact and needs no learning rate, but it costs about to invert and fails if is singular (duplicate or collinear features).
Gradient descent: repeat
It scales to millions of examples and features. Standardise features first, , or the loss bowl is stretched and descent is slow.
Beyond straight lines
“Linear” means linear in the weights, not the inputs. Add transformed features such as , or and you can fit curves (polynomial regression). More features means more flexibility, and more risk of overfitting: a degree-15 polynomial can thread through every training point and be useless in between.
flowchart LR A["Degree 1:<br/>underfits"] --- B["Degree 3:<br/>good fit"] --- C["Degree 15:<br/>overfits"]
Evaluating
- MSE / RMSE (root of MSE, in the target’s units): “typical error ±₹4 lakh”.
- MAE (mean absolute error): less sensitive to outliers.
- R² : the fraction of variance explained. 0 means no better than predicting the mean; it can be negative on test data.
Assumptions (and what breaks)
- Linearity: the relationship is linear in the features. If not, add features or use another model.
- Independent errors: violated by time series, where neighbouring errors correlate.
- Constant error variance (homoscedasticity): errors that grow with the prediction suggest transforming the target, e.g. .
- No strong multicollinearity: correlated features make coefficients unstable.
- Outliers pull hard on squared error; consider MAE, Huber loss, or cleaning the data.
Regularised versions
- Ridge (L2): add . It shrinks weights, stabilises against collinearity, and has the closed form .
- Lasso (L1): add . It drives some weights exactly to zero, which performs feature selection.
- Elastic Net: both.
More on these in the bias–variance lesson.
Check yourself
R² = 0.85 on the test set means…
Why scale features before gradient descent?
Two features are almost perfectly correlated. What happens to the linear regression coefficients?
Item 6 of 24 · updated 2026-10-12