Understanding AI · lesson 15 of 60 · about 6 minutes
Measuring Distance
With more than one feature, how far apart are two examples? Learn how AI measures similarity.
What you'll learn
- Work out the distance between two examples with two features
- Explain why distance means similarity
- Spot when one feature unfairly outweighs another
Distance means difference
To find the closest example, AI needs a number for how different two examples are. With one feature it's just the gap. With two features, a simple way is to add up the gaps in each one.
A small total means the examples are alike. A big total means they're very different.
Two fruits with two features each: (weight, smoothness). Add the size of each gap.
a = (150, 9)
b = (140, 3)
distance = abs(a[0] - b[0]) + abs(a[1] - b[1])
print(distance)Output
16Watch the scale
Notice weight gave a gap of 10 and smoothness a gap of 6. If weight were in milligrams, its gap would be 10000 and smoothness would hardly count at all. AI often rescales features to similar ranges first so that no single feature drowns out the rest.
Tip
Small distance, similar examples. Big distance, very different ones.
Now try it yourself
This lesson is free in Codiqo, with 6 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.