What is machine learning?
Programs whose rules are fitted to data rather than written by hand, the train and predict loop, the vocabulary you will use for the rest of the track, and when machine learning is the wrong tool
You will learn
- Explain the difference between a program whose rules are written by hand and one whose rules are fitted to data
- Define feature, label, model, parameter, loss and prediction and use them correctly
- Describe the train and predict loop and say which part runs once and which part runs forever
- Name the three classic kinds of learning and give one example of each
- Decide, for a given problem, whether machine learning is a sensible tool
This lesson is for anyone who can write a little code and wants to know what machine learning is before touching a library. There is no code on this page. The aim is a precise picture of what changes when a program’s rules are fitted to data rather than typed by a person, and the vocabulary that every later lesson in the beginner track uses without explanation.
Two ways to get a program
Suppose you need a program that decides whether an email is spam. The traditional way is to write the rules. If the subject line is in capitals, add a point. If the sender is in the address book, subtract three. If the body mentions a lottery, add two. Above some threshold, call it spam. Every rule is yours, and when the program is wrong you know which line to change.
That works when the rules are known, few and stable. Spam is none of those. It changes every month, the useful signals are spread across thousands of words, and the interactions between them are too many to enumerate. A rule set that took a year to write is out of date in a quarter.
Machine learning turns the problem around. You collect examples of the decision you want made, emails that a person has already marked as spam or not spam, and run a procedure that searches for rules which reproduce those decisions. You still choose the shape the rules may take (a weighted sum of word counts, say, or a tree of yes-or-no questions). The procedure chooses the numbers.
Take “fitted to data” literally. A machine learning model is a program with blanks in it. Training fills the blanks with numbers chosen to make the program’s answers on the examples as close as possible to the answers a person gave. The procedure is a search, and the next three lessons build that search from arithmetic up.
A worked example with three numbers
Take the smallest possible version. You want to predict the price of a flat from its floor area, and you have three sold flats to learn from.
| Area (m²) | Price (thousands) |
|---|---|
| 50 | 200 |
| 80 | 290 |
| 100 | 350 |
You decide the rule will be a straight line: price equals some slope times area plus some offset. That decision is the model shape. The slope and the offset are the two blanks, and the job of training is to fill them.
Try slope 3 and offset 50. The line predicts 200 for the first flat, 290 for the second and 350 for the third. Every prediction matches, the error on these examples is zero, and training could stop here. A fourth flat of 70 m² would be predicted at 3 times 70 plus 50, which is 260.
Now suppose the third flat had sold for 340. The same line predicts 350, an error of 10. Slope 2.8 and offset 60 would give 200, 284 and 340, with errors of 0, 6 and 0. Which line is better depends on how you add up the errors, and the way you add them up has a name: the loss. A common choice squares each error and takes the mean, so the first line scores 100 divided by 3 and the second scores 36 divided by 3. The second line wins. Training is the process of trying slopes and offsets, measuring the loss and moving towards lower loss. Lesson 03 shows the exact arithmetic for this case, and lesson 04 shows the general search that works when there is no formula.
The vocabulary
These words come up on every page of the Field Guide.
- A feature is one measured input. Floor area was a feature. A spam filter might have ten thousand, one per word.
- A label is the answer attached to an example. Price was the label. “Spam” or “not spam” is a label.
- An example (or sample, or row) is one set of features with its label. A dataset is a collection of them.
- A model is the program with blanks. A straight line is a model. A tree of questions is a model. A neural network is a model with millions of blanks.
- A parameter (or weight) is one of the blanks. The slope and the offset were the two parameters. The “7 B” in “7 B parameters” counts these.
- The loss is the single number that says how badly the current parameters fit the examples. Lower is better.
- Training (or fitting) is the search for parameters that make the loss small.
- Prediction (or inference) is running the model, parameters fixed, on an input it has not seen.
The split between training and prediction is the most useful thing to internalise. Training is expensive and happens once, or occasionally. Prediction is cheap and happens every time the program is used. Weeks on thousands of GPUs is the first half. A question typed into a chat interface and answered in a second is the second half.
The train and predict loop
Put together, the life of a model looks like the diagram below.
The loop at the top is the whole of training. Show the model examples, compare its answers with the labels, adjust the parameters a little in the direction that reduces the loss, repeat. For a straight line there is a formula that lands on the best parameters in one step. For almost everything else the loop is the only option, and it can run for millions of iterations.
The frozen copy at the bottom is what gets shipped. A trained model is a file of numbers plus the code that knows the model’s shape. Loading that file and running it is prediction. No learning happens during prediction. A deployed model does not improve from use unless somebody runs the top row again.
The dashed line is the part beginners forget and practitioners spend most of their time on. The world changes, the training examples go stale, and predictions get worse. Detecting that and retraining is covered under MLOps and deployment and in lesson 10.
Three kinds of learning
The flats and the spam filter are both supervised learning: every example comes with a label, and the model learns to map inputs to labels. Predicting a number (price) is regression. Predicting a category (spam or not) is classification. Most of this track is supervised, because most useful models in industry are.
Unsupervised learning has no labels. You have a pile of inputs and want structure: which customers resemble each other, which of these 10,000 dimensions matter, which records look nothing like the rest. Clustering, dimensionality reduction and anomaly detection live here. The output is a description of the data rather than a prediction.
Reinforcement learning has neither labels nor a fixed dataset. An agent takes actions in an environment, receives a reward signal and learns which actions lead to more reward. Games, robot control and some stages of training chat models use it. It has its own topic hub.
Large language models cut across these. Their main training is self-supervised: the label is a piece of the input itself, the next word in a document, so any text is a training example and no human labelling is needed. A smaller second stage uses supervised examples and reinforcement learning to shape the behaviour. The transformers and LLMs hub covers the mechanism, and lesson 09 covers using one well.
Generalisation is the whole game
Return to the three flats. A line through all three has zero loss. So does a wiggly curve that passes through all three points and does something absurd in between. Both fit the training examples perfectly, and only one will predict a fourth flat sensibly.
The property you care about is not how well a model fits the examples it was trained on but how well it predicts examples it has never seen. That is generalisation, and its failure mode, where a model memorises the training examples instead of the pattern behind them, is overfitting. A model too flexible for the amount of data will overfit, one too rigid will underfit, and choosing between them is most of the practical craft.
You cannot measure generalisation on the training data. You measure it by holding examples back, never showing them to training, and scoring the model on those. Lesson 02 makes this concrete and shows how easily the held-out score can be fooled. Lesson 06 goes deeper into choosing the right score.
When machine learning is the wrong tool
If the rules are known and few, write them. A date parser does not need a model. A tax calculation has a specification. A model would be slower to build, harder to test and wrong more often.
If there is no data, there is nothing to learn from. “We want a model to predict X” should be followed at once by “and we have N examples of X”. If N is zero, or the examples do not contain the signal, no amount of modelling helps. Lesson 02 is titled “Data is the model” for this reason.
If a wrong answer is unacceptable, a fitted model cannot help. It gives you a probability of being right on inputs that resemble its training data, and confident nonsense outside that range. Safety-critical systems wrap models in rules and fallbacks for this reason, and the safety, security and privacy hub discusses what that looks like.
If you must explain each decision, a complex model may be the wrong choice even when it is more accurate. Interpretability is a live research area, but a short decision tree a regulator can read is sometimes worth more than a neural network that scores two points higher.
The reverse test is the useful one. Machine learning is the right tool when the rules are unknown or too many to write, when there are plenty of labelled examples, when occasional errors are tolerable and when production inputs look like the training examples. Spam filtering, demand forecasting and image classification all pass that test. The remaining nine lessons in this track follow the diagram above from left to right and then downwards, ending with a model behind a service in lesson 10.
Exercise
- Pick a task at work or at home that involves a judgement call. Write down the features, the label, where labelled examples would come from and roughly how many you could collect in a month. Then decide whether machine learning is a sensible tool for it, in one sentence.
- Using the three flats with the third sold at 340, compute the mean squared loss for slope 2.5 and offset 80. Is it better or worse than slope 3 and offset 50.
- A deployed spam filter starts letting through a new kind of spam. Using the train and predict diagram, say which boxes are involved in fixing it and which are not.
Summary
- A machine learning model is a program with blanks. Training fills the blanks with numbers chosen to make the loss on labelled examples small.
- Features are the inputs, labels are the answers, parameters are the blanks, the loss measures the mismatch, and prediction runs the model with the parameters frozen.
- Training runs once and is expensive. Prediction runs on every request and is cheap. A deployed model does not learn from use unless it is retrained.
- Supervised learning has labels, unsupervised learning has none, reinforcement learning has rewards, and language models are mostly self-supervised.
- The score that matters is on examples the model has never seen. Fitting the training data is easy and proves nothing.
Further reading
- The Elements of Statistical Learning, Hastie, Tibshirani and Friedman: chapters 1 and 2 cover supervised learning, the bias and variance trade-off and the idea of a loss. The PDF is free from the authors.
- Probabilistic Machine Learning: An Introduction, Murphy: chapter 1 is a compact survey of the kinds of learning with modern examples. Free PDF from the author.
- Pattern Recognition and Machine Learning, Bishop: section 1.1 fits a polynomial to a handful of points and shows overfitting with pictures. Free PDF from the author’s page.