In normal programming, a person writes the rules: if the temperature is above 21 °C, turn the heating off. In machine learning, the computer works out the rules itself from examples. Show it thousands of emails labelled "spam" or "not spam", and it learns what spam tends to look like. Every modern AI system, including the large language models behind chatbots, is built this way. This guide covers the ideas underneath all of them.
Three kinds of learning
| Type | What it learns from | Examples |
|---|---|---|
| Supervised | Examples with the right answer attached (labels) | Spam filters, predicting house prices, recognising handwritten digits |
| Unsupervised | Examples with no answers; it finds structure on its own | Grouping customers by behaviour, spotting unusual readings |
| Reinforcement | Rewards and penalties from trying things | Game-playing AIs, robot control, tuning chatbot behaviour from feedback |
The inputs are called features (for a house: floor area, bedrooms, location) and the answer to predict is the label or target (the price).
The simplest model: fitting a line
Suppose you have five examples where the output is roughly twice the input: (1, 2.1), (2, 3.9), (3, 6.2), (4, 7.8) and (5, 10.1). A model with the form y = m × x + c has two parameters, m and c. "Training" means choosing them to make the predictions as close as possible to the real answers, usually by minimising the sum of the squared errors. For these points that gives m = 1.99 and c = 0.05. Ask it about an input of 6 and it predicts about 12.0.
That's machine learning in miniature. Real models have far more parameters: a neural network might have millions, a large language model billions. But the idea is the same: adjust the parameters until the model's outputs match the examples, using an algorithm such as gradient descent to make the adjustments.
Training data and test data
A model that has memorised its examples isn't useful; what matters is how well it does on data it has never seen. So the data is split: most is used for training, and a portion, often around 20%, is held back as test data and only used at the end to measure performance. If you tune the model on the test data, it stops being a fair test.
Overfitting and underfitting
- Underfitting: the model is too simple to capture the pattern, like a straight line through data that curves. It does badly on training and test data alike.
- Overfitting: the model is so flexible that it fits the noise in the training data as well as the pattern. It scores brilliantly on training data and badly on new data.
More data, simpler models and techniques that penalise complexity all reduce overfitting. You can watch it happen in the neural network trainer.
Measuring a classifier
Accuracy alone can mislead. Take a spam filter tested on 100 emails, 20 of which are spam. It catches 15 of the spam (true positives), misses 5 (false negatives), wrongly flags 3 good emails (false positives) and correctly passes 77.
| Measure | Question it answers | Result |
|---|---|---|
| Accuracy | How many did it get right overall? (15 + 77) ÷ 100 | 92% |
| Precision | When it says spam, how often is it right? 15 ÷ (15 + 3) | 83% |
| Recall | Of the real spam, how much did it catch? 15 ÷ (15 + 5) | 75% |
A filter that marked nothing as spam would still score 80% accuracy here, because 80 of the 100 emails are fine, while catching no spam at all. Which measure matters depends on the cost of each mistake: a medical test needs high recall; a spam filter needs high precision so real emails aren't lost.
Where the data comes from matters
A model can only learn what's in its data. If the training data under-represents some people or situations, the model will do worse for them, and may repeat unfair patterns from the past. The AI vs bias simulator works through real cases. Good practice is to check results separately for different groups, not just overall.
Going deeper
Least squares by hand
For a straight line, the best m and c have a direct formula: m = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)², and c = ȳ − m x̄, where x̄ and ȳ are the means. Most models don't have a neat formula like this, which is why they're trained step by step with gradient descent instead.
Validation data
Choosing settings such as how many layers a network has, or how long to train, is itself a kind of fitting. So practitioners split off a third set, the validation set, to choose those settings, keeping the test set untouched until the very end.
Features matter
For simpler models, choosing and preparing good features (scaling numbers to similar ranges, turning categories into numbers, combining raw measurements) often improves results more than choosing a cleverer algorithm. Deep learning reduces this work by learning features from raw data, at the cost of needing much more of it.
Sources
The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.