Lesson 3.6 · Stage 03: The maths behind machine learning
Maths chapter 4: Analytic geometry: distance, angle and shadow
Analytic geometry measures vectors: how long they are, how far apart, and the angle between them. ML uses these to decide which data items are "similar".
Analytic geometry measures vectors: how long they are, how far apart, and the angle between them. ML uses these to decide which data items are “similar”.
4.1 Distance between two points
Subtract the two vectors, then take the length of the result.
Worked example. Points (1, 2) and (4, 6). Difference = (3, 4). Distance = √(9 + 16) = 5.
Why ML needs it: the k-nearest-neighbours method predicts by looking at the closest data points.
4.2 Norm and unit vectors
“Norm” is the grown-up word for length. A unit vector has length 1. To make one, divide a vector by its own length: (3, 4) has length 5, so its unit vector is (3/5, 4/5) = (0.6, 0.8). It keeps the direction and throws away the size.
4.3 The angle between two vectors
The dot product hides the angle inside it:
Worked example. a = (1, 0) and b = (1, 1). Dot product = 1. Lengths are 1 and √2 ≈ 1.414. So cos θ = 1 ÷ 1.414 ≈ 0.707, and θ = 45° (class 10 trigonometry table).
| cos θ | Angle | Meaning |
|---|---|---|
| 1 | 0° | same direction |
| 0 | 90° | at right angles, unrelated |
| −1 | 180° | opposite directions |
4.4 Orthogonal vectors
Two vectors are orthogonal (perpendicular) when their dot product is 0. Example: (1, 2) · (2, −1) = 2 − 2 = 0, so they meet at 90°. In ML, orthogonal directions carry completely separate information.
4.5 Projection: the shadow of a vector
Shine a light straight down onto a line. The projection of a onto b is the shadow a casts on b’s line.
Worked example. a = (2, 3), b = (1, 1). a · b = 5 and b · b = 2. Shadow = 2.5 × (1, 1) = (2.5, 2.5).
Why ML needs it: PCA (Chapter 5) and linear regression both work by projecting data onto the best line.
4.6 Cosine similarity
Cosine similarity is cos θ used as a score for “how alike are these two things?”.
Worked example. Three people rate action, comedy and romance films out of 5:
| Person | Ratings (action, comedy, romance) |
|---|---|
| Asha | (5, 4, 0) |
| Bala | (4, 5, 1) |
| Chitra | (0, 1, 5) |
- Asha vs Bala: dot = 20 + 20 + 0 = 40. Lengths √41 ≈ 6.40 and √42 ≈ 6.48. Similarity ≈ 40 ÷ 41.5 ≈ 0.96, very alike.
- Asha vs Chitra: dot = 0 + 4 + 0 = 4. Lengths 6.40 and √26 ≈ 5.10. Similarity ≈ 4 ÷ 32.6 ≈ 0.12, very different.
A film app would recommend Bala’s favourites to Asha. Oracle AI Vector Search does this same calculation on text and image vectors.
Try it
- Find the distance between (2, 1) and (5, 5).
- Find the unit vector of (6, 8).
- Find the angle between (2, 0) and (3, 3).
- Are (3, −2) and (4, 6) orthogonal?
- Find the projection of (4, 2) onto (1, 0).