The numpy norm of a vector or matrix is the maximum absolute value of all its components. The function numpy.linalg.norm() calculates the norm of a matrix or vector in Python using NumPy library and returns one among seven norms depending on parameters specified as inputs to this function:
Euclidean Norm: This is also called 2-norm because it equals to square root of sum of squares, that is, √(x*x + y*y + z*z). It can be calculated by numpy.linalg.euc().
- Manhattan Norm: This is also called 1-norm and equals to sum (or integral) over non-zero elements multiplied with the corresponding elements of vector or matrix. It can be calculated by numpy.linalg.manh().
Table of Contents
The np.linalg.norm() Function in NumPy
The np.linalg.norm() function is used in NumPy to calculate one of the eight different matrix norms or one of the vector norms. It can be used with matrices, vectors, or general arrays. The result will always be a scalar value that represents how far apart each element in an array is from the mean (average) of that array!
Syntax
numpy.linalg.norm(x, ord=None, axis=None, keepdims=False)
Parameters
x: array_like
Array of inputs. Unless ord is None, x must be 1-D or 2-D if axis is None. The 2-norm of x.ravel will be returned if both axis and ord are None.
Ord: {non-zero int, inf, -inf, ‘fro’, ‘nuc’}, optional
The normative order (see table under Notes). Numpy’s inf object is denoted by inf. None is the default value.
Axis: {None, int, 2-tuple of ints}, optional.
If axis is an integer, it indicates the x-axis along which the vector norms should be computed. The axes that hold 2-D matrices are specified if axis is a 2-tuple, and the matrix norms of these matrices are computed. If axis is None, either a vector norm (for 1-D x) or a matrix norm (for 2-D x) is returned. None is the default value.
In version 1.8.0, there is a new feature.
Keepdims: bool, optional
If this is set to True, the axes that are normed over are left as dimensions of size one in the result. The result will broadcast accurately against the original x if you use this option.
In version 1.10.0, there is a new feature.
Returns
N: float or ndarray
The matrix or vector’s Norm (s).
Vector Norm Example
import numpy as np
vector = np.arange(5)
vector_norm = np.linalg.norm(vector)
print("Vector norm:")
print(vector_norm)
Output

Matrix Norm Example
import numpy as np
matrix = np.array([[ 3, 6, 9],
[2, 4, 6]])
matrix_norm = np.linalg.norm(matrix)
print("Matrix norm:")
print(matrix_norm)
Output

Matrix Norm Along Particular Axis
import numpy as np
matrix = np.array([[ 3, 6, 9],
[2, 4, 6]])
matrix_norm = np.linalg.norm(matrix, axis = 1)
print("Matrix norm along particular axis :")
print(matrix_norm)

Vector/Matrix Norm
import numpy as np
vector = np.arange(4)
matrix = vector.reshape((2, 2))
vector_norm = np.linalg.norm(vector)
print("Vector norm:")
print(vector_norm)
matrix_norm = np.linalg.norm(matrix)
print("Matrix norm:")
print(matrix_norm)
Output

Conclusion
The np.linalg.norm() function in NumPy calculates one of the eight different matrix norms or vector norm and can be used with matrices, vectors, and general arrays. This is a handy tool when you need to calculate distances between elements within your data set!