# Linear Regression and Regularization

> Least squares, interpreting coefficients, and ridge, lasso and elastic net penalties for more stable models. Free notes, key concepts, resources and a mini...

Canonical: https://www.internhack.in/roadmaps/tracks/machine-learning/linear-regression
Last updated: 2026-11-28

Least squares, interpreting coefficients, and ridge, lasso and elastic net penalties for more stable models.

Beginner · about 8 hours · difficulty 2/5

Learn first: [Framing ML Problems and the scikit-learn API](https://www.internhack.in/roadmaps/tracks/machine-learning/ml-problem-framing), [Optimization and Gradient Descent](https://www.internhack.in/roadmaps/tracks/machine-learning/optimization)

Linear regression predicts a number as a weighted sum of features. It is fast, interpretable and a strong baseline, and its ideas (loss, gradients, regularization) carry into every other model.

### Core ideas

- **Ordinary least squares** minimizes mean squared error; it has a closed-form solution and a gradient descent solution.
- **Coefficients** give the change in prediction per unit feature change, holding others fixed. Scale features before comparing magnitudes.
- **Assumptions** (linearity, independent errors, constant variance) matter most for inference, less for pure prediction.
- **Polynomial and spline features** let a linear model fit curves.

### Regularization

- **Ridge (L2)** shrinks all coefficients; helps with correlated features.
- **Lasso (L1)** drives some coefficients to exactly zero, acting as feature selection.
- **Elastic net** mixes both.

The penalty strength (`alpha`) is a hyperparameter chosen by cross-validation. Always scale features first, because the penalty treats all coefficients equally.

### Pitfalls

Extrapolating far outside the training range, reading coefficients causally, and ignoring residual plots that reveal a missing nonlinearity.

**Good enough to move on** when you can fit OLS, ridge and lasso, choose alpha with `RidgeCV` or `LassoCV`, and interpret the resulting coefficients and residuals.

## Key concepts

- **Ordinary least squares**
- **Interpreting coefficients**
- **Residual analysis**
- **Polynomial features**
- **Ridge, lasso and elastic net**
- **Statsmodels for inference**

## Mini project

Predict median house value on the scikit-learn California Housing dataset with OLS, ridge and lasso, compare cross-validated RMSE, and plot which coefficients lasso sets to zero as alpha grows.

## Check yourself

Why must you standardize features before applying ridge or lasso regularization?

## Free resources

- [scikit-learn: Linear models](https://scikit-learn.org/stable/modules/linear_model.html) (scikit-learn)
- [An Introduction to Statistical Learning (chapters 3 and 6)](https://www.statlearning.com/) (ISL)

Previous: [Framing ML Problems and the scikit-learn API](https://www.internhack.in/roadmaps/tracks/machine-learning/ml-problem-framing) · Next: [Logistic Regression and Linear Classifiers](https://www.internhack.in/roadmaps/tracks/machine-learning/logistic-regression)

## More in Classical Supervised Learning

- [Framing ML Problems and the scikit-learn API](https://www.internhack.in/roadmaps/tracks/machine-learning/ml-problem-framing)
- [Linear Regression and Regularization](https://www.internhack.in/roadmaps/tracks/machine-learning/linear-regression)
- [Logistic Regression and Linear Classifiers](https://www.internhack.in/roadmaps/tracks/machine-learning/logistic-regression)
- [k-Nearest Neighbours and Naive Bayes](https://www.internhack.in/roadmaps/tracks/machine-learning/knn-and-naive-bayes)
- [Decision Trees](https://www.internhack.in/roadmaps/tracks/machine-learning/decision-trees)
- [Bagging and Random Forests](https://www.internhack.in/roadmaps/tracks/machine-learning/random-forests)
- [Gradient Boosting with XGBoost and LightGBM](https://www.internhack.in/roadmaps/tracks/machine-learning/gradient-boosting)
- [Support Vector Machines and Kernels](https://www.internhack.in/roadmaps/tracks/machine-learning/support-vector-machines)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Machine Learning Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/machine-learning)
