Lesson 20
Lasso Regression: Drop a Power with Python
Use lasso to drop extra powers on Ananya's Hyderabad house curve. The L1 penalty can set a weak scaled weight to exactly zero.
Your progress: 0 of 13 steps
0 of 9 code cells run · saved in this browser only
Step 1 of 13
Imagine
What real-world problem are we solving?
Four powers that should not be there
Ridge kept all six powers of house size and pulled the weights down. The curve got calmer, and every power was still in the prediction. Ananya does not want a price that depends on if and already explain the bend. She wants a penalty that can delete a power.
On these same twelve flats, degree 2 was the honest curve: about 83.6 lakh at 2000 sq ft, test RMSE 2.94. Degree 6 with no penalty still prices a 5000 sq ft villa at about 22,454 lakh. Lasso starts from those six powers and is allowed to set some weights to zero.
Step 2 of 13
Brainstorm
What would you try first?
Shrink, or remove
Ridge makes a large weight expensive, but the weight almost never lands on zero, so you still have to explain , , and . A penalty on the absolute value of each weight has a corner at zero. Once a weight reaches that corner, the cheapest choice is to leave it there. The power drops out of the formula. The cost of that choice is a new alpha, on a different scale from ridge, and a model that can still go wild outside the houses we have seen.
Step 3 of 13
Discover
Why do we need this algorithm?
Discover lasso regression
The fit minimises the squared misses plus a charge for every weight that is not zero:
scikit-learn averages the squared error (the ) before adding the penalty. Ridge, in the same library, does not average first. So Lasso(alpha=0.2) is not a milder Ridge(alpha=1). Tune lasso on its own path.
| Symbol | Say it as | Meaning here |
|---|---|---|
| the penalty strength | You choose it. Larger pushes more weights onto exactly 0 | |
| $ | w_j | $ |
| the number of training houses | Here, 8. It sits in the formula, which is why this alpha is not ridge's alpha |
There is no tidy matrix formula. The practical rule, once each column is scaled to average 0 and spread 1, is a soft threshold. Let be how strongly column still lines up with the prices the other columns have not explained. Then
(and when is negative). The intercept is the training average, 70.25 lakh, and it is not penalised. Scale the columns on the training houses only, or alpha secretly punishes the largest power.
Step 4 of 13
Visualize
What does it look like?
See a power disappear
The slider starts at alpha 0.2. Powers still in the model are listed under the chart. The dashed curve is degree 6 with no penalty, drawn only across the houses we have.
Move toward 0.01 and a high power comes back. Move toward 10 and the two remaining weights get too small to follow the bend.
Step 5 of 13
Mathematics
What equation describes it?
One column, so you can check the threshold
The degree-6 path is a loop. This section uses only size, scaled, with alpha 10, on the eight training houses. You can do it on paper.
The training sizes have mean and population standard deviation . After scaling, their correlation with price is
Alpha is 10, and 37.08 is larger than 10, so the weight is not zero:
A 2000 sq ft house has scaled size , so
If alpha were 40, which is past 37.08, the weight would be exactly 0 and every size would be predicted at 70.25 lakh. That is the difference from ridge: the weight is allowed to arrive at zero and stay there.
Step 6 of 13
Dataset
What data are we using?
The same twelve houses
Edit a price in the notebook and the table follows. New rows you do not list in test_rows stay in the training set.
The same twelve houses as the polynomial and ridge lessons. Illustrative teaching data, not real listings.
size_kinput feature (X)1000 sq ft- Built-up area. 1.5 means 1500 sq ft. This is the input x.
price_lakhtarget (y)₹ lakh- Sale price. 100 lakh = ₹1 crore. This is what we predict, y.
| Row | size_k | price_lakh | Used for |
|---|---|---|---|
| 1 | 0.6 | 27 | ● train |
| 2 | 0.8 | 29 | ● train |
| 3 | 1 | 40 | ▢ test |
| 4 | 1.2 | 43 | ● train |
| 5 | 1.4 | 55 | ● train |
| 6 | 1.5 | 56 | ▢ test |
| 7 | 1.8 | 69 | ● train |
| 8 | 2 | 86 | ● train |
| 9 | 2.2 | 93 | ▢ test |
| 10 | 2.5 | 118 | ● train |
| 11 | 2.8 | 135 | ● train |
| 12 | 3 | 157 | ▢ test |
Every third house (houses 3, 6, 9 and 12) is held back as the test set. The model never sees them while learning.
Step 7 of 13
Manual calculation
Can we compute it by hand?
Check the one-column result
This cell should print rho 37.0771, weight 27.0771, and 83.67 for 2000 sq ft.
Step 8 of 13
Python notebook
How do we run it in code?
Let lasso choose the powers
Build through , scale them on the training houses, then Lasso(alpha=0.2). The weights come out about
Four of the six powers are gone. What remains is size and size squared, which is the pair the polynomial lesson chose by comparing test error. A 2000 sq ft house is 83.48 lakh, next to degree 2's 83.56. Training RMSE is 2.32 lakh. Test RMSE is 3.03 lakh, next to degree 2's 2.94.
The 5000 sq ft guess is still 358 lakh. Deleting did not make a villa outside the table safe. Ridge at its own alpha 1 had a lower test RMSE on these four houses (1.04) and kept every power. Lasso's win here is the shorter formula, not a better score. The notebook asks scikit-learn for a tight tolerance, so these weights finish moving. The library default stops a little early and the last digits shift.
Step 9 of 13
Experiment
What changes when we change things?
Change only alpha
Model parameters
Learned by the algorithm from the data
- intercept
- Predicted price when every scaled column is 0. With standard scaling this is the average training price, 70.25 lakh. Lasso does not penalise it.
- weights w1 … w6
- Learned weights on the scaled powers x, x², … x⁶. Lasso can set one of these to exactly zero, which drops that power.
Hyperparameters
Chosen by you before training
- alpha
- How hard a non-zero weight is punished. You choose it. In scikit-learn this alpha is not the same number as ridge's alpha, because lasso averages the squared error first.
- degree
- How many powers of size to build before the penalty. Fixed at 6 so you can see which powers lasso keeps.
| Alpha | Powers kept | Train RMSE | Test RMSE | 5000 sq ft |
|---|---|---|---|---|
| 0.01 | , , | 1.92 | 6.26 | −230.4 |
| 0.2 | , | 2.32 | 3.03 | 358.4 |
| 1 | , | 2.52 | 3.55 | 353.1 |
| 10 | , | 10.31 | 13.36 | 293.0 |
- Alpha 0.01 drops and still carries a negative (about −7.73). The villa price goes negative. A small penalty is not automatically a sensible model, and which powers survive can change when the solver is allowed to finish.
- Alpha 0.2 is the first setting in this list that drops every power above . Test RMSE is close to the simple degree-2 curve.
- Alpha 1 keeps the same two powers and fits a little worse. The zeros did not flip back on.
- Alpha 10 still has two powers, and both errors are large. Sparse does not mean "well fitted". At alpha 30 only survives and the test RMSE is about 38.
Four test houses make 3.03 look precise. Treat 0.2 as "this is where the extra powers leave", then compare that two-power model with degree 2 before you ship it.
Step 10 of 13
Evaluation
How good is the model?
Score the two-power model
MAE
mean(|y - ŷ|)
Good for. Typical miss in the target's own units.
Limit. Treats a small and a huge miss almost alike.
MSE
mean((y - ŷ)²)
Good for. Training objective; punishes big misses.
Limit. Units are squared, so hard to read.
RMSE
√MSE
Good for. Typical miss, with big misses weighted more.
Limit. Sensitive to outliers.
R²
1 - SSE/SST
Good for. Share of variation the model explains.
Limit. Can look great on training data while the model overfits.
| Metric | Formula | Good for | Limitation |
|---|---|---|---|
| MAE | mean(|y - ŷ|) | Typical miss in the target's own units. | Treats a small and a huge miss almost alike. |
| MSE | mean((y - ŷ)²) | Training objective; punishes big misses. | Units are squared, so hard to read. |
| RMSE | √MSE | Typical miss, with big misses weighted more. | Sensitive to outliers. |
| R² | 1 - SSE/SST | Share of variation the model explains. | Can look great on training data while the model overfits. |
For lasso at alpha 0.2, on the four held-out houses:
| Metric | Value | How to say it |
|---|---|---|
| MAE | 2.82 lakh | The typical miss on these four flats |
| RMSE | 3.03 lakh | Close to degree 2's 2.94, and far from a reason to trust 5000 sq ft |
| 0.995 | High, and still based on four houses |
Alpha 0.01 has a better training RMSE (1.92 against 2.32) and a worse test RMSE (6.26). Training error would have kept the negative villa price.
Step 11 of 13
Applications
Where is it used, and when is it wrong?
When to use lasso regression
Use it when a linear model has more columns than you want to explain, and some of them can leave. The absolute-value penalty drops a column by setting its weight to zero. On these houses it dropped four powers and left the curve the earlier lesson already liked.
| Check | Use lasso when | Do not use it when |
|---|---|---|
| Columns | Several inputs, or extra powers, and you want a short formula | Two columns carry the same signal and you need both kept. Ridge shrinks them together |
| Weights | A weak column should be allowed to hit exactly zero | You only want smaller weights. Ridge does that and rarely hits zero |
| Scale | You standardise the training columns first, then tune alpha on lasso's own scale | You paste ridge's alpha into Lasso and expect the same fit |
| Fit | Test error stays near a simpler model you already trust | Alpha is so large that the remaining weights underfit (here, alpha 10 and beyond) |
| Range | You predict inside the training sizes | You read a zero weight as permission to price a 5000 sq ft house. Alpha 0.2 still says about 358 lakh |
Where lasso regression is used
- Many possible inputs: measurements, flags, or powers, when only a few should appear in the explanation.
- A check on a polynomial: ask which powers survive, then fit that smaller degree directly.
- A sparse baseline before a more flexible model, so you know which columns were doing the work.
Benefits: the formula can get shorter, the dropped columns are obvious, and the fit is still a weighted sum.
Limitations: correlated columns compete, and lasso often keeps one and drops the other even when both matter. Alpha is not comparable with ridge's alpha. A tiny alpha can keep a harmful high power (the −230 lakh villa). With more columns than rows the zeros depend on the solver's path. Four test houses will flatter one alpha.
Common mistakes: comparing a lasso weight with a ridge weight; choosing alpha by training error; fitting the scaler on the test rows; celebrating a zero without looking at the houses that remain.
| Situation | Try instead |
|---|---|
| Keep every correlated column, only smaller | Ridge regression |
| One smooth bend and you will pick the degree yourself | Polynomial regression |
| Shrink and also allow exact zeros | Elastic net |
| The points already sit on a straight line | Linear regression |
Step 12 of 13
Assessment
Did you understand?
Check your understanding
Replace the ... in the challenge, then take the quiz. The challenge should print 4.
Step 13 of 13
Recap
What did we learn?
Recap
- Problem: ridge kept every power. Ananya wants powers that are not helping to leave the formula.
- Idea: add to the squared error. A weight can land on exactly zero.
- Scale, and do not reuse ridge's alpha. scikit-learn averages the squared error before the lasso penalty.
- By hand: scaled size has correlation 37.08. Alpha 10 leaves a weight of 27.08 and predicts 83.67 lakh at 2000 sq ft. Alpha 40 would make that weight exactly 0.
- In practice: degree 6, scaled, alpha 0.2 keeps and only. Test RMSE is 3.03 lakh. A 2000 sq ft house is 83.48 lakh. A 5000 sq ft house is still about 358 lakh.
- Small alpha is not safe. Alpha 0.01 keeps a negative and predicts about −230 lakh outside the data. Alpha 10 keeps two powers and underfits.
- When to use it: you want a shorter linear formula and can stand to drop a column. Use ridge when correlated columns should all stay. Keep degree 2 when you already know that curve fits.
Next: Elastic net mixes the two penalties. The penalty idea itself is on Regularization. The full notebook is in the ML Lab.





