What is the np.linalg.norm() method in NumPy?

In NumPy, the np.linalg.norm() function is used to calculate one of the eight different matrix normsMagnitude of the matrix or one of the vector normsMagnitude of the vector.

Syntax

numpy.linalg.norm(x, ord=None, axis=None, keepdims=False)

Parameters

  1. x: This is an input array.
  2. ord: This stands for “order”. The different orders of the norm are given below:
order norm for matrix norm for vector
None Frobenius norm 2-norm
fro Frobenius norm
nuc nuclear norm
inf max(sum(abs(x), axis=1) max(abs(x))
-inf min(sum(abs(x), axis=1)) min(abs(x))
0 sum(x != 0)
1 max(sum(abs(x), axis=0)) as below
-1 min(sum(abs(x), axis=0)) as below
2 2-norm (largest sing. value) as below
-2 smallest singular value as below
other sum(abs(x)**ord)**(1./ord)
  1. axis: If the axis is an integer, then the vector norm is computed for the axis of x. If the axis is a 2-tuple, the matrix norms of specified matrices are computed. If the axis is None, then either a vector norm (when x is 1-D) or a matrix norm (when x is 2-D) is returned.

  2. keepdims: It receives a boolean value. If its value is​ true, then the axes that are normed over are left in the result as dimensions with size one. Otherwise, the axes which are normed over are kept in the result.

Code examples

Let’s see different code examples of the numpy.linalg.norm() function with different parameter configurations.

from numpy import linalg as LNG
import numpy as np
# creating an array using arrange function
# -3, -2, -1, 0, 1, 2, 3, 4, 5
a = np.arange(9) - 3
# converting the array into a matrix of 3x3
a = a.reshape((3,3))
# Printing matrix
print(a)
# computing norm for a matrix
print(LNG.norm(a))
# computing vector norms
print(LNG.norm(a, axis=0))

Line 6: it first creates a 3x3 matrix filled with numbers from -3 to 5.

Line 13: it calculates the Frobenius norm of the matrix using LNG.norm(a), which computes the square root of the sum of the squared values of all elements.

Line 16: it calculates the vector norms along the columns of the matrix using LNG.norm(a, axis=0), which computes the Euclidean/L2 norm of each column vector.

In the following example, we will define ord for matrix norm and axis=1 for vector norm calculations.

from numpy import linalg as LNG
import numpy as np
# creating an array using arrange function
# -3, -2, -1, 0, 1, 2, 3, 4, 5
a = np.arange(9) - 3
# converting the array into a matrix of 3x3
a = a.reshape((3,3))
# Printing matrix
print(a)
# computing norm for a matrix
print(LNG.norm(a, ord= -np.inf))
# computing vector norms
print(LNG.norm(a, axis= 1))

Line 13: it computes the norm of the matrix using the ord parameter set to -np.inf, which calculates the minimum absolute row sum.

Line 16: it computes the vector norms along the rows of the matrix using axis=1, which computes the Euclidean norm for each row vector.

Let's see another example where we're using different values of the ord parameter with an axis parameter for the matrix norm.

from numpy import linalg as LNG
import numpy as np
# creating an array using arrange function
# -3, -2, -1, 0, 1, 2, 3, 4, 5
a = np.arange(9) - 3
# converting the array into a matrix of 3x3
a = a.reshape((3,3))
# Printing matrix
print(a)
# computing norm for a matrix
print(LNG.norm(a, ord= 2, axis=1))
# computing vector norms
print(LNG.norm(a, axis=0))

Line 13: it computes the L2 norm along each row of the matrix using LNG.norm(a, ord=2, axis=1), which returns an array containing the L2 norms of each row.

Line 16: it computes the L2 norm along each column of the matrix using LNG.norm(a, axis=0), which returns an array containing the L2 norms of each column.

Copyright ©2024 Educative, Inc. All rights reserved