Math for AILoading progress…

Norm and distance

Compute vector operations, lengths, angles and projections and explain what each measures.

Length comes from Pythagoras

To measure the straight-line distance between two points, move along the axes to make a right triangle: across by dx, up by dy. The straight line is the hypotenuse, so distance = √(dx² + dy²). The coordinates of the difference vector Q − P are exactly dx and dy.

-6-6-4-4-2-2224466PQ
offsetsquared
across (Qx − Px)39
up (Qy − Py)416
sum of squares25
‖Q − P‖ = √((3)² + (4)²)
= √25 = 5
‖P − Q‖ = 5 (swapping the points changes nothing)
Drag P and Q. The dashed legs are the offsets; the table squares and adds them; the green line is the distance.

Norm: the length of one vector

The Euclidean norm of a vector is its length: ‖a‖ = √(a₁² + a₂² + …). It is the distance from the origin to the tip. For [3, 4] that is √(9 + 16) = √25 = 5, the famous 3-4-5 triangle. The distance between two points is the norm of their difference: dist(P, Q) = ‖Q − P‖. For P = [1, 2] and Q = [4, 6] the difference is [3, 4], so the distance is 5.

A neat link to the last lesson: a·a = a₁² + a₂² + …, so ‖a‖ = √(a·a). The dot product of a vector with itself is its squared length.

Predict first. What is the distance between [1, 2] and [4, 6]?

Any number of coordinates

Nothing in the formula depends on there being two coordinates. For four, add two more squared offsets. You cannot draw four dimensions, but you can compute them, and that is what machine learning does all the time.

iuᵢvᵢ(uᵢ − vᵢ)²
1 1 21
2 0 -11
3 2 04
4 3 30
sum6
‖u − v‖ = √6 = 2.449
‖u‖ = √(u₁² + u₂² + u₃² + u₄²) = 3.742
Two vectors with four coordinates each. Move the sliders and watch the squared offsets.

Common mistakes

Distance is the norm of the difference, not the difference of the norms. Take a = [3, -3] and b = [-3, 3]: both have norm 4.243, so the difference of norms is 0, yet the points are 8.485 apart (they sit on opposite sides of the origin). Also easy to forget: the final square root. The sum of squares is the squared distance. It is a legitimate shortcut when you only need to rank which point is nearest, because squaring keeps the order, but it is not the distance.

Chapter context

A vector is an ordered set of coordinates in a specified space. It can describe a displacement, a data record or a learned representation. Operations are meaningful only when the coordinates refer to compatible dimensions.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

a⋅b=∑iaibi,∥a∥2=∑iai2.a\cdot b=\sum_i a_i b_i,\quad \|a\|_2=\sqrt{\sum_i a_i^2}. cos⁡(a,b)=a⋅b∥a∥2∥b∥2,proj⁡ba=a⋅bb⋅bb.\cos(a,b)=\frac{a\cdot b}{\|a\|_2\|b\|_2},\quad \operatorname{proj}_b a=\frac{a\cdot b}{b\cdot b}b.

Both vectors must have the same dimension; cosine and projection need the denominator to be nonzero.

Worked example

For a=[2,3] and b=[4,1], matching products are 8 and 3, so the dot product is 11. Squared norms are 13 and 17. The cosine is 11/sqrt(221), approximately 0.73994. The projection of [3,4] onto [1,0] keeps only the first coordinate.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import sqrt, isclose
from operator import mul
a, b = [2, 3], [4, 1]
dot = sum(map(mul, a, b))
assert dot == 11
cosine = dot / (sqrt(sum(x*x for x in a))*sqrt(sum(x*x for x in b)))
assert isclose(cosine, 11/sqrt(221))
assert isclose(cosine, 0.7399400734, abs_tol=1e-9)

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Dot product is not elementwise multiplication alone. Similarity and distance have opposite ordering conventions. Zero vectors cannot be normalized by dividing by their norm.