Skip to content

What machine learning is

Instead of writing a rule by hand, pick one from a family of rules by how well it fits examples, and judge it on new inputs.

This topic

First, the picture

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.

pass through the examples
–
corrections so far
0
0.00 / 24.85 s
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, x1x_1 and x2x_2. I write them together as x=(x1,x2)\mathbf{x}=(x_1,x_2). Its class, A or B, is its label, yy. The ten labelled examples are the training set, and I write mm for the number of examples, here m=10m=10.

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

y^={Aif w1x1+w2x2+b>0,Botherwise.\hat y=\begin{cases}\text{A} & \text{if } w_1x_1+w_2x_2+b>0,\\ \text{B} & \text{otherwise.}\end{cases}

The hat on y^\hat y marks a prediction, the rule’s guess at the label. The numbers w1w_1 and w2w_2 are the weights, w=(w1,w2)\mathbf{w}=(w_1,w_2), and bb is the bias term; together they are the rule’s parameters. The sum w1x1+w2x2+bw_1x_1+w_2x_2+b 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.

Two kinds of question. Left: a number to predict, a taxi fare from the distance (made-up fares); the line predicts 9 for 4 km. Right: a class to predict; the line sorts the examples into class A and class B.
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 =3+1.5×=3+1.5\times 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 y^=wx+b\hat y=wx+b, with a plain ww 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:

Different examples, a different rule. The same start and the same steps, given ten other examples of the same two kinds, end on another line. The new dot (3, −1) is class A by the first rule and class B by the second.
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, (3,−1)(3,-1), 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: (2,2)(2,2), (1,3)(1,3), (3,1)(3,1), (3,3)(3,3), (2,4)(2,4);
  • class B: (−1,−1)(-1,-1), (1,−1)(1,-1), (−1,1)(-1,1), (−2,0)(-2,0), (0,−2)(0,-2).

The run starts with w=(0,1)\mathbf{w}=(0,1) and b=0b=0: the rule “A if x2>0x_2>0”, a level line. A dot is wrong when its score has the wrong sign or is exactly 0. Then, for a class A dot, w\mathbf{w} gains x\mathbf{x} and bb gains 1; for a class B dot, w\mathbf{w} loses x\mathbf{x} and bb loses 1.

PassWrong dotClassIts scoreNew weightsNew bias term
1(−1,1)(-1,1)B11(1,0)(1,0)−1-1
2(1,3)(1,3)A00(2,3)(2,3)00
2(−1,1)(-1,1)B11(3,2)(3,2)−1-1
3(1,−1)(1,-1)B00(2,3)(2,3)−2-2

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

2x1+3x2−2>0 ⇒ A,2x_1+3x_2-2>0\ \Rightarrow\ \text{A},

found with four corrections. The dot (−1,1)(-1,1) was corrected twice: one nudge does not always fix the dot it was made for.

New dots. (3,4)(3,4) scores 6+12−2=166+12-2=16, class A; (−2,−2)(-2,-2) scores −4−6−2=−12-4-6-2=-12, class B; (0,2)(0,2) scores 0+6−2=40+6-2=4, class A. And (1,1)(1,1) scores 2+3−2=32+3-2=3, class A.

Different examples. Class A at (2,1)(2,1), (3,3)(3,3), (1,4)(1,4), (4,1)(4,1), (2,3)(2,3) and class B at (−2,1)(-2,1), (0,−1)(0,-1), (−1,−3)(-1,-3), (1,−2)(1,-2), (−3,−1)(-3,-1), taking turns as before, from the same start. In pass 1, (−2,1)(-2,1) scores 1 and is wrong, giving w=(2,0)\mathbf{w}=(2,0), b=−1b=-1; then (1,−2)(1,-2) scores 1 and is wrong, giving w=(1,2)\mathbf{w}=(1,2), b=−2b=-2. Pass 2 scores 2, −2, 7, −4, 7, −9, 4, −5, 6, −7, all right. The rule is x1+2x2−2>0x_1+2x_2-2>0, after two corrections. The new dot (3,−1)(3,-1) scores 6−3−2=16-3-2=1 under the first rule, class A, and 3−2−2=−13-2-2=-1 under the second, class B.

A number rule. With made-up fares, a rule fare =3+1.5×=3+1.5\times distance in km predicts 3+1.5×4=93+1.5\times4=9 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

WordMeaningHere
Exampleone item with its measurementsa dot
Featureone measurement, x1x_1, x2x_2a coordinate
Labelthe answer given with an example, yyclass A or B
Parametersthe numbers a rule fitsw1w_1, w2w_2, bb
Line ruleA if w1x1+w2x2+b>0w_1x_1+w_2x_2+b>0the score’s sign
Regressionpredict a numbery^=wx+b\hat y=wx+b
Classificationpredict a classA or B
Unsupervisedfind structure with no labelsgroups
Training setthe labelled examples a rule is fitted tom=10m=10 dots
New dataexamples the rule has not seenthe grey dots

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look