Vector Norm (np.linalg.norm)
Measure the length of a vector and the distance between two vectors with np.linalg.norm — L2, L1 and max norms, row-wise norms and normalisation.
How "big" is a player's stat vector? How "far apart" are Virat and Rohit statistically? Both questions are answered by a norm — the length of a vector. Norms appear everywhere in machine learning: distances in k-nearest-neighbours, normalising features, regularisation (L1/L2) and cosine similarity.
Definition
A norm is a function that assigns a non-negative length to a vector. The default Euclidean (L2) norm of
vis‖v‖ = √(v[0]² + v[1]² + … + v[n-1]²)— the straight-line distance from the origin to the pointv, exactly like Pythagoras' theorem.
- L2 norm (
ord=2, default for vectors) — square, sum, square root. "As the crow flies". - L1 norm (
ord=1) — sum of absolute values. "Manhattan / taxi-cab distance" along grid streets. - Max (∞) norm (
ord=np.inf) — the largest absolute value. - Distance between two vectors = norm of their difference:
np.linalg.norm(a - b). - Unit vector — a vector with norm 1, made by dividing a vector by its norm (normalising).
Syntax
python
np.linalg.norm(x, ord=None, axis=None, keepdims=False)| Parameter | Meaning | Default |
|---|---|---|
x | Vector or matrix | — required |
ord | Which norm: None/2 (L2), 1 (L1), np.inf (max) … | None → L2 for vectors, Frobenius for matrices |
axis | Compute one norm per row (axis=1) or per column (axis=0) | None → one number for the whole array |
keepdims | Keep the reduced axis with size 1 (handy for broadcasting) | False |
Example — L2, L1 and max norm of (3, 4)
python
import numpy as np
v = np.array([3, 4])
print(np.linalg.norm(v))
print(np.sqrt(np.sum(v ** 2)))
print(np.linalg.norm(v, ord=1))
print(np.linalg.norm(v, ord=np.inf))text
5.0
5.0
7.0
4.0How it works:
text
y
4 ┤ • (3, 4)
│ ╱ │
│ L2=5╱ │ 4 L2 = √(3² + 4²) = √25 = 5
│ ╱ │ L1 = |3| + |4| = 7
│ ╱ │ max = max(|3|, |4|) = 4
0 ┼────────┴──── x
0 3The second line proves the definition: norm(v) is just sqrt(sum(v**2)), which also equals np.sqrt(v @ v).
Example — distance between two players and normalising
python
virat = np.array([45.0, 140.0]) # [batting average, strike rate]
rohit = np.array([30.0, 145.0])
print(np.linalg.norm(virat - rohit).round(3))
unit = virat / np.linalg.norm(virat)
print(unit.round(4), np.linalg.norm(unit))text
15.811
[0.306 0.952] 1.0The Euclidean distance is √(15² + 5²) = √250 ≈ 15.811. Dividing by the norm gives a unit vector (length exactly 1) pointing in the same direction — used before cosine similarity and in many ML pipelines.
Tip: when features have very different scales (average ≈ 40, strike rate ≈ 140), the larger one dominates the distance. Standardise features first in real projects.
Example — matrix norm and row-wise norms
python
M = np.array([[1, 2], [3, 4]])
print(np.linalg.norm(M).round(4))
print(np.linalg.norm(M, axis=1).round(4))
print(np.linalg.norm(M, axis=0).round(4))text
5.4772
[2.2361 5. ]
[3.1623 4.4721]- No
axis→ the Frobenius norm:√(1 + 4 + 9 + 16) = √30 ≈ 5.4772— all entries treated as one long vector. axis=1→ one L2 norm per row (each player's vector length):√5,√25.axis=0→ one per column.
Comparison — the common norms
| Norm | ord | Formula | (3, 4) | Used for |
|---|---|---|---|---|
| L2 / Euclidean | None or 2 | √Σ v² | 5.0 | distances, unit vectors, ridge regularisation |
| L1 / Manhattan | 1 | Σ |v| | 7.0 | robust distances, lasso regularisation |
| Max / infinity | np.inf | max |v| | 4.0 | worst-case error |
| Frobenius (matrix) | None on 2D | √Σ all entries² | — | size of a whole matrix |
Common mistakes
- ❌ Writing
np.sqrt(v ** 2)and forgetting the sum (gives element-wise absolute values). ✅np.sqrt(np.sum(v ** 2))or simplynp.linalg.norm(v). - ❌
np.linalg.norm(a) - np.linalg.norm(b)as a "distance". ✅ Distance isnp.linalg.norm(a - b). - ❌ Forgetting
axis=1when you want one length per row — you get a single number for the whole matrix. ✅ Passaxis. - ❌ Normalising a zero vector (division by 0 →
nan). ✅ Check the norm is non-zero first.
Interview tip
Q: What is the difference between the L1 and L2 norm? A: L2 is the square root of the sum of squares (straight-line length) and penalises large values heavily; L1 is the sum of absolute values (grid distance) and is less sensitive to outliers. In regularisation, L1 (lasso) pushes weights to exactly zero, while L2 (ridge) shrinks them smoothly.
Key points to remember
- A norm measures the length (size) of a vector; the default L2 norm is √(sum of squares).
- np.linalg.norm(v) equals np.sqrt(np.sum(v ** 2)) and np.sqrt(v @ v).
- ord=1 gives the L1 (sum of absolute values) norm; ord=np.inf gives the max absolute value.
- The distance between two vectors is np.linalg.norm(a - b).
- v / np.linalg.norm(v) is a unit vector (length 1) in the same direction.
- axis=1 gives one norm per row; with no axis a matrix gets the Frobenius norm.
Try it yourself
Norms of the (3, 4) vector
Nearest player by distance
Practice: normalise player vectors
Revision Flashcards
Tap a card to flip it and see the answer.
