np.linalg.norm()
A norm measures the length or size of a vector or matrix as a single number.
In this page:
Syntax
length = np.linalg.norm(vector)
np.linalg.norm(vector, ord=p)
np.linalg.norm()
By default norm computes the Euclidean (L2) length of a vector, the square root of the sum of squares. The ord argument selects other norms such as 1 (sum of absolute values) or np.inf (largest absolute value).
Norms are used for distances and error measurements.
Note:
The distance between two points is norm(a - b).
Example: np.linalg.norm()
import numpy as np
v = np.array([3, 4])
print(np.linalg.norm(v))
print(np.linalg.norm(v, ord=1))
print(np.linalg.norm(np.array([1, 2]) - np.array([4, 6])))
# Output:
# 5.0
# 7.0
# 5.0
Related Topics
Common Mistakes
- Forgetting ord changes the definition
- Confusing vector and matrix norms
- Not subtracting before measuring a distance
Chapter Summary
- Default is the L2 norm
- ord selects the norm type
- Distance equals norm of the difference
- Works on vectors and matrices
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: