← Back to NumPy Course | Chapter 7: Linear Algebra | Lesson 4 of 6

np.linalg.norm()

A norm measures the length or size of a vector or matrix as a single number.

In this page:

  1. np.linalg.norm()
Syntax
python
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()

python
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
  1. Forgetting ord changes the definition
  2. Confusing vector and matrix norms
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.