AI Guesses by Looking at Neighbours
About this lesson, for parents and teachers
How can a computer guess what something is when it has never seen it before? One simple answer: look at the most similar things it already knows, and let them vote.
Ages 9–11 · about 8 minutes · builds on How AI Learns From Examples · leads to A Perceptron Learns to Draw a Line, K-Means: Finding Groups Without Labels
Try this
- Tap the chart to drop the mystery animal ?. Lines show its nearest neighbours and how far away each one is.
- Drag it until the AI says rabbit.
- Move the k slider and find a spot where 1 neighbour and 7 neighbours disagree.
- Predict what 5 neighbours will vote for two mystery animals.
How it works
Every dot on the chart is an animal the computer already knows, with a label: cat, dog or rabbit. Each animal is described by two numbers, its weight and its ear length. Those numbers place it on the chart. Similar animals end up close together, so the cats, dogs and rabbits form groups.
When a new animal arrives, the computer measures the distance from it to every known animal, using a straight line on the chart (the Pythagorean theorem does the maths). Then it picks the k closest ones. That is why this method is called k-nearest neighbours, or k-NN. The computer does this for every single known animal, then sorts them from nearest to farthest.
The neighbours vote, and the label with the most votes wins. With k = 1, only the single closest animal decides, so one unusual example (like a big fluffy cat sitting among the dogs) can fool it. With a bigger k, more neighbours vote and odd ones get outvoted. If k is too big, though, faraway animals get a say too.
k-NN does not really learn rules. It just remembers every example and compares. That makes it easy to understand, but slow when there are millions of examples. It also shows a key idea of machine learning: the computer is only as good as its examples. Wrong or missing examples lead to wrong guesses.
Where engineers use this
Recommendation systems suggest songs or films that are "near" ones you liked, and some apps find similar photos or products by comparing their numbers in the same way. Data scientists often try k-NN first because it is so simple.
Words to know
- Label
- The right answer attached to an example, like cat or dog.
- Feature
- A number that describes something, like weight or ear length.
- Distance
- How far apart two points are on the chart. Close means similar.
- k-nearest neighbours
- Guessing a label by letting the k most similar known examples vote.
Talk about it
Goal: Drop a new point on a chart and let its nearest neighbours vote on what it is.
- If you met an animal you had never seen before, how would you guess what kind it is?
- Why might asking 7 neighbours be safer than asking just 1?
- What could go wrong if the computer only had examples of cats and dogs and then saw a rabbit?
Without a screen: Measure a few household objects (length and weight, or length and width) and plot them on squared paper with a label, like spoon or fork. Then measure a mystery object, plot it, and find its 3 nearest dots with a ruler. Do they vote correctly?
Curriculum links: CSTA K-12 1B-DA-07 (use data to highlight or propose cause-and-effect relationships, predict outcomes) and AI4K12 Big Idea 3 (computers can learn from data); Common Core 5.G.A.2 (graphing points on a coordinate plane)
Questions
What is k nearest neighbors for kids?
It is a way for a computer to guess what something is by finding the most similar examples it already knows and letting them vote. The k is how many neighbours get a vote.
How does AI classify things?
It turns each thing into numbers called features, then uses examples with known answers to decide. k-NN does this by comparing distances; other methods, like neural networks, learn patterns from the examples.
What is a good value of k?
There is no single best k. Small k can be fooled by one odd example, while very large k lets faraway examples vote. People usually test a few odd values like 3, 5 or 7 and keep the one that guesses best.