Machine learning, step by step

Cost Function, Super Simple

No scary equations. Just one idea: a score for how wrong your model is.

1What are we trying to do?

We want a model that guesses a house's price from its size. We know the real prices of a few houses:

House sizeReal price
1000 sq ft₹30 lakh
1500 sq ft₹45 lakh
2000 sq ft₹60 lakh

At first, a model's guesses will be wrong. So how do we measure how wrong?

2The cost function is just a score

Think of it like golf: a lower score is better.

5Model A cost
Good guesses ✅
50Model B cost
Bad guesses ❌
Low cost = good modelZero cost = perfect predictions

3How is the score worked out?

Take one house. Real price ₹30 lakh, model said ₹25 lakh.

Why square? A mistake of −5 and a mistake of +5 would cancel to zero if we just added them. Squaring makes every mistake positive, and it punishes big mistakes extra hard.

The official name is Mean Squared Error. Andrew Ng's formula also divides by 2. That only makes later math tidier and doesn't change the idea.

4The model is just a "price per 1000 sq ft"

Our model is: price = w × size. The number w is how many lakh each 1000 sq ft is worth. The computer's job is to find the best w.

Try it. Move w and watch the guesses and the cost change.

COST (average of the squares)

5The big idea: cost vs. w makes a bowl

Now imagine trying every w and plotting the cost for each. You always get a U-shape, like a bowl:

COST

Drag the slider and watch the ball roll up and down the bowl. Finding the bottom is what "training" means.

6How does the computer find the bottom?

Imagine standing on a hill in the dark and wanting to reach the lowest spot. You:

This is called gradient descent. Press the button to take steps with the ball above. The step size can be changed here:

Try the biggest step size. The ball overshoots the bottom and flies out. Too tiny, and it takes forever. Not too big, not too small is best.

Gradient descent comes next in the course. For now, being able to explain the cost function is the goal.

7Remember this

ModelMakes the guesses
wThe setting that controls the guesses
Cost functionScore of how wrong the guesses are
Minimizing costFinding the w with the lowest score
Gradient descentThe method that finds it

Before moving on, can you answer these in your own words? Tap to check.

What does a cost function measure?

How wrong the model's predictions are, as one number. Lower is better.

Why do we square the mistakes?

So positive and negative mistakes can't cancel out, and big mistakes count more.

Why do we want the cost to be as small as possible?

Smaller cost means the predictions are closer to the real answers, so the model is better.

8Quick quiz

Five questions. Tap an answer to see if you got it.