Length and Distance
Norms.
How long is a vector? And how far apart are two data points? These questions power recommendation systems, clustering, and nearest neighbour models. The answer is the norm.

- How long is a vector?
- How far apart are two points?
- The norm
Pythagoras in any dimension
The length of a vector is the square root of the sum of its squared values. For the vector three, four, that's five, just like Pythagoras. The distance between two points is the length of their difference. Run it. Lin alg dot norm does it all.
import numpy as np
v = np.array([3, 4])
print("length:", np.sqrt((v ** 2).sum()), np.linalg.norm(v))
a = np.array([1, 2, 3])
b = np.array([4, 6, 3])
print("distance:", np.linalg.norm(a - b))length: 5.0 5.0 distance: 5.0
Two common norms
The usual length is called the L two norm, or Euclidean distance, the straight line. The L one norm adds up the absolute values instead, like walking along city blocks, so it's called Manhattan distance. Both appear in machine learning, especially in regularization, which we'll meet later.
L2: Euclidean
- √(Σ x²)straight linenp.linalg.norm(v)
L1: Manhattan
- Σ |x|city blocksnp.linalg.norm(v, 1)
Summary
A vector's length is its norm, and distance is the norm of the difference. L two is the straight line, and L one is city blocks. Next, cosine similarity.
Key takeaways- np.linalg.norm
- distance = ‖a − b‖
- L1 and L2