Machine Learning · Topic 1 · Introduction to Machine Learning · Guided tutorial · Intermediate

Introduction to Machine Learning: learn it by doing it

By the end you will take one real dataset of iris flowers from a vague question to a measured prediction: choose the learning setting, build feature vectors, compute distances, predict with k-nearest neighbours, and explain why more features can make things worse.

Before you startWhat a mean and a standard deviation are, how to read a scatter plot with two axes, and Pythagoras’ theorem. The theory behind every step is on the Blueprint page; come back here to practise it.

12
steps · one idea each
56 min
estimated time
✎
you need: pen and paper
1936
Fisher’s Iris data, carried through every step
01

Tell a learned rule from a written rule

⏱ 3 min · choose one
Explain

A normal program is a recipe a person writes: data + rules → answers. For many jobs nobody can write the full recipe. Machine learning turns it round: you give the computer examples together with the right answers, and it works out the rule itself: data + answers → rules. The learned rule is called the model. It then answers new cases, and it is judged only on cases it has never seen.

TRADITIONAL PROGRAMMING data rules program answers MACHINE LEARNING data answers learning rules = model
Try it

The botanist hands the computer 24 measured flowers, each with its species name, and gets back a rule that names new flowers. In the machine-learning picture, where is that rule?

02

Name the task, the measure and the experience

⏱ 4 min · match pairs
Explain

When does a program really learn? Tom Mitchell’s definition (1997): a program learns from experience E with respect to a task T and a performance measure P, if its performance at T, measured by P, improves with E.

In plain words: T is the job, P is a number that scores the job, and E is the examples the program learns from. A hand-written rule is as good on day one as it will ever be; it does not improve with E, so it is not learning.

Spam filter · T: sort emails into spam / not spam · P: share of emails sorted correctly · E: emails already labelled spam or not spam
Try it

Match T, P and E for the botanist’s iris program. One description is left over.

03

Find the features and the label

⏱ 4 min · click the diagram
Explain

A computer cannot look at a flower; it needs numbers. Each measured property is a feature, and the list of an example’s features is its feature vector x = (x₁, x₂, …, xd), where d is the number of features. The thing we want to predict is the label y.

Fisher’s Iris data (1936) has 150 flowers, 50 of each of three species. Every flower has 4 measurements in centimetres, so d = 4. A handwritten digit in the MNIST set is 28 × 28 pixels, so it has 784 features, one per pixel.

Try it

Here are three rows of Fisher’s Iris table. Click (or Tab to it and press Enter) the column that is the label y.

sepallength5.17.06.3 sepalwidth3.53.23.3 petallength1.44.76.0 petalwidth0.21.42.5 species■ setosa■ versicolor■ virginica all measurements in cm
04

Choose the kind of learning

⏱ 3 min · choose one
Explain

What the learner is given decides the kind of learning. With the right answers (labels) it is supervised: learn to predict the label. With the data alone it is unsupervised: find structure, such as groups (clustering). With only a reward after acting it is reinforcement learning: try actions and keep what scores well.

SUPERVISED labels given UNSUPERVISED groups found REINFORCEMENT agent world act reward score after acting
Try it

A park ranger measures the petals of 150 irises, but nobody on the team can name the species. The ranger wants to know whether the flowers fall into natural groups. Which setting is this?

05

Measure how close two flowers are

⏱ 6 min · type a number · pen and paper
Explain

Once a flower is a point on a graph, “similar” means “close”. The straight-line distance between two points is Pythagoras’ theorem, called the Euclidean distance: subtract, square, add, take the square root.

d(q, p) = √( (q₁ − p₁)² + (q₂ − p₂)² )
q (2, 3)p (5, 7) 5 − 2 = 37 − 3 = 4 √(9+16) = 5

Running example: we plot each flower by its petal length (x₁) and petal width (x₂), both in cm.

Try it

A new flower has petals q = (4.8 cm, 1.6 cm). A labelled versicolor from the table, A, has petals (4.4 cm, 1.3 cm). Work out d(q, A) on paper and type it.

cmaccepted within ± 0.01 cm
06

Your turn: predict a species with k-NN

⏱ 8 min · two parts · pen and paper
Explain

With distances in hand, the simplest learner of all is the k-nearest-neighbour rule (k-NN): to label a new flower, find the k labelled flowers closest to it and take the majority label among them. It is like guessing someone’s home town from where their closest friends live.

Here are four real versicolor and virginica rows from the Iris table (petal length, petal width, cm), with the distances to our new flower q = (4.8, 1.6) that are already known:

flowerpetals (cm)speciesd(q, ·) cm
A(4.4, 1.3)■ versicolor0.50
B(5.1, 1.8)▲ virginica?
C(5.6, 2.1)▲ virginica0.94
D(4.7, 1.4)■ versicolor0.22
Try it · your turn Part a

Compute the missing distance d(q, B).

cm± 0.01 cm
Part b

What does 3-NN (k = 3) predict for q?

07

See why units matter

⏱ 4 min · predict, then reveal
Explain

A distance adds the squared differences of all features, so a feature written with bigger numbers counts for more. In step 5, petal length gave 0.16 of the total 0.25, which is 64 %. Write the same length in millimetres and its difference becomes ten times bigger, so its square becomes a hundred times bigger, while the width (still in cm) stays the same.

The fix is to put features on a common scale, usually by standardising each one: z = (x − mean) ÷ standard deviation, with the mean and standard deviation taken from the training flowers only.

share of d² from length = (length diff)² ÷ d²
Try it

Keep q and A from step 5, but write petal length in mm (q: 48 mm, A: 44 mm) and leave petal width in cm. Set the slider to your prediction: what percentage of d² now comes from petal length?

50 %
0 %100 %
Your prediction: ________ %
08

Put the project stages in order

⏱ 5 min · put in order
Explain

Every machine-learning project follows the same pipeline. Start from the question: until you have decided the task T and the measure P you do not know what to collect. Examples must exist before they can be turned into numbers.

Then, before any fitting, hide a test set, like the examiner’s unseen roads in a driving test. Even the mean and standard deviation used for scaling must come from the training flowers only, or test information leaks in and the score is too optimistic. Train, and only at the very end measure P on the hidden flowers.

Running example: the botanist now has 30 labelled flowers.

Try it

Put the six stages in order, first at the top. Use the ↑ and ↓ buttons (they work with Tab and Enter).

  1. Measure accuracy on the 6 held-back flowers
  2. Collect flowers together with their species names
  3. Fit the scaling and train k-NN on the other 24 flowers
  4. Decide the task (name the species) and how success is measured
  5. Turn each flower into numbers (petal length, petal width)
  6. Hold back 6 of the 30 flowers as a test set
09

Catch an overfitting model

⏱ 4 min · fill in the blanks
Explain

A model is judged on data it has not seen. A model that is too simple misses the pattern (underfitting). A model that is too flexible bends to every random wiggle of its training data: training error near zero, test error high. That is overfitting.

1-NN is the extreme case. Ask it about a flower it was trained on and the nearest labelled flower is that flower itself, at distance 0, so it copies its own label. Its training score therefore tells you nothing; only the held-back flowers can.

Try it

Complete the botanist’s notes about 1-NN on the 24 training flowers (no two are identical).

On its own 24 training flowers, 1-NN scores accuracy, because . To judge it fairly the botanist uses . If training accuracy is high but test accuracy is much lower, the model is .
10

Count the data the curse demands

⏱ 4 min · type a number
Explain

Each feature adds an axis. To keep the same density, say 10 samples along every axis, you need 10 for one feature, 10 × 10 = 100 for two, and 10 × 10 × 10 = 1,000 for three: 10ᵈ for d features. Space grows exponentially, so data that looked dense becomes sparse and “nearest” neighbours stop being near. Richard Bellman named this the curse of dimensionality (1957).

d = 1 · 10 d = 2 · 100 d = 3 · 1,000

A second face of the curse: in 10 dimensions, a cube that holds 10 % of uniformly spread data must span 0.11/10 ≈ 79 % of each axis, so a “neighbourhood” is hardly local any more.

Try it

The botanist stops using only the petals and uses all 4 Iris features. How many flowers are needed to keep 10 samples along every axis? (Fisher’s whole dataset has 150.)

flowersexact; commas and 10^d are fine
11

Decide when to transfer what was learned

⏱ 3 min · choose one
Explain

Labels are the expensive part: measuring a flower takes seconds, naming its species takes a botanist. Transfer learning reuses a model trained on a big, related job for a new job with few labels. The early layers of an image network trained on the 1.2 million labelled ImageNet photos already detect edges, textures and shapes; you keep them (frozen) and train only a new final layer on your small dataset.

layers 1–3: edges, textures, shapes (from ImageNet)frozen
layers 4–5: object parts (from ImageNet)frozen
new final layer: your few classestrain
Try it

The botanist now wants photos, not measurements: a herbarium has only 40 labelled photos of a rare iris and a look-alike species. What is the best plan?

12

Put it all together

⏱ 8 min · five parts · pen and paper
Explain

No new idea here: one more flower arrives and you take it through the whole topic. You will use the learning setting (step 4), the measure P and the test set (steps 2, 8 and 9), the Euclidean distance (step 5), the k-NN vote (step 6) and the curse of dimensionality (step 10).

The flower: petals q = (5.0 cm, 1.6 cm). The labelled flowers are A, B, C and D from step 6:

flowerpetals (cm)species
A(4.4, 1.3)■ versicolor
B(5.1, 1.8)▲ virginica
C(5.6, 2.1)▲ virginica
D(4.7, 1.4)■ versicolor
Try it · capstone Part a · setting
Part b · performance P
Part c · distance
cm± 0.01 cm
Part d · prediction
Part e · the curse
flowers
✓

What you can do now

Each outcome is ticked when the steps that teach it are done.
  • ○Explain how machine learning differs from writing rules by hand, and name T, P and E for a problem.steps 1–2
  • ○Describe an example as a feature vector with a label, and explain why features must share a scale.steps 3, 7
  • ○Choose between supervised, unsupervised, reinforcement and transfer learning from the feedback available.steps 4, 11
  • ○Compute a Euclidean distance by hand and make a k-nearest-neighbour prediction.steps 5–6
  • ○Organise a project around a held-back test set and recognise overfitting.steps 8–9
  • ○Explain the curse of dimensionality and compute how much data more features demand.step 10
  • ○Take a new case through the whole topic, from setting to prediction.step 12
Your score · solved without the answer
0 / 12

Steps where you viewed the worked answer count as done, but not in this score.

Go to the Blueprint quiz