Tell a learned rule from a written rule
≈ 4 minA 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”.
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.
The farm has four jobs to automate. Which one is a job for machine learning rather than for rules a person writes?
- Price, unit conversion and alphabetical order each have an exact rule a person can type in: multiply, multiply by a fixed factor, compare letters.
- “Apple or orange?” is easy for a person but almost impossible to write as a rule: apples can be green, oranges can be reddish, sizes overlap.
- So the sorting job is the one for machine learning: show the machine many fruits with known answers and let it find the rule. Answer: tell apples from oranges.
Find the features and the label
≈ 4 minData 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.
Here are four fruits the farm has measured. Click the column the machine must learn to predict.
Keyboard: Tab to a part, Enter to select it.
- Weight (g) and redness (0–10) can be measured on any fruit, new or old: they are the features.
- A row (for example fruit 2: 168 g, redness 3, orange) is one example, not a column.
- For a new fruit we do not know the “fruit” column; that is what the machine must say. Answer: the “fruit” column is the label.
Put the project steps in order
≈ 5 minFirst 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.
Use ▲ and ▼ to put the fruit sorter’s steps in the order they happen, first at the top.
- Train the model on the training fruits
- Collect fruits whose answers are known
- Check the model on the hidden fruits, then use it
- Ask a clear question: apple or orange?
- Hide some fruits away as the test set
- Describe each fruit with features (weight, redness)
- Ask a clear question: apple or orange? Without it you do not know what to collect.
- Collect fruits whose answers are known.
- Describe each fruit with features (weight, redness).
- Hide some fruits as the test set, before any learning.
- Train the model on the training fruits only.
- Check it on the hidden fruits, then use it on real new fruits.
Pick the way to learn
≈ 5 minThere 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.
Match each situation to the way of learning that fits it.
- a · Every photo has an answer (tag) to copy → supervised.
- b · No answers, only “who is alike” → unsupervised (for example k-means).
- c · The arm learns from points earned by its own actions → reinforcement.
- d · Few tags, but a model that already knows edges, colours and shapes → transfer: reuse it and adjust it with the 200 photos.
Split the data 80 / 20
≈ 4 minHow 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.
The farm workers have now labelled 250 fruits. With an 80 / 20 split, how many fruits go into the test set?
- Test share = 20 % = 20 ÷ 100 = 0.2.
- Test fruits = 250 × 0.2 = 50.
- Training fruits = 250 − 50 = 200 (that is the 80 %).
Your turn: classify a fruit by hand
≈ 8 min · pen and paperTo 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.
Worked mini-example, N to A: sizes 5 − 2 = 3, rednesses 4 − 8 = −4; squares 9 and 16; sum 25; √25 = 5.
Do the same for the other two known fruits, then decide.
- N to B: differences 5 − 6 = −1 and 4 − 2 = 2; squares 1 and 4; sum 5; √5 ≈ 2.24.
- N to C: differences 5 − 3 = 2 and 4 − 6 = −2; squares 4 and 4; sum 8; √8 ≈ 2.83.
- N to A was 5 (worked example). Smallest: B at 2.24.
- With k = 1, N copies B’s label: orange.
- Note: asking the 3 nearest (k = 3) gives a vote of 2 apples to 1 orange, so apple. How many neighbours you ask is a choice you make, and it can change the answer.
Judge the model on unseen data
≈ 5 minA 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.
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?
Accepted within ± 1 of the best setting. The numbers are illustrative but follow the typical pattern.
- Test errors for flexibility 1 → 10: 32, 25, 18, 14, 16, 20, 24, 28, 33, 38 %.
- The lowest test error is 14 % at flexibility 4 (accuracy 1 − 0.14 = 86 %, about 43 of the 50 test fruits right).
- Left of 4 the model underfits (both errors high); right of 4 it overfits: training error keeps falling to 0 % while test error climbs to 38 %.
Rescale a feature before measuring distance
≈ 5 minTake 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.
The fix is feature scaling: put features on similar ranges first. Here we measure weight in tens of grams, so 150 g becomes 15.
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.
- 150 g ÷ 10 = 15 and 190 g ÷ 10 = 19 (tens of grams).
- Difference: 19 − 15 = 4.
- Square: 4 × 4 = 16, equal to redness’s 4 × 4 = 16, so both features now count about equally.
- Distance P–Q = √(16 + 16) = √32 ≈ 5.66, instead of √(1,600 + 16) ≈ 40.2 in raw grams.
Count the boxes as features grow
≈ 5 minCut 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.
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?
- Boxes = 10 × 10 × 10 × 10 = 10,000.
- Fruits per box = 1,000 ÷ 10,000 = 0.1.
- So at least 9 boxes in 10 are empty: most new fruits land in a box with no known fruit nearby. Compare 100 per box with 1 feature and 10 per box with 2.
Put it all together
≈ 8 minThe 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 mango | distance | label |
|---|---|---|
| 1st | 1.2 | ripe |
| 2nd | 1.5 | unripe |
| 3rd | 1.9 | ripe |
- a · The mangoes are tagged ripe/unripe, so there are answers to copy: supervised.
- b · 500 × 0.2 = 100 test mangoes (400 for training).
- c · Keep weight, softness and skin colour: they relate to ripeness. The day of the week tells nothing about ripeness; it would only add a fourth dimension and spread the 400 training mangoes thinner.
- d · Rescale weight (for example to tens of grams), or a difference of 40 g would count 100 times more than a softness difference of 4.
- e · Votes of the 3 nearest: ripe, unripe, ripe → 2 to 1: ripe.
- f · A 29-point gap between seen and unseen mangoes is the sign of overfitting: make the model less flexible and judge it on the test score.
What you can do now
- ○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.
Work through the steps to fill this in.