Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Return the Norm of the vector over given axis in Linear Algebra in Python
The norm of a vector or matrix measures its magnitude or size. In NumPy, you can calculate various types of norms using numpy.linalg.norm(), including vector norms along specific axes.
Syntax
numpy.linalg.norm(x, ord=None, axis=None, keepdims=False)
Parameters
x: Input array (vector or matrix)
ord: Order of the norm (default is 2-norm)
axis: Axis along which to compute the norm
keepdims: Whether to keep dimensions in the result
Basic Vector Norm Example
Let's start with calculating the norm of a simple vector ?
import numpy as np
from numpy import linalg as LA
# Create a simple vector
vector = np.array([3, 4, 5])
print("Vector:", vector)
# Calculate 2-norm (default)
norm_2 = LA.norm(vector)
print("2-norm:", norm_2)
# Calculate 1-norm (Manhattan distance)
norm_1 = LA.norm(vector, ord=1)
print("1-norm:", norm_1)
# Calculate infinity norm (maximum absolute value)
norm_inf = LA.norm(vector, ord=np.inf)
print("Infinity norm:", norm_inf)
Vector: [3 4 5] 2-norm: 7.0710678118654755 1-norm: 12.0 Infinity norm: 5.0
Matrix Norm Along Specific Axis
For matrices, you can compute norms along specific axes ?
import numpy as np
from numpy import linalg as LA
# Create a matrix
matrix = np.array([[-4, -3, -2],
[-1, 0, 1],
[ 2, 3, 4]])
print("Matrix:")
print(matrix)
# Norm along axis=0 (column-wise)
norm_axis0 = LA.norm(matrix, axis=0)
print("\nNorm along axis=0 (columns):", norm_axis0)
# Norm along axis=1 (row-wise)
norm_axis1 = LA.norm(matrix, axis=1)
print("Norm along axis=1 (rows):", norm_axis1)
# Overall matrix norm (Frobenius norm)
frobenius_norm = LA.norm(matrix)
print("Frobenius norm:", frobenius_norm)
Matrix: [[-4 -3 -2] [-1 0 1] [ 2 3 4]] Norm along axis=0 (columns): [4.58257569 4.24264069 4.58257569] Norm along axis=1 (rows): [5.38516481 1.41421356 5.38516481] Frobenius norm: 9.273618495495704
Using Different Norm Orders
Different ord values give different types of norms ?
import numpy as np
from numpy import linalg as LA
matrix = np.array([[-4, -3, -2],
[-1, 0, 1],
[ 2, 3, 4]])
# Different matrix norms
print("Matrix infinity norm:", LA.norm(matrix, ord=np.inf))
print("Matrix 1-norm:", LA.norm(matrix, ord=1))
print("Matrix 2-norm:", LA.norm(matrix, ord=2))
print("Matrix -1 norm:", LA.norm(matrix, ord=-1))
Matrix infinity norm: 9.0 Matrix 1-norm: 7.0 Matrix 2-norm: 7.348469228349534 Matrix -1 norm: 4.0
Common Norm Types
| ord Parameter | Norm Type | Description |
|---|---|---|
| None or 2 | 2-norm | Euclidean norm (default) |
| 1 | 1-norm | Manhattan norm (sum of absolute values) |
| np.inf | ?-norm | Maximum absolute value |
| -np.inf | -?-norm | Minimum absolute value |
Conclusion
Use numpy.linalg.norm() to calculate vector and matrix norms. Specify axis parameter to compute norms along specific dimensions, and use different ord values for various norm types like L1, L2, and infinity norms.
