Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 04

Regularization

Nine beginner steps: why large weights overfit, Ridge and Lasso formulas, a marks example, and a mind map before the quiz.

Beginner20 min
Three fits on one study-hours table: no fine, a useful fine, and a fine that is too heavy

What You Will Learn

Nine steps. Click one to jump.

1. What is Regularization?

On the gradient descent page, the farmer walks downhill until the miss is small. That walk is allowed to make a weight as large as it wants, if a large weight shrinks the miss on the students he already knows.

Regularization adds a second rule: large weights pay a fine.

total loss = how wrong the predictions are + fine for big weights

The walk is the same. The hill is taller wherever a weight is huge, so the farmer stops at smaller weights.

Picture a tailor. Without a rule, he adds a tuck for every student so the coat fits those five people exactly. The next student cannot wear it. The fine says: fewer tucks. The coat is a little loose on the five, and it still fits a new person.

2. Types of Regularization

We fine the weights, not the intercept c. The line still needs a starting height.

Name Also called The fine What happens to weights
Ridge L2 α × (square of each weight) Every weight shrinks. Useful ones stay. Useless ones get small, but not always exactly 0.
Lasso L1 α × (absolute value of each weight) A useless weight can land on exactly 0 and drop out.
Elastic Net L1 + L2 a mix of both fines Shrinks like Ridge and can still zero a few weights like Lasso.

α (alpha) is how loud the fine is.

  • α = 0 means no fine. Ordinary line. Same as panel 1 above.
  • A small α is panel 2.
  • A huge α is panel 3. Even the useful hours weight is crushed.

3. Where is it used?

Use a fine when a new person matters more than a perfect score on the rows you already saw.

  • Many columns, few rows. Five students and twenty details. A free fit can memorize all five.
  • A column that only looks useful. Pen colour, breakfast, or a lucky seat. It lines up with the five marks by chance.
  • Columns that move together. Hours studied and pages read. Both want a big weight. The fine shares the credit instead of letting both explode.
  • You will predict someone new. A sixth student, a new house, a new patient.

Skip the fine when you only need to describe these exact five rows and will never predict anyone else.

4. Which algorithms use it?

The idea is one idea. The name on the algorithm changes.

Algorithm How the fine shows up
Linear regression Ridge or Lasso on the weights m
Logistic regression Same fine, while the model learns yes/no
Support vector machine A penalty that keeps the boundary from hugging every point
Neural networks Weight decay is the Ridge fine on the network's weights

Gradient descent still does the stepping. Regularization only changes the loss it steps on.

5. What problem do those algorithms face?

Stay with the study-hours table, and add one useless column. Breakfast does not cause marks. On these five rows it happens to be 1 on the two noisier days.

Hours Marks Breakfast
1 30 0
2 45 1
3 50 0
4 68 1
5 70 0

The honest pattern is still about 10 marks per hour, plus 20:

marks ≈ 10 × hours + 20

A model with no fine can also use breakfast. One such fit is:

marks ≈ 10 × hours + 20 + 8 × breakfast

On the five training rows that extra +8 pulls 2 hours toward 48 and 4 hours onto 68. Training looks clever. It memorized a coin-flip column.

Now a new student, 6 hours, who did eat breakfast. Breakfast still should not add marks. Honest prediction: 10 × 6 + 20 = 80. The memorizing model says 80 + 8 = 88. Those 8 marks are the problem. The algorithm fit the training table too tightly.

With only a handful of rows, a flexible model will do this whenever you let every weight grow for free.

6. How does Regularization solve the problem?

Write the loss in words, then in symbols. MSE is the average squared miss. α is the strength of the fine. m is the hours weight. p is the breakfast weight.

Ridge: loss = MSE + α × (m² + p²)

Lasso: loss = MSE + α × (|m| + |p|)

Watch one pair of weights. Hours weight stays near 10. Breakfast weight is the one that cheats.

Hours weight m Breakfast weight p New student: 6 hours, breakfast = 1
No fine 10 8 10×6 + 20 + 8 = 88
Lasso, fine strong enough 10 0 10×6 + 20 + 0 = 80
Fine much too strong about 0 0 predicts nearly the same mark for every hour

The picture at the top is the same comparison as a curve. No fine wiggles through the dots. A right-sized fine stays near the straight hours line. Too much fine goes flat, and then both the training students and the new student look wrong.

Why the two fines behave differently, in one glance:

Weight p Ridge cost, α = 1 (p²) Lasso cost, α = 1 (absolute p)
8 64 8
2 4 2
0 0 0

Ridge charges a lot for a large p, so 8 is pulled down, often to something small but not zero. Lasso charges in a straight line, and the downhill step can sit exactly at 0. That is feature selection: breakfast leaves the model.

Elastic Net sends both bills, so a group of overlapping columns can shrink together and a junk column can still drop out.

Put columns on a similar scale before you compare their fines. Hours are 1–5. A salary column might be 30,000–70,000. The salary weight looks tiny only because the column is huge, and the fine would punish the wrong one.

7. Key Takeaways

  • Regularization is a fine for large weights, added on top of the usual miss.
  • Gradient descent does not change. The loss changes, so the steps prefer smaller weights.
  • Ridge squares weights and shrinks them all.
  • Lasso uses absolute values and can set a useless weight to 0.
  • Elastic Net uses both.
  • α = 0 is an ordinary line. α too large underfits: the model ignores real patterns such as hours.
  • Judge the model on a new student, not on a perfect score for the five you trained on.

8. Mind Map

Mind map: regularization is a fine, with Ridge, Lasso, Elastic Net, and alpha around it

Read the centre first, then the four corners. If you can say each box in your own words, you have the lesson.

9. Quiz

The questions below use this story: a fine on weights, Ridge versus Lasso, and what happens when α is 0 or much too large. Answer from the tables, not from memory of a library name.

Check your understanding

Regularization Quiz

What does regularisation add to the loss function?

1 / 11