Linear regression (and ridge, lasso, elastic net)
Fit the straight line, or flat plane, that is closest to your data, and see how a penalty makes it more stable.
Lesson 25 of 6614 minIntermediate
You will learn to
- Explain how a linear model turns inputs into a prediction and how least squares chooses the weights
- Describe what the penalty strength does in ridge, lasso and elastic net
- Say when a linear model is a good first choice and when it is not
Before this lesson
Intuition
Imagine pinning a stiff ruler across a scatter of points and moving it until it sits as close to all of them as it can. The ruler is a linear model. To predict a new row you read the ruler at that input.
With one input the model is a line. With many inputs it is a flat surface (a plane, or a hyperplane in higher dimensions). Each input has one weight that says how much the prediction moves when that input rises by one unit, with the other inputs held fixed. That is why linear models are easy to explain: "each extra square metre adds about this much to the price".
The ruler is placed by least squares. For every training row, the residual is the true value minus the prediction. Least squares chooses the weights that make the sum of squared residuals smallest. Squaring makes every residual count and punishes big misses more than small ones.
A plain linear model has no knob that controls its flexibility, so it can overfit when there are many inputs, especially inputs that are almost copies of each other. Ridge, lasso and elastic net add a penalty on the size of the weights. They accept a little more error on the training rows in return for weights that are steadier on new rows.
Picture it
The line does not have to pass through any point. It balances the gaps above it against the gaps below it. Move any point far from the others and the line tilts to chase it, because a large gap, once squared, dominates the sum.
The math
For inputs the model predicts
where is the intercept (the prediction when every input is zero) and is the weight of input . Least squares chooses and the to minimise
with training rows. With one input there is a closed form. Let and be the means:
The penalised versions minimise the same squared error plus a penalty, where is the penalty strength:
- Ridge adds (an L2 penalty). It shrinks all weights toward zero but rarely makes one exactly zero.
- Lasso adds (an L1 penalty). It can set some weights exactly to zero, so it also selects features.
- Elastic net mixes both. The parameter moves the mix toward lasso (1) or toward ridge (0).
The intercept is not penalised. Because the penalty depends on the size of the weights, inputs must be on comparable scales; see Scaling and normalization. The idea behind the penalty is covered in Regularization.
Worked example
Take five points: x = 1, 2, 3, 4, 5 and y = 2.1, 3.9, 6.2, 7.8, 10.1. The means are and .
- The numerator is (-2)(-3.92) + (-1)(-2.12) + 0 + (1)(1.78) + (2)(4.08) = 7.84 + 2.12 + 1.78 + 8.16 = 19.9.
- The denominator is 4 + 1 + 0 + 1 + 4 = 10.
- So the weight is 19.9 / 10 = 1.99, and the intercept is 6.02 - 1.99 x 3 = 0.05.
The model is . For x = 6 it predicts 11.99. The residuals are 0.06, -0.13, 0.18, -0.21 and 0.10, and their squares add up to 0.107. Compared with 39.708 for always predicting the mean, this is an R2 of about 0.997. We checked these numbers with scikit-learn's LinearRegression.
Now add a ridge penalty with alpha = 10. For a single centred input the weight becomes the numerator divided by (denominator + alpha): 19.9 / (10 + 10) = 0.995. The weight is cut in half and the prediction at x = 6 falls to 9.005. Lasso with alpha = 1 gives 1.49 on the same data, and with alpha = 4 or more it gives exactly 0, which means "ignore this input". Both shrinkages are on purpose: with noisy data and many inputs, a smaller weight is often closer to the truth on new rows.
How mlflow does it
Regression pipelines offer Linear Regression (lr), Ridge Regression (ridge), Lasso Regression (lasso) and Elastic Net (en). Plain linear regression has no tunable parameters. Ridge, lasso and elastic net expose alpha and fit_intercept (and l1_ratio for elastic net); each parameter has a tooltip that explains what raising or lowering it does. For classification the matching linear models are Logistic Regression and Ridge Classifier. Add a scaler step before any of them. The docs pages linked below list every algorithm and parameter.
Common mistakes
- Forgetting to scale before a penalised model. The penalty treats a weight on a tiny-valued column differently from one on a huge-valued column. Add a scaler step first.
- Reading a weight as proof of cause. A weight describes the pattern in the data, with the other inputs held fixed. Correlated inputs share the credit in unstable ways. Do not present weights as causes.
- Using a linear model on a curved relationship without adding features. The fit will be biased. Add squares, logs or interactions, or move to trees.
- Letting a few extreme rows decide the line. Squared error amplifies outliers. Look at the residual plot and see Outliers.
- Raising alpha until the score looks good on the training data. Training error always rises with alpha. Choose alpha by cross-validated score.
Try it in mlflow
Compare plain, ridge and lasso regression
- Open Pipelines and pick a regression pipeline (for example one built on a house-price or concrete-strength dataset).
- Open the Models part, search for Linear Regression, Ridge Regression and Lasso Regression, and add all three.
- Click Train 3 algorithms and open the run when it finishes.
- Compare the cross-validated R2 and RMSE of the three candidates. If they are close, prefer the simplest.