The numpy.linalg.norm() method calculates the matrix or vector norm of an input array. Norms quantify the “size” or “magnitude” of vectors and matrices.
A vector norm measures the “length” or “magnitude” of a vector, while a matrix norm measures the size of a matrix in terms of rows, columns, or singular values.
Let’s calculate the vector norm (Euclidean distance L2). It measures straight-line distance.
import numpy as np vector = np.array([2, 4]) vector_norm = np.linalg.norm(vector) print(vector_norm) # Output: 4.47213595499958
In the above code, we calculated the Euclidean distance between two points. 2 and 4.
Behind the scenes, it will calculate like this: sqrt(2^2 + 4^2) = sqrt(4 + 16) = sqrt(20) and square root of 20 is 4.4721.
So, concepts like Euclidean distance (L2 norm) or Manhattan distance (L1 norm) are vector norms.
Syntax
numpy.linalg.norm(x, ord=None, axis=None, keepdims=False)
Parameters
| Argument | Description |
| x (array_like, required) | It represents an array or scalar. It must be 1D or 2D. |
| ord (optional, default=None) | It specifies the norm order.
Vector norms:
Matrix norms:
|
| axis (optional, default=None) |
It represents an axis or axes over which to compute the norm. |
| keepdims (bool, optional, default=False) | If it is True, it retains the reduced axes in the output as singleton dimensions. |
1-norm (Manhattan), Infinity norm, General p-norm (p=3)
Let’s calculate the Manhattan norm, the infinity norm, and the general p-norm for vectors.
import numpy as np vector = np.array([3, 4]) # 1-norm (Manhattan) manhattan_norm = np.linalg.norm(vector, ord=1) print(manhattan_norm) # Output: 7.0 (|3| + |4| = 7) # Infinity norm (max absolute value) infinity_norm = np.linalg.norm(vector, ord=np.inf) print(infinity_norm) # Output: 4.0 (max(|3|, |4|) = 4) # General p-norm (p=3) pnorm = np.linalg.norm(vector, ord=3) print(pnorm) # Output: 4.497941445275415 # ∣1∣^3=1,∣2∣^3=8,∣3∣^3 = 27 # 1+8+27 = 36 (Sum) # 36^(1/3) = 4.497941445275415 (Cube Root)
You can see that for different types of norms, we passed different ord values. For example, for 1-norm, we passed ord=1, for infinity norm, we passed ord=np.inf, and for p-norm, we passed ord=3.
Matrix norms
Let’s find the Frobenius (default for matrices) norm of the flattened matrix.
import numpy as np mat = np.array([[1, 2], [3, 4]]) matrix_norm = np.linalg.norm(mat) print(matrix_norm) # Output: 5.477225575051661
Here is the explanation for the output: sqrt(1^2 + 2^2 + 3^2 + 4^2) = 5.47722
1-norm (max column sum), Infinity norm (max row sum), and Nuclear norm
1-norm is the maximum absolute column sum of the matrix.
∞-norm is the maximum absolute row sum of the matrix.
The nuclear norm is the sum of singular values.
import numpy as np mat = np.array([[1, 2], [3, 4]]) # 1-norm (max column sum) print(np.linalg.norm(mat, ord=1)) # Output: max(|1|+|3|, |2|+|4|) = 6.0 # Infinity norm (max row sum) print(np.linalg.norm(mat, ord=np.inf)) # Output: max(|1|+|2|, |3|+|4|) = 7.0 # Nuclear norm (sum of singular values) print(np.linalg.norm(mat, ord='nuc')) # Output: 5.8309518948453
By passing ord=1, we calculated the 1-norm of a matrix.
By passing ord=np.inf, we calculated the infinity norm of an input matrix.
And finally, for nuclear norm, we passed ord=nuc.
That’s all!


