Python

Finding Norm of an Array Using NumPy

In this post, I describe finding the norm of a numpy array. The norm of an array is a function that maps the array to a non-negative real number. To find the norm of a numpy array, we use the numpy’s numpy.linalg.norm method. The method takes an array or an array-like object (ex: Python lists) as input and returns a float or an array of norm values.

Let’s see an example.

$ python3

Python 3.8.5 (default, Mar  8 2021, 13:02:45)

[GCC 9.3.0] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import numpy as np

>>> a = np.linspace(-4, 4, 9)

>>> a

array([-4., -3., -2., -1.,  0.,  1.,  2.,  3.,  4.])

>>> np.linalg.norm(a)

7.745966692414834

The default norm computed by numpy is the L2-norm which is also known as the Euclidean norm. The order of norm can be specified using the ord parameter supplied to numpy.linalg.norm. Continuing from above,

>>> np.linalg.norm(a, ord=1)

20.0

The above statement calculated norm 1. Norm 1 is simply the sum of the absolute values of the array. In general, the norm of a vector for any order ord is computed as:

 (∑i | x |ord )1/ord

Where the summation is performed over the absolute value of each element of the array. One can calculate the infinity norm bypassing np.inf as an order. Norm infinity is the maximum absolute value of all elements in the array.

>>> np.linalg.norm(a, ord=np.inf)

4.0

Suppose we have a matrix for which the norm should be computed.

>>> a = np.linspace(-4, 4, 9).reshape(3, 3)

>>> a

array([[-4., -3., -2.],

[-1.,  0.,  1.],

[ 2.,  3.,  4.]])

>>> np.linalg.norm(a)

7.745966692414834

The above returns the euclidean norm computed across the entire matrix. But there are scenarios where we will be required to compute norms across a particular axis. NumPy also allows using a parameter axis to specify an axis along which the norm can be computed for matrices. Using the parameter axis, one can pass the axis across which the norm should be computed. Axis 0 is the first dimension. Continuing from the previous example, if we specify axis=0, the norm will be computed across the rows, and specifying axis=1 computes the norm across the columns.

>>> a

array([[-4., -3., -2.],

           [-1.,  0.,  1.],

           [ 2.,  3.,  4.]])

>>> np.linalg.norm(a, axis=0)

array([4.58257569, 4.24264069, 4.58257569])

>>> np.linalg.norm(a, axis=1)

array([5.38516481, 1.41421356, 5.38516481])

If it is a multi-dimensional matrix, a tuple of integers that specifies the axis across which the norm is to be computed can be passed to the axis parameter.

>>> a = np.linspace(1, 8, 8).reshape(2, 2, 2)

>>> a

array([[[1., 2.],

           [3., 4.]],

           [[5., 6.],

            [7., 8.]]])

>>> np.linalg.norm(a, axis=(1, 2))

array([ 5.47722558, 13.19090596])

>>> a[0,:,:]

array([[1., 2.],

           [3., 4.]])

>>> np.linalg.norm(a[0,:,:])

5.477225575051661

>>> a[1,:,:]

array([[5., 6.],

           [7., 8.]])

>>> np.linalg.norm(a[1,:,:])

13.19090595827292

In the above example, when we specified axis=(1,2) the norm is computed across the axis 1 and 2 for every subarray in axis 0.

About the author

Arun Palaniappan