NumPy linalg.norm()
NumPy Linear Algebra: `numpy.linalg.norm()`
Linear Algebra is a branch of mathematics concerning linear equations and their representations through matrices and vector spaces. In NumPy, the `linalg.norm()` function calculates the norm of a vector or matrix, which is a measure of its magnitude.
Usage
The `numpy.linalg.norm()` function is used to compute different norms of vectors and matrices, which are essential in various mathematical and machine learning applications. It can calculate norms such as L1, L2, Frobenius, and others like infinity norm by specifying the appropriate parameters.
numpy.linalg.norm(x, ord=None, axis=None, keepdims=False)
x
: Input array (vector or matrix).ord
: Order of the norm (e.g., 1, 2, 'fro', `np.inf` for infinity norm).axis
: Axis along which to compute the norms. If not specified, the norm of the entire array is computed.keepdims
: If True, retains reduced dimensions with length one.
By default, if `ord` and `axis` are not specified, the function computes the L2 norm of the entire array.
Examples
1. Basic Vector Norm (L2 Norm)
import numpy as np
vector = np.array([3, 4])
norm = np.linalg.norm(vector)
print(norm)
This example computes the L2 norm (Euclidean norm) of a 2D vector, which results in 5.0, representing its magnitude.
2. Matrix Frobenius Norm
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
norm = np.linalg.norm(matrix, 'fro')
print(norm)
Here, the Frobenius norm is specified using the `ord` parameter. It calculates the square root of the sum of the absolute squares of its elements.
3. L1 Norm of a Matrix along Axis
import numpy as np
matrix = np.array([[1, -2, 3], [-4, 5, -6]])
norm = np.linalg.norm(matrix, ord=1, axis=0)
print(norm)
This example uses the `ord` and `axis` parameters to calculate the L1 norm (sum of absolute values) along each column of the matrix.
4. Infinity Norm of a Vector
import numpy as np
vector = np.array([1, -2, 3])
norm = np.linalg.norm(vector, ord=np.inf)
print(norm)
This example demonstrates the use of `ord=np.inf` to compute the maximum absolute value in the vector.
Tips and Best Practices
- Select an appropriate norm. Choose the norm that aligns with your application's requirements, like L1 for sparsity or L2 for magnitude.
- Leverage the `axis` parameter. Compute norms along specific dimensions for multidimensional arrays using the `axis` parameter.
- Use `keepdims` for dimension consistency. Set `keepdims=True` to retain dimensions, aiding in subsequent broadcasting operations.
- Manage complex numbers. For arrays with complex numbers, norms are calculated using the modulus of the elements.
- Consider computational resources. Be cautious of computational cost and memory usage with large matrices.