Tell a learned rule from a written rule
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.
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?
- Traditional programming: data + rules → answers. A person writes the rules.
- Machine learning: data + answers → rules.
- The 24 measured flowers are the data; their species names are the answers (labels).
- So the rule that names new flowers is the output of learning: it is the model. Answer: the second option.
Name the task, the measure and the experience
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.
Match T, P and E for the botanist’s iris program. One description is left over.
- T, the job: naming the species of a new iris flower.
- P, the score: the share of held-back flowers named correctly (accuracy).
- E, what it learns from: the measured flowers with their species names.
- Left over: the 2.5 cm rule. It is written by a person and never improves with more flowers, so it is neither E nor learning.
Find the features and the label
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.
Here are three rows of Fisher’s Iris table. Click (or Tab to it and press Enter) the column that is the label y.
- The four numeric columns (sepal length, sepal width, petal length, petal width) are measured with a ruler: they are the features, so d = 4.
- The species is what we want the program to predict for a new flower: it is the label y.
- First row as a feature vector: x = (5.1, 3.5, 1.4, 0.2), y = setosa.
Choose the kind of learning
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.
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?
- Labels? None: nobody can name the species, so supervised learning is impossible.
- Actions and rewards? None: the flowers are measured, not acted on.
- Data alone, and the goal is to find groups: that is unsupervised learning (clustering), for example k-means with k = 3.
- Afterwards an expert must look at each group to decide whether it is a species.
Measure how close two flowers are
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.
Running example: we plot each flower by its petal length (x₁) and petal width (x₂), both in cm.
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.
- Differences: 4.8 − 4.4 = 0.4 cm and 1.6 − 1.3 = 0.3 cm.
- Squares: 0.4² = 0.16 and 0.3² = 0.09.
- Sum: 0.16 + 0.09 = 0.25.
- Square root: √0.25 = 0.50 cm. (A 3-4-5 triangle scaled by 0.1.)
Your turn: predict a species with k-NN
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:
| flower | petals (cm) | species | d(q, ·) cm |
|---|---|---|---|
| A | (4.4, 1.3) | ■ versicolor | 0.50 |
| B | (5.1, 1.8) | ▲ virginica | ? |
| C | (5.6, 2.1) | ▲ virginica | 0.94 |
| D | (4.7, 1.4) | ■ versicolor | 0.22 |
Compute the missing distance d(q, B).
What does 3-NN (k = 3) predict for q?
- Part a: differences 0.3 and 0.2; squares 0.09 and 0.04; sum 0.13; √0.13 = 0.36 cm.
- Sort the distances: D 0.22 (versicolor), B 0.36 (virginica), A 0.50 (versicolor), C 0.94 (virginica).
- The 3 nearest are D, B and A: versicolor 2 votes, virginica 1 vote.
- Part b: 3-NN predicts versicolor. An odd k with two classes can never tie.
See why units matter
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.
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?
- Length difference: 48 − 44 = 4 mm, squared 16.
- Width difference: 0.3 cm, squared 0.09 (unchanged).
- d² = 16 + 0.09 = 16.09, so d ≈ 4.01.
- Share from length: 16 ÷ 16.09 ≈ 99.4 % (it was 0.16 ÷ 0.25 = 64 % in cm). The width now barely counts, so standardise the features before using distances.
Put the project stages in order
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.
Put the six stages in order, first at the top. Use the ↑ and ↓ buttons (they work with Tab and Enter).
- Measure accuracy on the 6 held-back flowers
- Collect flowers together with their species names
- Fit the scaling and train k-NN on the other 24 flowers
- Decide the task (name the species) and how success is measured
- Turn each flower into numbers (petal length, petal width)
- Hold back 6 of the 30 flowers as a test set
- Decide the task and how success is measured (T and P).
- Collect flowers with their species names (E).
- Turn each flower into numbers (features).
- Hold back 6 of the 30 flowers as a test set.
- Fit the scaling and train k-NN on the other 24.
- Measure accuracy on the 6 held-back flowers.
Splitting before scaling keeps every trace of the test flowers out of training.
Catch an overfitting model
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.
Complete the botanist’s notes about 1-NN on the 24 training flowers (no two are identical).
- Each training flower’s nearest neighbour is itself (distance 0), so 1-NN returns its own label: 100 % training accuracy.
- That score says nothing about new flowers; judge it on the 6 held-back test flowers.
- High training accuracy with much lower test accuracy is overfitting. A larger k (say 3 or 5) smooths the rule.
Count the data the curse demands
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).
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.
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.)
- Samples for k per axis in d dimensions: kᵈ.
- k = 10, d = 4: 10⁴ = 10 × 10 × 10 × 10 = 10,000 flowers.
- Fisher’s 150 flowers are 1.5 % of that, so in 4-D the Iris data is far sparser than in the 2-D petal plot (10² = 100).
Decide when to transfer what was learned
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.
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?
- A large network has millions of weights; 40 photos cannot pin them down, so training from scratch overfits (step 9).
- Clustering ignores the labels you do have and gives unnamed groups (step 4); reinforcement learning is for acting and rewards, not labelled photos.
- Reuse a network pretrained on ImageNet’s 1.2 million labelled photos, freeze its layers, and train only a new final layer on the 40 photos: transfer learning.
Put it all together
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:
| flower | petals (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 |
- Part a: every training flower has its species label, so it is supervised learning (classification).
- Part b: P is accuracy on held-back test flowers; training accuracy is misleading (1-NN always scores 100 %).
- Part c: distances from (5.0, 1.6): A √(0.6² + 0.3²) = √0.45 ≈ 0.67; B √(0.1² + 0.2²) = √0.05 ≈ 0.22; C √(0.6² + 0.5²) = √0.61 ≈ 0.78; D √(0.3² + 0.2²) = √0.13 ≈ 0.36. The nearest is B at 0.22 cm.
- Part d: the 3 nearest are B (virginica), D (versicolor), A (versicolor): versicolor, 2 votes to 1. Note that 1-NN would say virginica: the choice of k matters, and you would pick k by test accuracy.
- Part e: 10⁶ = 1,000,000 flowers. Adding 2 features to the 4 of step 10 multiplied the need by 100.
What you can do now
- ○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
Steps where you viewed the worked answer count as done, but not in this score.