Step 1 of 10 · 0 done
Machine Learning · Topic 1 · Introduction to Machine Learning · Beginner · Guided tutorial

Introduction to Machine Learning: learn it by doing it

By the end you will be able to plan a small machine-learning project yourself: pick the way to learn, choose the features, split the data, classify a new case by hand and judge the result fairly.

Before you startHow to read a table of data (rows are examples, columns are measurements), how to read a dot on a graph with two axes, and percentages. The theory behind every step is on the Blueprint page.

Reset my progress
10
steps
≈ 53 min
in all
✎
you need: pen and paper
7
outcomes
01

Tell a learned rule from a written rule

≈ 4 min
The running example: you are building a fruit-sorting machine for a farm that must send apples one way and oranges the other.

A normal program follows rules a person typed in. Machine learning turns this around: you give the computer many examples together with their answers, and it works out the rule itself. Arthur Samuel, who coined the name in 1959, described it as giving computers “the ability to learn without being explicitly programmed”.

rules (typed)+ data program answers examples+ answers learning a rule

It pays off when a task is easy for people but nobody can write the rule down step by step. Tom Mitchell (1997) put it this way: a program learns from experience E at a task T if its performance P improves with E.

Try it · choose one

The farm has four jobs to automate. Which one is a job for machine learning rather than for rules a person writes?

02

Find the features and the label

≈ 4 min
The machine never sees a fruit, only numbers about it. Choosing those numbers is your job.

Data for machine learning is a table. Each row is one example (here, one fruit). Each measured column is a feature: a number that describes the example, such as its weight or how red it is. The one column that holds the answer you want the machine to give is the label.

The machine learns how the features relate to the label. Later, for a new fruit, it gets only the features and must produce the label on its own.

Try it · click the right part of the diagram

Here are four fruits the farm has measured. Click the column the machine must learn to predict.

#weight (g)redness 0–10fruit 11528apple 21683orange 31417apple 41832orange

Keyboard: Tab to a part, Enter to select it.

03

Put the project steps in order

≈ 5 min
Every machine-learning project, from fruit sorting to spam filters, walks the same path.

First you ask a clear question with a definite answer (“apple or orange?”). Then you collect examples whose answers are known, and describe each one with features. Before learning anything, you set some examples aside and hide them: the test set. The machine learns only from the rest, the training set. At the end you check it on the hidden examples, which stand in for fruits it will meet in the future.

The program that comes out of training (the learned rule) is called the model.

Try it · put the 6 steps in order

Use ▲ and ▼ to put the fruit sorter’s steps in the order they happen, first at the top.

  1. Train the model on the training fruits
  2. Collect fruits whose answers are known
  3. Check the model on the hidden fruits, then use it
  4. Ask a clear question: apple or orange?
  5. Hide some fruits away as the test set
  6. Describe each fruit with features (weight, redness)
04

Pick the way to learn

≈ 5 min
What you have decides how the machine learns: answers, no answers, rewards, or a head start.

There are three basic ways to learn, plus one shortcut (“3 + 1 ways”):

supervised every example comes with its answer (a label), and the machine learns to copy them. Our fruit sorter is supervised.
unsupervised no answers at all; the machine finds groups of examples that look alike, for instance with k-means.
reinforcement an agent acts, gets a reward or penalty, and keeps what earns points.
transfer reuse a model already trained on a big, similar task (a pretrained model) and adjust it with a few new examples.

Try it · match the pairs

Match each situation to the way of learning that fits it.

05

Split the data 80 / 20

≈ 4 min
The usual habit: learn on about 80 % of the examples and keep 20 % hidden for the final test.

How many examples should you hide? A common starting point is an 80 / 20 split: 80 % for training, 20 % for testing. On the Blueprint’s walk, 20 fruits were split into 16 for training and 4 for testing.

Big datasets do the same: the handwritten-digit set MNIST has 70,000 pictures, of which 60,000 are for training and 10,000 are kept for the test.

Try it · type a number

The farm workers have now labelled 250 fruits. With an 80 / 20 split, how many fruits go into the test set?

fruits (exact whole number)
06

Your turn: classify a fruit by hand

≈ 8 min · pen and paper
The simplest learner of all, nearest neighbour: copy the answer of the most similar known fruit.

To label a new fruit, nearest neighbour finds the known fruit that is most like it and gives the same answer. “Most like it” means the smallest distance between their feature values on a graph. Each fruit is (size, redness), both 0–10.

distance = √( (size difference)² + (redness difference)² )

In words: subtract the sizes, subtract the rednesses, square both (multiply each by itself, so minus signs vanish), add them, then take the square root.

010size →10red 5 A (2, 8) apple B (6, 2) orange C (3, 6) apple N (5, 4) new

Worked mini-example, N to A: sizes 5 − 2 = 3, rednesses 4 − 8 = −4; squares 9 and 16; sum 25; √25 = 5.

Your turn · three parts

Do the same for the other two known fruits, then decide.

a · distance from N to B (6, 2)
units, 2 decimals (± 0.05)
b · distance from N to C (3, 6)
units, 2 decimals (± 0.05)
c · with one neighbour (k = 1), N is…
07

Judge the model on unseen data

≈ 5 min
The golden rule: a model’s score on the hidden test fruits counts, not its score on the fruits it learned from.

A model can be made more or less flexible: how closely it follows every training fruit. A very stiff model misses the real pattern and does badly everywhere: it underfits. A very flexible one remembers every training fruit, quirks and all, so its training error drops to 0 % but it stumbles on new fruits: it overfits.

error = wrong ÷ checked · accuracy = 1 − error

The best model is the one with the lowest error on the unseen fruits, usually somewhere in the middle.

Try it · set the slider to your prediction

The chart shows the sorter’s training error for ten settings of flexibility. Where will the error on the 50 hidden test fruits be lowest?

training error (seen fruits)test error (unseen) · hidden until you check

Accepted within ± 1 of the best setting. The numbers are illustrative but follow the typical pattern.

08

Rescale a feature before measuring distance

≈ 5 min
In step 6 both features ran from 0 to 10. Real weights are in grams, and that breaks the distance.

Take two fruits: P weighs 150 g with redness 7; Q weighs 190 g with redness 3. In grams the weight difference is 40, squared 1,600. The redness difference is 4, squared 16. In the distance, weight now counts 100 times more than colour, so colour hardly matters.

in grams weight² = 1,600 redness² = 16 rescaled weight² = ? redness² = 16

The fix is feature scaling: put features on similar ranges first. Here we measure weight in tens of grams, so 150 g becomes 15.

Try it · fill in the blanks

Rescale the weights of P (150 g) and Q (190 g) to tens of grams.

The weight difference becomes tens of grams, and its square is , now about the same size as redness’s 16.

Exact whole numbers.

09

Count the boxes as features grow

≈ 5 min
More features sound helpful, but each one makes the space far bigger: the curse of dimensionality.

Cut each feature into 10 steps (light to heavy, green to red). With 1 feature there are 10 boxes a fruit can land in; with 2 features, 10 × 10 = 100 boxes; with d features, 10 multiplied by itself d times (10d). Each new feature multiplies the boxes by 10, but your number of fruits stays the same, so they spread thinner and thinner and a new fruit rarely has a close neighbour.

Richard Bellman named this the curse of dimensionality in 1957. The cure: keep only features that matter.

Try it · type a number

The farm keeps its 1,000 fruits but now measures 4 features (weight, redness, diameter, sweetness), each cut into 10 steps. On average, how many fruits are there per box?

fruits per box (± 0.001)
10

Put it all together

≈ 8 min
The farm now wants a sorter for ripe and unripe mangoes. You plan it, using every step so far.

The whole topic in one project. A worker has tagged 500 mangoes as “ripe” or “unripe”. For each mango the farm recorded its weight (g), its softness (0–10), its skin colour (0–10, green to yellow) and the day of the week it was picked.

Walk the path from step 3: decide the way to learn (step 4), split the data (step 5), choose the features (steps 2 and 9), scale them (step 8), classify with neighbours (step 6) and judge the result fairly (step 7).

nearest known mangodistancelabel
1st1.2ripe
2nd1.5unripe
3rd1.9ripe
Capstone · six parts
a · way to learn
b · mangoes in the test set (80 / 20)
mangoes (exact)
c · features to keep (tick all that help)
d · before measuring distances, weight in grams should be…
e · with k = 3 (table on the left), the new mango is…
f · it scores 99 % on training mangoes, 70 % on test mangoes. This means…
✓

What you can do now

Each outcome ticks itself when the steps that teach it are done (solved, or answer viewed).
  • ○Explain how machine learning differs from writing rules, and name the features and the label in a table of data.
  • ○Put the steps of a machine-learning project in order and split the data 80 / 20.
  • ○Choose supervised, unsupervised, reinforcement or transfer learning for a task.
  • ○Classify a new example with nearest neighbour by hand, on rescaled features.
  • ○Tell overfitting and underfitting from a good fit using unseen test data.
  • ○Explain the curse of dimensionality with the number of boxes.
  • ○Plan a small machine-learning project from question to fair test.
Your score · solved without the worked answer
0 / 10

Work through the steps to fill this in.