Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

numpy
np.linalg.norm()
method

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

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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

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

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))

RELATED TAGS

numpy
np.linalg.norm()
method
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring