In this Python Scipy tutorial, we will learn about the “Python Scipy Linalg” to compute the norm vector of the one and two-dimensional array with different orders and axis of the array by covering the following topics.
- Python Scipy Linalg Norm
- How to find the norm vector using the parameter order
- Python Scipy Linalg Norm Infinity
- How to find the norm of the 2d array
- How to find the norm of the array on a specified axis
- Python Scipy Linalg Norm Keepdims
Python Scipy Linalg Norm
The method norm()
in a module scipy.linalg
of Python Scipy that is used for Vector or matrix norms. The value of the ord argument determines which of eight possible matrix norms or an infinite number of vector norms this function can return. There is only support for ord=None for tensors with ranks other than 1 or 2.
The syntax is given below.
scipy.linalg.norm(a, ord=None, axis=None, keepdims=False, check_finite=True)
Where parameters are:
- a(array_data): Input matrix, a must be either 1-D or 2-D if the axis is None, unless ord is also None. A. Ravel’s 2-norm will be returned if both axis and ord are None.
- ord(-inf,inf, int,nuc, froc): The usual order of the norm. inf stands for the inf object in NumPy.
- axis(int): Axis designates the axis of an along which the vector norms will be computed if it is an integer. A 2-tuple indicates the axes that hold 2-D matrices, and the matrix norms of these matrices are determined if the axis is such a 2-tuple. A vector norm (when an is 1-D) or a matrix norm (when an is 2-D) is returned if the axis is None.
- keepdims(boolean): The axes that are normed over are retained in the outcome as dimensions with size one if this is set to True. With this selection, the outcome will be correctly broadcast in comparison to the initial a.
- check_finite(boolean): Whether or not to make sure the input matrix only has finite numbers. Disabling may improve performance, but if the inputs do contain infinities or NaNs, it may cause issues (crashes, non-termination).
The method norm()
returns n
(The matrix or vector’s norm (s)).
Let’s take an example by following the below steps:
Import the required libraries or methods using the below python code.
from scipy import linalg
import numpy as np
Generate an array of data that represents the coldest cities in the USA in ascending order like Firebanks
, Grand Forks
, Williston
, Fargo
, Duluth
, Aberdeen
, St.Cloud
, Bismarck
, Marquette
and Huron
using the below code.
array_data = np.arange(10) - 3.0
array_data
Compute the norm of the above data using the below code.
linalg.norm(array_data)
This is how to compute the norm of the array using the method norm()
of Python Scipy.
Python Scipy Linalg Norm Order
The method norm()
of Python Scipy accepts a parameter ord
which is used for the order of the norm vector. The parameter ord
accepts the following values as shown below in the table.
Ord | norm for vectors | norm for matrices |
fro | – | Frobenius norm |
nuc | – | nuclear norm |
None | 2-norm | Frobenius norm |
-inf | min(abs(a)) | min(sum(abs(a), axis=1)) |
inf | max(abs(a)) | max(sum(abs(a), axis=1)) |
0 | sum(a != 0) | – |
-1 | as below | min(sum(abs(a), axis=0)) |
1 | as below | max(sum(abs(a), axis=0)) |
-2 | as below | Smallest singular value |
2 | as below | 2-norm (largest sing. value) |
Let’s take an example by following the below steps:
Import the required libraries or methods using the below python code.
from scipy import linalg
import numpy as np
Generate an array of data and reshape it to dimension (3,3) using the below code.
array_data = np.arange(9) - 3.0
array_reshape = array_data.reshape((3,3))
array_reshape
Compute the norm of the above data using the ord equal to fro using the below code.
linalg.norm(array_reshape,'fro')
Again compute the norm with the ord equal to nuc using the below code.
linalg.norm(array_reshape,'nuc')
This is how to compute the norm using the parameter ord
of the method norm()
of Python Scipy.
Read Python Scipy Mann Whitneyu
Python Scipy Linalg Norm Infinity
The parameter ord
also accepts values like inf
, -inf
to define the order of the norm that we have already learned in the above subsection “Python Scipy Linalg Norm Order”. Here we will only take examples to see how the norm value changes if we change the order values to infinity.
Import the required libraries or methods using the below python code.
from scipy import linalg
import numpy as np
Create an array of data and reshape it to dimension (3,3) using the below code.
array_data = np.arange(9) - 3.0
array_reshape = array_data.reshape((3,3))
array_reshape
Find the norm of the above data using the ord equal to inf using the below code.
linalg.norm(array_reshape,np.inf)
Again compute the norm of the 1d array with the ord equal to inf using the below code.
linalg.norm(array_data,np.inf)
This is how to compute the norm with an order equal to infinity using the method norm()
with parameter ord
.
Read Python Scipy Stats Poisson
Python Scipy Linalg Norm 2d array
We have already computed the norm of the 1d array and also reshaped the array to different dimensions to compute the norm, so here we will see how to compute the norm of the two-dimensional array by following the below steps:
Import the required libraries or methods using the below python code.
from scipy import linalg
import numpy as np
Create a 2d array of data using the below code.
array_data = np.array([[2, 1, 3], [6, 4, 6]])
array_data
Compute the norm of the above-created 2d data using the below code.
linalg.norm(array_data)
This is how to compute the norm of the 2d data using the method norm()
of Python Scipy.
Python Scipy Linagl Norm Axis
The Python Scipy method norm()
has a parameter axis
to compute the vector norm along the specified axis of the given array.
If the axis is an integer, it designates the axis along which the vector norms will be computed. A 2-tuple indicates the axes that hold 2-D matrices, and when this occurs, the matrix norms of these matrices are calculated. When the axis is None, either a vector norm (for 1-D a) or a matrix norm (for 2-D a) is returned.
Let’s take an example and compute the vector norm along the axis of the array by following the below steps:
Import the required libraries or methods using the below python code.
from scipy import linalg
import numpy as np
Create a 2d array of data using the below code.
array_data = np.array([[2, 1, 3], [6, 4, 6]])
array_data
First, find the norm of the above-created 2d data without specifying the axis or on the default axis using the below code.
linalg.norm(array_data)
Find the norm along the axis equal to zero using the below code.
linalg.norm(array_data,axis = 0)
Again find the norm along the axis equal to one using the below code.
linalg.norm(array_data,axis = 1)
This is how to find the norm vector along the specific axis of the given array using the parameter axis
of the method norm()
of Python Scipy.
Read Python Scipy Kdtree
Python Scipy Linalg Norm Keepdims
The Python Scipy method norm()
accepts a parameter keepdims
that the normed-over axes are retained in the result as dimensions of size one if this is set to True. The outcome will be correctly communicated when using this option in comparison to the original a.
Let’s take an example and see the effect of keeping the parameter keepdims
equal to true by following the below steps:
Import the required libraries or methods using the below python code.
from scipy import linalg
import numpy as np
Create a 2d array of data using the below code.
array_data = np.array([[3, 1, 2], [4, 6, 5]])
array_data
First, find the norm of the above-created 2d data without specifying the keepdims
parameter or on the default value of the keepdims
parameter using the below code.
linalg.norm(array_data)
Now change the value of keepdims
equal to True using the below code.
linalg.norm(array_data,keepdims = True)
This is how to use the parameter keepdims
with method norm()
of Python Scipy.
We have learned how to find the norm vector of the given array with parameters ord, axis, and keepdims, and also learned about how to find the norm of the 2d array with the following topics.
- Python Scipy Linalg Norm
- How to find the norm vector using the parameter order
- Python Scipy Linalg Norm Infinity
- How to find the norm of the 2d array
- How to find the norm of the array on a specified axis
- Python Scipy Linalg Norm Keepdims
You may like the following Python Scipy tutorials:
- Python Scipy Eigenvalues
- Python Scipy Stats Kurtosis
- Python Scipy Stats Mode
- Python Scipy Stats Multivariate_Normal
- Python Scipy Freqz
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.