Rules versus examples Watch
In normal programming you write the rules. In machine learning you give examples with answers, and the computer finds the rule. For a flat's rent, nobody writes the perfect formula - a model learns it from flats already rented.
The words every AI professional uses Watch
| Word | Meaning | In our example |
|---|---|---|
| Feature | An input the model uses | Size of the flat in square feet |
| Label | The answer we want to predict | Monthly rent in rupees |
| Training data | Examples the model learns from | 6 flats with known rents |
| Test data | Examples kept hidden, used only to check the model | 2 different flats |
| Model | The rule it learns | rent = w x size + b |
Build a model from scratch Watch
Our model is a straight line: rent = w x size + b. "Training" means finding the w (how much rent rises per square foot) and b (the starting amount) that fit the training flats best. The data below is a made-up example.
# size of a flat (square feet) -> monthly rent (rupees); made-up example data
train = [(450, 11000), (600, 14500), (750, 17000), (900, 21000), (1100, 25500), (1300, 29000)]
test = [(500, 12500), (1000, 23500)]
# "training": find the straight line rent = w * size + b that fits the training data best
n = len(train)
mean_x = sum(x for x, _ in train) / n
mean_y = sum(y for _, y in train) / n
w = sum((x - mean_x) * (y - mean_y) for x, y in train) / sum((x - mean_x) ** 2 for x, _ in train)
b = mean_y - w * mean_x
print(f"Learned: rent = {w:.2f} x size + {b:.0f}")
# Output:
# Learned: rent = 21.50 x size + 1392
The formula is called least squares: it picks the line with the smallest total squared distance from the training points. You do not need to memorise it - libraries do this for you - but now you know there is no magic inside.
Test on data the model has never seen Watch
A model is only useful if it works on new data. So we predict the two hidden test flats and measure the error.
errors = []
for size, real in test:
guess = w * size + b
errors.append(abs(guess - real))
print(f"{size} sq ft: predicted Rs {guess:,.0f}, real Rs {real:,}")
print(f"Average error: Rs {sum(errors) / len(errors):,.0f}")
# Output:
# 500 sq ft: predicted Rs 12,142, real Rs 12,500
# 1000 sq ft: predicted Rs 22,892, real Rs 23,500
# Average error: Rs 483
An average error of about 483 rupees on rents of 12,000 to 24,000 rupees is good for such a simple model. This measure is called the mean absolute error: the average size of the mistakes, in the same units as the answer.
Why models fail in real life Watch
- Too few features: real rent also depends on the area, floor and furniture. One feature can only explain so much.
- Overfitting: a model so complex that it memorises the training data, noise included, and does badly on new data.
- Biased or old data: a model learns whatever the examples teach - including their mistakes and unfairness.
Lesson learned Watch
Machine learning finds rules from examples. Every model follows the same routine: choose features and a label, train on one part of the data, test on a separate part, and measure the error honestly before you trust it.
Frequently asked questions
What is the difference between training data and test data?
Training data is what the model learns from. Test data is kept aside and used only to check how well the model works on examples it has never seen.
What is a feature in machine learning?
A feature is an input the model uses to make a prediction, such as the size of a flat when predicting its rent.
What is overfitting?
Overfitting is when a model learns the training examples too closely, including their noise, so it performs well on them but badly on new data.
Last checked: November 2026. Tools and free tiers change - check each official page. All examples are original; the data in them is made up for teaching.