Could you write a rule that sorts these dots? Below, a line learns one from ten examples. Before it plays: will the line end in the same place if the examples are different?
A rule learned from examples
Ten labelled examples, one at a time; the line moves after each one it gets wrong.
A line splits the plane: its shaded side says class A, the other side class B. It starts where it was put, before any example is seen.
Describe this picture
One plane, both axes from −4 to 5 at equal scales. A straight line divides it, with the class A side shaded; it starts level, along the horizontal axis, before any example is seen. Ten labelled examples arrive one at a time, taking turns, class A first: round dots of class A, which end in the upper right, and squares of class B, in the lower left. When a dot lands on the wrong side of the line, or exactly on it, it is ringed and the line turns towards putting it on its own side; a dot already on its own side leaves the line where it is. After the first pass the same ten examples are checked again in turn, faster, and the line moves only for a wrong one. On a wide screen a ledger beside the plane shows each pass with its examples in arrival order, the wrong ones ringed. The fourth pass moves nothing: four corrections in all. Then three grey new dots land one after another, and each takes the class of the side it falls on: two class A, one class B. The captions say why the line moves while it moves. Readouts count the pass through the examples and the corrections so far. There is a play button and no other control.
A rule with three numbers
The line in the picture is a rule: a dot on one side is class A, on the other class B. It has no understanding of dots. It is three numbers, and the clip changed those numbers until the rule agreed with every example.
Each example here is a point with two coordinates, its two measurements or features, and . I write them together as . Its class, A or B, is its label, . The ten labelled examples are the training set, and I write for the number of examples, here .
In linear algebra · an example as a vector
A list of measurements is a vector, as in Vectors in the linear algebra course: an arrow from the origin to the point, or a list of numbers. With three features the point sits in space; with a hundred, the arithmetic is the same.
A straight line on the plane is every point where a weighted sum is zero. The rule is
The hat on marks a prediction, the rule’s guess at the label. The numbers and are the weights, , and is the bias term; together they are the rule’s parameters. The sum is the dot’s score: positive on the A side, negative on the B side, zero on the line.
Every choice of the three numbers gives a rule, so the line rules form a family. Learning, here, is picking one member of that family by how well it fits the examples. That is all the word means on these pages: a rule fitted to examples.
How the clip picks it is simple. When a dot is wrong, the line is nudged towards it: for a class A dot, its coordinates are added to the weights and 1 to the bias term; for a class B dot, they are subtracted. The worked example below follows it by hand, and a later lesson studies the rule and when it stops.
Two kinds of question
Predicting a class is one kind of question. Predicting a number is the other.
Describe this picture
Two panels side by side. On the left, “a number to predict”: seven made-up taxi fares against distance, from 0 to 9 km, rising in a near-straight band, with the line fare distance through them; a dashed guide from 4 km up to the line and across reads 9. On the right, “a class to predict”: the ten examples of the clip and its final line, with the class A side shaded. There is no control.
- Regression predicts a number: a taxi fare from the distance, tomorrow’s temperature from today’s readings. A line rule for one feature is , with a plain for its one weight.
- Classification predicts a class: spam or not, class A or class B.
Both learn from examples that come with their answers, the labels. That is supervised learning. Sometimes there are no answers at all, only the measurements, and the question is whether the examples fall into groups. That is unsupervised learning; k-means clustering finds such groups.
Training and new data
The rule now gets all ten examples right. That was never the goal. The goal is the next dot, one the rule has not seen, and the three grey dots stand for it.
Now the answer to the question at the top. Ten different examples of the same two kinds, the same starting line and the same nudges end on a different line:
Describe this picture
Two planes side by side, both from −4 to 5. On the left, “the first ten examples” and the clip’s line, after four corrections. On the right, “ten different examples” of the same two kinds and the line the same start and the same steps lead to, after two corrections; it falls less steeply to the right. The same grey new dot, , lies just on the class A side of the first line and just on the class B side of the second. The caption begins “Different examples, a different rule”. There is no control.
Both rules fit their own ten examples perfectly, and they disagree about a new point. So fitting every example does not make a rule right for new ones. How well a rule does on new inputs is what counts, and it is what Bias and variance of a model measures. Nearest neighbours shows a second readout beside the training one, mistakes on new dots, and every lesson that fits something keeps it.
Watch out
A rule that fits every training example is not proved right for new ones. Judge it on examples it has not seen.
Worked example
The ten examples, in the order they arrive (A first, then taking turns):
- class A: , , , , ;
- class B: , , , , .
The run starts with and : the rule “A if ”, a level line. A dot is wrong when its score has the wrong sign or is exactly 0. Then, for a class A dot, gains and gains 1; for a class B dot, loses and loses 1.
| Pass | Wrong dot | Class | Its score | New weights | New bias term |
|---|---|---|---|---|---|
| 1 | B | ||||
| 2 | A | ||||
| 2 | B | ||||
| 3 | B |
In pass 4 the scores are 8, −7, 9, −3, 7, −1, 13, −6, 14, −8 in arrival order: every class A dot positive, every class B dot negative, so nothing moves and the run stops. The rule is
found with four corrections. The dot was corrected twice: one nudge does not always fix the dot it was made for.
New dots. scores , class A; scores , class B; scores , class A. And scores , class A.
Different examples. Class A at , , , , and class B at , , , , , taking turns as before, from the same start. In pass 1, scores 1 and is wrong, giving , ; then scores 1 and is wrong, giving , . Pass 2 scores 2, −2, 7, −4, 7, −9, 4, −5, 6, −7, all right. The rule is , after two corrections. The new dot scores under the first rule, class A, and under the second, class B.
A number rule. With made-up fares, a rule fare distance in km predicts for a 4 km ride. Here the two parameters are the 3 and the 1.5, and fitting would choose them from past rides.
Where you’ll meet this
Spam filters. Each message is turned into numbers from its words, and a rule fitted to mail people have already marked sorts the new mail.
Reading postcodes. A scanned digit is a grid of brightness values; a rule fitted to thousands of labelled digits reads new ones.
Recommendations. What similar people chose before is the training set; the question is what you will choose next.
Machines. Vibration readings from a healthy machine are the examples, and a reading that drifts from them is flagged.
Each is a rule fitted to past examples and judged on new ones.
Reference card
| Word | Meaning | Here |
|---|---|---|
| Example | one item with its measurements | a dot |
| Feature | one measurement, , | a coordinate |
| Label | the answer given with an example, | class A or B |
| Parameters | the numbers a rule fits | , , |
| Line rule | A if | the score’s sign |
| Regression | predict a number | |
| Classification | predict a class | A or B |
| Unsupervised | find structure with no labels | groups |
| Training set | the labelled examples a rule is fitted to | dots |
| New data | examples the rule has not seen | the grey dots |