Could a camera sort apples and pears when some of them look almost the same?
At a fruit packing centre in Ireland, a camera measures each piece of fruit as it rolls past. If an apple and a pear can be almost the same width, how could the sorter tell which is which?
You can write a rule that sorts fruit by width. Measure the fruit, then call it an apple if it’s seventy millimetres wide or more. Now try a pear that’s seventy-two millimetres wide. The rule sorts it as an apple. Extra rules can help, but exceptions can still slip through.
Mark a known pear at sixty-five millimetres and a known apple at seventy. A new fruit at sixty-nine is four millimetres from the pear and one from the apple. Copy the apple’s label because it’s closer. That’s nearest neighbour classification: you use the closest labelled training example to make a prediction.
Keep the known pear at sixty-five millimetres and the known apple at seventy. This time the new fruit is sixty-six millimetres wide. Which label should the nearest neighbour method assign? Pick one answer.
The new fruit is sixty-eight millimetres wide and seventy-two high. To compare it with each example, add the width and height gaps, ignoring minus signs. For the apple, two plus two is four. For the pear, three plus eighteen is twenty-one. Four is smaller, so predict apple.
Use the same reference fruits. The apple is seventy by seventy, and the pear is sixty-five by ninety. The new fruit is sixty-seven by eighty-six, in millimetres. Add the width and height gaps for each reference fruit, then pick the prediction.
Plot a new batch by width and height without recording the fruit types. You can still see two groups of nearby points. Finding groups from unlabelled measurements is called clustering. These groups might reflect size or shape, so you can’t assume they’re apple and pear groups.
Suppose you want to estimate a fruit’s mass. Record the width, height and actual mass of each training fruit. Fit a model using those examples. Then give the fitted model just the width and height of a new fruit. It predicts a number, say one hundred and sixty grams. That’s regression.
If you test on training fruit, a fruit can match itself at a distance of zero. Testing only this way can give you a misleading score. Set labelled test fruit aside before training. Give the model only their measurements, then compare its predictions with the known labels.
For an exam question about sorting apples and pears, identify the output as the predicted fruit type. Describe the training data: each fruit’s width, height and known type. Identify nearest neighbour as a suitable method. To explain it, say you add the measurement gaps and copy the closest example’s label.