How a Machine Actually Learns
You already know how to do this
Say you move to Bengaluru for a new job and start hunting for a flat. On day one, you have no idea what a fair rent is. A broker says ₹25,000 for a small 1BHK and you cannot tell if that is normal or a rip-off.
Two weeks and fifty flats later, something has changed. You see a flat and think "that should be around ₹18,000" before anyone tells you the price. And you are usually close.
Nobody gave you a formula. You saw enough examples, noticed that bigger flats cost more, and kept correcting your gut feeling every time a real price surprised you. That is learning from examples. A machine learns in almost exactly the same way, just with numbers instead of gut feelings.
The idea: guess, check, fix, repeat
When a machine "learns", it is not reading or understanding anything. It is doing something much simpler. It makes a guess, checks how wrong the guess was, and changes itself a little so the next guess is less wrong. Then it does that again. And again. Thousands of times.
01
Guess
Predict with the current setting.
02
Compare
Put the guess next to the real answer.
03
Measure
Work out how wrong it was.
04
Adjust
Nudge the setting to be less wrong.
↻ repeat until the error stops shrinking
That is really all training is. The rest of this course is mostly about doing those four steps well: better ways to guess, better ways to measure wrongness, and smarter ways to adjust.
Watching it happen with real numbers
Let us teach a machine to guess rent from flat size. We give it four flats whose rents we already know:
Key Points
- 500 sq ft rents for ₹10,000
- 700 sq ft rents for ₹15,000
- 800 sq ft rents for ₹16,000
- 1,000 sq ft rents for ₹20,000
The machine will guess rent with one simple rule: rent = w × size. Here w is just "rupees per square foot". The machine does not know the right value of w. Finding it is the whole job.
It starts with a random guess, say w = 10. Then it checks: 500 × 10 = ₹5,000, but the real rent is ₹10,000. The 1,000 sq ft flat comes out at ₹10,000 against a real ₹20,000. Every guess is too low. So it pushes w up.
Try w = 15. Now the 1,000 sq ft flat comes out at ₹15,000. Closer, but still too low. Push w up again. Try w = 25, and suddenly every guess is too high, so w has to come back down.
Keep going and w settles at about 20.3. At that value, the guesses are as close to the real rents as this simple rule can get.
Tip:Notice it never lands on a perfect answer. The 700 sq ft flat costs a bit more per square foot than the others, like real flats do. No single w fits all four exactly, so the machine settles for the w that is least wrong overall. That is normal, and it is true for almost every real model.
The proper words for what just happened
You just watched a model being trained. Here are the names for each piece, so the rest of the course makes sense:
Key Points
- Training data: the four flats with known rents. The examples the machine learns from.
- Parameter (also called a weight): w, the number the machine is allowed to change. Real models have thousands or even billions of these, but the idea is the same.
- Prediction: the machine's guess, like "₹15,000 for this flat".
- Error (also called loss): how far the prediction was from the real answer.
- Training: the whole loop of guess, measure error, adjust w, repeat.
- Model: what you have at the end. Here, it is the rule rent = 20.3 × size.
In code
Here is that exact loop in plain Python. No libraries. Paste it into Google Colab and run it. (If you have never used Colab, that is fine. Chapter 2 covers it. For now, just read the output.)
1sizes = [500, 800, 1000, 700]
2rents = [10000, 16000, 20000, 15000]
3
4w = 10 # first guess: ₹10 per square foot
5
6for step in range(7):
7 predictions = [w * s for s in sizes]
8 errors = [p - r for p, r in zip(predictions, rents)]
9 avg_error = sum(errors) / len(errors)
10 print(f"step {step}: w = {w:.2f}, average error = {avg_error:,.0f}")
11 w = w - avg_error / 1000
12
13print(f"\nlearned: ₹{w:.2f} per sq ft")
14print(f"a 900 sq ft flat should rent for about ₹{w * 900:,.0f}")step 0: w = 10.00, average error = -7,750 step 1: w = 17.75, average error = -1,938 step 2: w = 19.69, average error = -484 step 3: w = 20.17, average error = -121 step 4: w = 20.29, average error = -30 step 5: w = 20.32, average error = -8 step 6: w = 20.33, average error = -2 learned: ₹20.33 per sq ft a 900 sq ft flat should rent for about ₹18,299
A negative error means the guesses were too low. Look at how it shrinks: -7,750, then -1,938, then -484, and so on until it is almost zero. That is the machine learning.
The line doing the learning is w = w - avg_error / 1000. If the guesses are too low, the error is negative, so subtracting it pushes w up. If the guesses are too high, it pushes w down. The 1000 controls how big each nudge is. You will meet that idea properly as the "learning rate" in Chapter 4.
The real test: flats it has never seen
Look at the last line of the output. The machine never saw a 900 sq ft flat, yet it confidently says about ₹18,299. That is the entire point of machine learning. Learning from old examples is only useful if it works on new ones.
Think about two students preparing for boards. One memorises the answers to last year's paper. The other understands the chapters. On last year's paper, both score 100. On this year's paper, only one of them does well.
Machines can be either student. A model that only memorised its training examples is useless on new data. Doing well on new, unseen data is called generalisation, and it is what we actually care about. Chapter 5 is all about catching models that memorise instead of learn.
Where people get confused
Key Points
- "The computer understands rent." It does not. It found a number that made its guesses less wrong. There is no understanding behind it.
- "It keeps learning every time I use it." Usually not. Training happens first. After that, the model just makes predictions with the w it already found. Retraining is a separate step someone has to run.
- "Training finds the perfect answer." Real data is messy, so there is almost never a perfect answer. Training finds the least wrong one.
- "More training is always better." Train too hard on too few examples and the model starts memorising them. That is the first student from the boards example.
Quick check
What are the four steps a machine repeats while it learns?
Guess (make a prediction), compare it with the real answer, measure how wrong it was, and adjust its parameters to be a little less wrong. Then repeat.
The model predicts ₹12,000 for a flat that actually rents for ₹16,000. Should w go up or down?
Up. The prediction was too low, so w needs to be bigger to push the next prediction higher.
Why do we care how a model does on flats it never saw during training?
Because that is how it will be used. A model is only useful if it works on new data. Doing well only on the examples it trained on could just mean it memorised them.
Recap
Key Points
- Machine learning is a loop: guess, compare, measure the error, adjust. Repeat many times.
- The numbers the machine adjusts are called parameters (or weights). What you end up with is the model.
- Real data is messy, so training finds the least wrong answer, not a perfect one.
- The real goal is generalisation: doing well on new data, not just on the examples it trained on.
Next up: supervised and unsupervised learning. In this lesson, every flat came with its real rent. What happens when the data has no answers at all?
Theia
Can you explain this concept?
Practise How a Machine Actually Learns with realistic interview questions. Choose spoken or typed answers, get feedback on your reasoning, then return to the material that needs work.
Sign in to practise this conceptThe session stays focused on this lesson. Your selected interview level sets the bar.