Latest Code Tutorials

np.linalg.norm: Understanding numpy.linalg.norm()

The np.linalg.norm() is a library function used to calculate one of the eight different matrix norms or vector norms. The np.linalg.norm() method takes arr, ord, axis, and keepdims as arguments and returns the norm of the given matrix or vector.

The numpy.linalg.norm() method is used to get one of eight different matrix norms or vector norms. The return value depends on the value of the given parameter.


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


The np.linalg.norm() function takes mainly four parameters :

  • arr: The input array of n-dimensional.
  • ord: This stands for orders, which means we want to get the norm value. The norm value depends on this parameter. This can be of eight types which are:
Order Norm for Matrix Norm for vector
None Frobenius norm 2 norms
nuc nuclear norm
inf max(sum(abs(x), axis=1) max(abs(x))
-inf min(sum(abs(x), axis=1)) min(abs(x))
fro Frobenius norm
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 sign. value) as below
-2 smallest singular value as below
other sum(abs(x)**ord)**(1./ord)


  • axis:  If the axis is an integer, the vector value is computed for the axis of x. If the axis is 2-tuple, then the matrix value of the specified matrix is ​​calculated. If the axis is nonexistent, then either a vector criterion (when x is 1-D) or a matrix criterion (when x is 2D) is returned.
  • keepdims: If the keepdims argument is set to True, the axes which are normed over are left in the result as dimensions with size one. With the keepdims option, the result will broadcast correctly against the original x.

Return Value

The linalg norm() function returns the norm of the given matrix or vector.

Finding the norm of an array

See the following code.

# Program to calculate the norm of an array
import numpy as np

# Creating an array
arr1 = np.arange(12)-2
arr2 = arr1.reshape(3, 4)
# Printing the array
print("First array is: \n", arr1)
print("\nSecond array is:\n ", arr2)

# Calculating norm value with different order value
print(np.linalg.norm(arr1), "\n")
print(np.linalg.norm(arr2), "\n")
print(np.linalg.norm(arr1, np.inf), "\n")
print(np.linalg.norm(arr2, np.inf), "\n")
print(np.linalg.norm(arr1, -np.inf), "\n")
print(np.linalg.norm(arr1, -np.inf), "\n")
print(np.linalg.norm(arr1, 1), "\n")
print(np.linalg.norm(arr2, 1), "\n")
print(np.linalg.norm(arr1, -1), "\n")
print(np.linalg.norm(arr1, -1))


The array is:
 [[[1 2]
  [2 3]
  [3 4]]

 [[4 5]
  [4 5]
  [3 4]]

 [[5 6]
  [4 5]
  [4 5]]]
Shape of the array is:  (3, 3, 2)
Output is:
 [ 9 12]
➜  pyt clear
➜  pyt python3
First array is:
 [-2 -1  0  1  2  3  4  5  6  7  8  9]

Second array is:
  [[-2 -1  0  1]
 [ 2  3  4  5]
 [ 6  7  8  9]]








/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/linalg/ RuntimeWarning: divide by zero encountered in reciprocal
  absx **= ord



In this example, we have first created two arrays, one is 1dimensional, and the other is 2 dimensional, and we have printed that.

After that, we have calculated the norm value with different values of ord parameters. We can see that, for different parameters, the value is different.

Finding the norm of the given vectors

To find the norm of given vectors, use the linalg norm() function.

# Program to calculate norm of an array
import numpy as np

# Making vector array
arr1 = np.array([[5, 4, 3], [-3, 2, 1]])
print("The array is: \n", arr1)

# Printing Norm for different order values
print(np.linalg.norm(arr1, axis=0), "\n")

print(np.linalg.norm(arr1, axis=1), "\n")

print(np.linalg.norm(arr1, ord=1, axis=0), "\n")


The array is:
 [[ 5  4  3]
 [-3  2  1]]
[5.83095189 4.47213595 3.16227766]

[7.07106781 3.74165739]

[8. 6. 4.]


In this example, we have first created one vector array, and then we have printed that.

Finally, we have called linalg.norm() to calculate the norm value for different order values.

That is it for the np.linalg.norm() function.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.