Python – scipy.linalg.norm


The norm() function of the scipy.linalg package is used to return one of eight different matrix norms or one of an infinite number of vector norms.

Syntax

scipy.linalg.norm(x)

Where x is an input array or a square matrix.

Example 1

Let us consider the following example −

# Importing the required libraries from scipy
from scipy import linalg
import numpy as np

# Define the input array
x = np.array([7 , 4])
print("Input array:
", x) # Calculate the L2 norm r = linalg.norm(x) # Calculate the L1 norm s = linalg.norm(x, 3) # Display the norm values print("Norm Value of r :", r) print("Norm Value of s :", s)

Output

The above program will generate the following output −

Input array:
[7 4]
Norm Value of r : 8.06225774829855
Norm Value of s : 7.410795055420619

Example 2

Let us take another example −

# Importing the required libraries from scipy
from scipy import linalg
import numpy as np

# Define the input array
x = np.array([[ 6, 7, 8], [9, -1, -2]])
print("Input Array :
", x) # Calculate the L2 norm p = linalg.norm(x) # Calculate the L1 norm q = linalg.norm(x, axis=1) # Display the norm values print("Norm Values of P :", p) print("Norm Values of Q :", q)

Output

It will produce the following output −

Input Array :
[[ 6 7 8]
[ 9 -1 -2]]
Norm Values of P : 15.329709716755891
Norm Values of Q : [12.20655562 9.2736185 ]

Updated on: 22-Dec-2021

366 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements