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 size | Real 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.
Good guesses ✅
Bad guesses ❌
3How is the score worked out?
Take one house. Real price ₹30 lakh, model said ₹25 lakh.
- Find the mistake: 25 − 30 = −5
- Square it: (−5) × (−5) = 25
- Do this for every house, then take the average. That average is the cost.
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.
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:
- w too small → guesses too low → high cost
- w too big → guesses too high → high cost
- w just right → guesses match → lowest cost (the bottom of the bowl)
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:
- Feel which way goes downhill.
- Take a small step that way.
- Repeat until you can't go lower.
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
| Model | Makes the guesses |
| w | The setting that controls the guesses |
| Cost function | Score of how wrong the guesses are |
| Minimizing cost | Finding the w with the lowest score |
| Gradient descent | The 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.