AI Learning Courses
AI Learning Courses › Courses › Become an AI Professional › Lesson 5
AI PROFESSIONAL ยท LEVEL 1 · LESSON 5 · 9 MIN READ

How machine learning learns: train, test and measure (build one from scratch)

Machine learning sounds like magic until you build a tiny model yourself. In this lesson you teach Python to predict flat rents from size using only a few lines of code - and learn the train, test and measure routine behind every model.

Turn on JavaScript to watch the animated lesson. The full tutorial text is below.

Rules versus examples Watch

Traditional programming: people write the rules. Machine learning: the computer finds the rules from examples.
Traditional programming: people write the rules. Machine learning: the computer finds the rules from examples.

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

WordMeaningIn our example
FeatureAn input the model usesSize of the flat in square feet
LabelThe answer we want to predictMonthly rent in rupees
Training dataExamples the model learns from6 flats with known rents
Test dataExamples kept hidden, used only to check the model2 different flats
ModelThe rule it learnsrent = 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

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.