What Is Machine Learning
What machine learning is, how a model's numbers are chosen from examples, the main kinds of learning, and how to decide when it is the right tool.
On this page
Machine learning is a way of building a program whose rule is found from examples instead of being written by hand. You give a learning algorithm many past cases together with the correct answer for each, and it chooses the numbers inside a model so that the model's answers match those cases as closely as possible. It is used for problems where nobody can write the rule down, such as recognising spam, estimating a price or predicting which customers will leave.
Without it, you would have to write and maintain that rule yourself. For a problem like predicting late deliveries, a hand-written rule collects exceptions (late if the distance is large, unless it is a weekday morning, unless the driver is new), and each fix makes the rule harder to read and still wrong for the next unusual case. A spreadsheet of past deliveries with a late or on-time column already contains the information the rule needs.
How a model is learned from examples
The rule that learning produces is called a model. A model is a function that takes the known facts about one case and returns a guess. It counts as learned because some of the numbers inside it were chosen by fitting the examples, not typed in by a programmer. The terms below are used in every lesson of this module, with the late-delivery example as the illustration.
- An example is one past case, such as one delivery from the spreadsheet.
- A feature is a fact about the case that you know before the answer, such as the distance in kilometres.
- A label is the correct answer for that example, here whether the delivery was late or on time.
- A parameter is a number inside the model that learning sets, such as the cut-off distance beyond which a delivery is called late.
- Training is the process of choosing parameters that fit the labelled examples.
- A prediction is the trained model's answer for a new case that was not in the training data.
A worked example of training
The simplest useful model says that a delivery is late if its distance is at least t kilometres. It has one parameter, t. Training tries each distance in the data as the cut-off, counts how many of the seven examples the rule gets wrong, and keeps the cut-off with the fewest mistakes. One of the examples, the 10 km delivery, arrived on time even though it was far, so no cut-off can get every example right.
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 called late, so the four on-time ones are wrong. - km0213t253648510612guess0ok1late2late3late4late5late6lateactual0ok1ok2ok3late4late5ok6late
- t
- 3
- mistakes
- 3
- best t
- 3
- fewest
- 3
2Try
t = 3. The 2 km delivery is now correct, which leaves three mistakes. - km021325t3648510612guess0ok1ok2late3late4late5late6lateactual0ok1ok2ok3late4late5ok6late
- t
- 5
- mistakes
- 2
- best t
- 5
- fewest
- 2
3Try
t = 5. This gives two mistakes, because the 5 km and 10 km deliveries were on time but are called late. - km02132536t48510612guess0ok1ok2ok3late4late5late6lateactual0ok1ok2ok3late4late5ok6late
- t
- 6
- mistakes
- 1
- best t
- 6
- fewest
- 1
4Try
t = 6. Only the 10 km delivery is wrong, so this is one mistake and the best result 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, which makes two mistakes. - km0213253648510t612guess0ok1ok2ok3ok4ok5late6lateactual0ok1ok2ok3late4late5ok6late
- t
- 10
- mistakes
- 3
- best t
- 6
- fewest
- 1
6Try
t = 10. The 6 km and 8 km deliveries are missed and 10 km is still called late, which makes three mistakes. - km0213253648510612tguess0ok1ok2ok3ok4ok5ok6lateactual0ok1ok2ok3late4late5ok6late
- t
- 12
- mistakes
- 2
- best t
- 6
- fewest
- 1
7Try
t = 12. The 10 km delivery is finally correct, but 6 km and 8 km are missed, which makes two mistakes. - km0213253648510612guess0ok1ok2ok3late4late5late6lateactual0ok1ok2ok3late4late5ok6late
- learned t
- 6
- mistakes
- 1
- rules tried
- 7
8All cut-offs have been tried. The learned rule is "late if at least 6 km", which makes 1 mistake out of 7, and the value 6 came from the data instead of being typed in by a programmer.
The learned rule is "late if at least 6 km". If a new 7 km order comes in, the model predicts that it will be late, because 7 is at least 6. The model still gets the 10 km delivery wrong. That is normal, because real data contains cases that a simple rule cannot explain, and a model that matches every example has often memorised noise. Overfitting and Evaluation covers that problem.
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)Trying every candidate works here because there is one parameter and seven examples. Real models have thousands to billions of parameters, so the number of combinations is far too large to try, and training uses a guided search instead. How Models Learn explains that search.
Kinds of learning
Supervised learning
In supervised learning every training example has a label, like the late column above. The model learns to predict the label from the features. It is the most common kind in practice: spam filters, price estimates and demand forecasts all work this way. It works well when you can collect enough labelled examples, and it fails when the labels are wrong or inconsistent, because the model faithfully learns those errors. Most of this module is about supervised learning.
Unsupervised learning
In unsupervised learning the examples have no labels, and the program looks for structure on its own, for example grouping customers whose purchases look alike. Use it when you want to explore data or find groups and you do not yet know what the right answer is. The difficulty is that nothing tells you whether the structure it found is useful, so a person has to inspect the result and decide.
Reinforcement learning
In reinforcement learning the program takes actions, receives a reward or a penalty, and adjusts its behaviour to collect more reward over time. It is used for game playing and robot control, where the right answer is not known for each step but the outcome can be scored. It needs a large number of trials, usually in a simulation, so it is rarely the first choice for business problems.
Classification and regression
Supervised problems come in two forms, and the form decides how you measure success. In classification the label is a category, such as late or on time, spam or not spam, and the model is judged by how often it picks the right category and by which kind of wrong pick it makes. In regression the label is a number, such as a price or a delivery time in minutes, and the model is judged by how far its estimates are from the true values. The delivery example above is classification. Predicting how many minutes late the delivery will be would be regression, using the same features.
Why a trained model can still be wrong
The features cannot explain the label
The delivery model only sees distance. If lateness is mostly caused by rain, no amount of data will teach the model that, because the information is not in its inputs. Before collecting more examples, check whether the features could plausibly determine the answer, and add the missing ones.
The training data does not match real use
If every example came from quiet summer weeks, the learned cut-off will be wrong in December, when traffic and weather are different. The model only knows the situations it was shown. Collect examples from the conditions in which the model will run, and recheck its accuracy after deployment, because those conditions change.
Treating a prediction as a fact
"The model says late" really means that deliveries like this one were usually late in the past. Show the result as an estimate, and keep a way for a person to correct it, especially when a wrong answer costs something.
What this changes in practice
Before building anything with machine learning, decide whether it is the right tool. It fits when three things are true: the rule is hard to write down, you have examples with known answers, and you can accept occasional wrong answers. If the rule is known and exact, such as sales tax or a password check, write the code, because a learned rule is only usually right. If you have few examples or none with answers, the model has nothing to learn from. If every answer must be exact and explainable, as with a bank balance, a model that is usually right is not enough.
The same test applies when you use a language model for a task. Ask whether the task is exact, and if it is, do it with ordinary code and use the model only for the part that is fuzzy.
When you start a real problem, build the simplest possible baseline first, such as always predicting the most common label or applying one hand-written rule, and measure it. A learned model is only worth its extra cost if it clearly beats that baseline. Also check where the labels come from, because their quality sets the upper limit on what the model can learn.
For each task, decide whether you would write the rule by hand or learn it from examples, and give a one-line reason.
Show a sample answer
- Write it by hand. The rule is exact, so learning it would only add mistakes.
- Learn it. Fake reviews have subtle signs that nobody can list, and moderators can label examples.
- Learn it. Past tickets with real resolution times make good examples, and a rough estimate is still useful.
- Write it by hand. Whether 31 April exists is a fact, not a pattern.
The test is whether the rule is hard to write down, whether you have examples with answers, and whether you can live with occasional mistakes. You need all three to be true before learning is worth using.
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?
Next lesson
How Models Learn shows the method that replaces trying every value, which is to measure the error with a loss and then follow it downhill with gradient descent.