Unit 2 · Length, angle and the dot product
Lesson 09 · Available to read
Distance between vectors
The distance between two vectors is the norm of their difference; similar data points lie close together.
Lesson 6 showed that a − b is the displacement from point b to point a. Its length is therefore the distance between them. The Euclidean distance is defined as d(a, b) = ‖a − b‖.
For a = [5, 1] and b = [1, 4], the difference is a − b = [4, −3], so d(a, b) = √(16 + 9) = 5. The formula is symmetric: b − a = [−4, 3] has the same norm, so d(a, b) = d(b, a). The distance is zero only when the two vectors are equal.
The definition extends unchanged to higher dimensions. In ℝ³, the distance between [1, 0, 2] and [3, 1, 4] is ‖[2, 1, 2]‖ = √9 = 3.
Distance turns intuitive similarity into a number. Representing houses as feature vectors (lesson 4), we may say that two houses are similar when their vectors are close. Methods such as nearest-neighbour classification predict properties of a new item from the items closest to it. One caution: features on large scales dominate the distance. A difference of $10,000 in price outweighs any difference in the number of rooms, so features are usually rescaled to comparable ranges first.
The takeaway
d(a, b) = ‖a − b‖: the distance between two vectors is the length of their difference.
Practice
0 of 5 attempted
- 1Calculate
Find the distance between [2, 1] and [5, 5].
Saved in this browser
- 2Calculate
Find the distance between [1, 0, 2] and [3, 1, 4] in ℝ³.
Saved in this browser
- 3Multiple choice
How does d(a, b) compare with d(b, a)?
- 4Explain in your own words
Houses are described as [area in m², rooms, price in $]. Why might Euclidean distance be misleading here, and what is a remedy?
Saved in this browser
- 5Apply
A new song has features [0.8, 0.2]. Song A is [0.7, 0.3] and song B is [0.1, 0.9]. Which is its nearest neighbour?
Saved in this browser