Calculating the norm
To define how close two vectors or matrices are, and to define the convergence of sequences of vectors or matrices, the norm is used. The function scipy.linalg.norm is used to calculate the matrix or vector norm.
Syntax: scipy.linalg.norm(a , ord , axis , keepdims , check_finite)
Parameters:
- a: It is an input array or matrix.
- ord (Optional): It is the order of the norm
- axis (Optional): It denotes the axes.
- keepdims (Optional): If the value is True, the axes which are normed over are left in the output as dimensions with size=1.
- check_finite (Optional): It checks if the input matrix consists of only finite numbers.
Returns:
- scipy.linalg.norm returns the norm of a.
The function scipy.linalg.norm returns one of the seven different matrix norms or one of an infinite number of vector norms.
- The L2 norm evaluates the distance of the vector coordinate from the origin of the vector space. It is also known as the Euclidean norm as it is calculated as the Euclidean distance from the origin. The result is a positive distance value.
- The L1 norm is evaluated as the sum of the absolute vector values. It is an evaluation of the Manhattan distance from the origin of the vector space.
Example:
Python
# Importing the required libraries from scipy import linalg import numpy as np # Initializing the input array x = np.array([ 6 , 3 ]) # Calculating the L2 norm a = linalg.norm(x) # Calculating the L1 norm b = linalg.norm(x , 1 ) # Displaying the norm values print (a) print (b) |
Output:
SciPy Linear Algebra – SciPy Linalg
The SciPy package includes the features of the NumPy package in Python. It uses NumPy arrays as the fundamental data structure. It has all the features included in the linear algebra of the NumPy module and some extended functionality. It consists of a linalg submodule, and there is an overlap in the functionality provided by the SciPy and NumPy submodules.
Let’s discuss some methods provided by the module and its functionality with some examples.