What Is Machine Learning
How a program can find its own rule from examples instead of being told it. Learn the core words, watch a rule being learned, and see when ML fits.
On this page
You run a small delivery service, and customers keep asking whether their parcel will be late. You could sit down and write rules. Late if the distance is over some number of kilometres. Unless it is a weekday morning. Unless the driver is new. Every week you find a case your rules got wrong, and every fix makes the rules harder to read.
You already have something better than your guesses: a spreadsheet of past deliveries, each with its distance and whether it actually arrived late. Machine learning means letting the program read that spreadsheet and work out the rule itself.
Rules versus examples
In ordinary programming, you write the rule and the computer applies it. In machine learning, you give the computer examples together with the right answers, and it finds a rule that fits them. You then use that rule on new cases it has never seen.
The rule it finds is called a model. A model is just a function: it takes the facts about one case and returns a guess. What makes it "learned" is that some numbers inside it were chosen by looking at the examples, not typed in by you.
The words you will keep seeing
Every lesson in this module uses the same handful of terms. Here they are on the delivery example.
| Word | Meaning | In the delivery example |
|---|---|---|
| Example | One past case | One delivery from the spreadsheet |
| Feature | A fact you know before the answer | The distance in km |
| Label | The right answer for that example | Late or on time |
| Parameter | A number inside the model that learning sets | The cut-off distance |
| Training | Choosing parameters that fit the examples | Finding the best cut-off |
| Prediction | Using the trained model on a new case | "This 7 km delivery will be late" |
Watch a rule being learned
Start with the simplest possible model: "a delivery is late if its distance is at least t km". The model has one parameter, t. Training means picking the best value for it.
Here are seven past deliveries, sorted by distance. Notice the 10 km one: it arrived on time even though longer trips are usually late. Real data has cases like that. Maybe the roads were empty that day.
The plan is blunt: try each distance as the cut-off, count how many past deliveries that rule gets wrong, and keep the cut-off with the fewest mistakes.
for each candidate t: mistakes = 0 for each delivery: guess = late if distance >= t if guess != actual: mistakes += 1 keep t if it has the fewest mistakes so far- km02t13253648510612guess0late1late2late3late4late5late6lateactual0ok1ok2ok3late4late5ok6late
- t
- 2
- mistakes
- 4
- best t
- 2
- fewest
- 4
1Try
t = 2. Every delivery is at least 2 km, so the rule says all of them are late. The four on-time deliveries are wrong. - km0213t253648510612guess0ok1late2late3late4late5late6lateactual0ok1ok2ok3late4late5ok6late
- t
- 3
- mistakes
- 3
- best t
- 3
- fewest
- 3
2Try
t = 3. Now the 2 km delivery is called on time, which is right. Three mistakes left. - km021325t3648510612guess0ok1ok2late3late4late5late6lateactual0ok1ok2ok3late4late5ok6late
- t
- 5
- mistakes
- 2
- best t
- 5
- fewest
- 2
3Try
t = 5. Two mistakes: the 5 km and 10 km deliveries were on time but the rule says late. - km02132536t48510612guess0ok1ok2ok3late4late5late6lateactual0ok1ok2ok3late4late5ok6late
- t
- 6
- mistakes
- 1
- best t
- 6
- fewest
- 1
4Try
t = 6. Only the unusual 10 km delivery is wrong. One mistake, the best so far. - km0213253648t510612guess0ok1ok2ok3ok4late5late6lateactual0ok1ok2ok3late4late5ok6late
- t
- 8
- mistakes
- 2
- best t
- 6
- fewest
- 1
5Try
t = 8. The rule now misses the late 6 km delivery and still gets 10 km wrong. Two mistakes, so 6 stays the best. - km0213253648510t612guess0ok1ok2ok3ok4ok5late6lateactual0ok1ok2ok3late4late5ok6late
- t
- 10
- mistakes
- 3
- best t
- 6
- fewest
- 1
6Try
t = 10. Three mistakes: both late deliveries at 6 and 8 km are missed, and 10 km is still called late. - km0213253648510612tguess0ok1ok2ok3ok4ok5ok6lateactual0ok1ok2ok3late4late5ok6late
- t
- 12
- mistakes
- 2
- best t
- 6
- fewest
- 1
7Try
t = 12. The 10 km delivery is finally right, but 6 and 8 km are missed. Two mistakes. - km0213253648510612guess0ok1ok2ok3late4late5late6lateactual0ok1ok2ok3late4late5ok6late
- learned t
- 6
- mistakes
- 1
- rules tried
- 7
8Done. The learned rule is "late if at least 6 km", with 1 mistake out of 7. Nobody typed the number 6; it came from the data.
A new order comes in for 7 km. The model says late, because 7 is at least 6. That is a prediction: an answer for a case that was not in the training data.
The model still gets the 10 km delivery wrong, and that is fine. No simple rule explains every quirk in real data. A model that matches every single example has often memorized noise, which is the trap covered in Overfitting and Evaluation.
The code
function learnThreshold(distances, late) {
let bestT = null;
let fewest = Infinity;
for (const t of distances) { // try each distance as the cut-off
let mistakes = 0;
for (let i = 0; i < distances.length; i++) {
const guess = distances[i] >= t; // the rule: late if at least t km
if (guess !== late[i]) mistakes++;
}
if (mistakes < fewest) {
fewest = mistakes;
bestT = t;
}
}
return { threshold: bestT, mistakes: fewest };
}
const distances = [2, 3, 5, 6, 8, 10, 12];
const late = [false, false, false, true, true, false, true];
learnThreshold(distances, late); // { threshold: 6, mistakes: 1 }def learn_threshold(distances, late):
best_t = None
fewest = float("inf")
for t in distances: # try each distance as the cut-off
mistakes = 0
for d, was_late in zip(distances, late):
guess = d >= t # the rule: late if at least t km
if guess != was_late:
mistakes += 1
if mistakes < fewest:
fewest = mistakes
best_t = t
return best_t, fewest
distances = [2, 3, 5, 6, 8, 10, 12]
late = [False, False, False, True, True, False, True]
learn_threshold(distances, late) # (6, 1)n candidate cut-offs, and each one is checked against all n examples.
Trying every option works here because there is one parameter and seven examples. Real models have thousands to billions of parameters, and you cannot try every combination. They need a smarter way to search, which is the subject of How Models Learn.
Three kinds of learning
- Supervised learning. Every example has a label, like our late or on-time column. Spam filters, price estimates and photo tagging work this way. Most of this module is about it.
- Unsupervised learning. There are no labels. The program looks for structure on its own, such as groups of customers who shop alike.
- Reinforcement learning. The program acts, gets a reward or penalty, and adjusts. Game-playing programs and robot control are typical uses.
When machine learning is the wrong tool
Machine learning gives you a rule that is usually right. That is a great trade when the rule is hard to write by hand and an occasional mistake is acceptable. It is a bad trade in other cases.
- The rule is already known. Sales tax, sorting a list, checking a password. Write the code.
- You have few examples, or examples without answers. The model can only learn what the data shows.
- Every answer must be exact and explainable. "Usually right" is not good enough for a bank balance.
Try it yourself
For each task, decide whether you would write the rule by hand or learn it from examples. Give a one-line reason for each.
Show a sample answer
- Write it by hand. The rule exists and is exact. Learning it would only add mistakes.
- Learn it. Fake reviews have many subtle signs that nobody can list completely, and you can collect examples labelled by moderators.
- Learn it. Past tickets with their real resolution times are good examples, and an estimate that is roughly right is still useful.
- Write it by hand. Whether 31 April exists is a fact, not a pattern.
The test is the same each time: is the rule hard to write down, do you have examples with answers, and can you live with occasional mistakes? You need yes to all three.
Check yourself
In the delivery model, what is the parameter?
The learned rule still gets the 10 km delivery wrong. What should you conclude?
Which task is the worst fit for machine learning?
What to practise next
Trying every value stops working once a model has more than a few parameters. How Models Learn shows the method that replaces it: measure the error with a loss, then walk downhill with gradient descent.