Course contents · Lesson 2 of 15

How Does Machine Learning Work? A Simple Explanation

How AI Learns · Lesson 2 of 15 · 8 min Foundations

Your phone's photo app can find every picture of a beach you have taken, yet nobody wrote a rule that describes what a beach looks like. So how does machine learning work, if no person writes the rules? The short answer: the computer is shown many examples together with the right answers, makes a guess for each one, and adjusts itself a little every time it is wrong. This lesson walks through that loop step by step, with no maths.

The three ingredients

In the previous lesson you saw that machine learning finds its own rules from examples. To do that it needs three things. The first is data: the examples themselves, such as photos of fruit, emails or past house sales. Each example is described by features, the details the learner can look at, such as colour, shape and size.

The second ingredient is labels: the right answer attached to each example, like "apple" or "banana". Labels are usually added by people, which is why collecting good labelled data is often the slowest and most expensive part of a project.

The third ingredient is the learner, called a model. A model is a program with a large set of adjustable numbers, often called parameters or weights. Those numbers decide how features turn into an answer. When you train a model from scratch they start out random, so its first guesses are nonsense. Learning means changing those numbers until the guesses match the labels.

A table of five fruits with columns colour, shape, size and label, feeding a model through an arrow marked train. A new green, round, 8 cm fruit without a label goes into the model and comes out as apple
The model is trained on rows that have labels, then used on a new row that has none.

Notice that the new green fruit in the diagram has features but no label. That is the whole point of training: once the model has learned from labelled rows, you can use it on fresh examples where nobody knows the answer yet.

The learning loop

Training a model is one simple loop, repeated a huge number of times. Show the model an example. Let it guess. Compare the guess with the label. Measure how wrong it was, as a single number called the error (or loss). Then adjust the model's numbers a little, in the direction that would have made this guess less wrong.

A five-step loop: show an example, the model guesses banana with 60 percent confidence, compare with the label which says apple, measure the error, adjust a little, then an arrow labelled next example returns to the start, with repeat thousands or millions of times in the centre
No single pass teaches the model much; the skill comes from running the small loop over and over.

Why only a little? A big change that fixes one example usually breaks others. Small nudges, added up over thousands of examples, push the numbers towards values that work for most of them at once. The method that works out which way to nudge each number is called gradient descent, and you do not need its maths to follow the idea.

In practice the model often looks at a small batch of examples, say 32, and adjusts once per batch. One full pass through all the training data is called an epoch, and training usually runs for many epochs.

Training data and test data

A model that scores well on the examples it trained on has not proved anything yet. It might simply have memorised them. So before training starts, you set aside part of the labelled data, often 10 to 20 percent, and never show it to the model during training.

That hidden part is the test data. After training, you ask the model about those examples and compare its answers with their labels. The test score tells you how the model is likely to do on new examples in real use. It is like a teacher keeping some exam questions secret, so that students cannot pass by memorising the practice paper.

What can go wrong

A model only knows what its examples show it. If every apple in the training data is red, the model may decide that "red" is what makes an apple, and fail on a green one. Too few examples, or examples that leave out whole groups, give a biased model. That matters a lot when the examples are people, for instance faces or loan applications.

The other classic failure is overfitting: the model memorises the training examples, including their noise and quirks, instead of learning the general pattern. An overfit model looks perfect on training data and does badly on test data. That gap is exactly what the test set is there to catch.

Two scatter charts of weight against size with the same dots. On the left a wiggly curve passes through every dot but misses a new point badly. On the right a straight line misses each dot slightly but lands close to the new point
Passing through every training dot is a warning sign, not a success: the simpler line predicts the new fruit far better.

Compare the two charts. The wiggly curve on the left has zero error on the old dots, yet its guess for the new fruit is far off. The straight line on the right is slightly wrong on every old dot but close on the new one, because it captured the real trend: bigger fruit weighs more.

Three ways a machine can learn

Everything so far is supervised learning: every example comes with a label, like the child being told "apple". Unsupervised learning has no labels at all; the model sorts the fruit into piles of similar-looking items without knowing their names. Reinforcement learning learns by trial and reward, like a fruit-picking robot that gets points for each fruit it picks without bruising, and slowly works out which movements earn the most points.

Check yourself

Why does a model adjust its numbers only a little after each mistake?

A big change that fixes one example tends to break others, while many small nudges settle on numbers that work for most examples together.

A model scores 99% on its training data and 60% on its test data. What is the likely problem?

Overfitting: it has memorised the training examples instead of learning a pattern that carries over to new ones.

Sorting customers into groups of similar shoppers, without any labels, is which kind of learning?

Unsupervised learning, because the model finds the groups by itself with no right answers given.