Tuesday, November 19, 2024
Google search engine
HomeLanguagesReturn the Norm of the vector over given axis in Linear Algebra...

Return the Norm of the vector over given axis in Linear Algebra using NumPy in Python

In this article, we will how to return the Norm of the vector over a given axis in Linear Algebra in Python.

numpy.linalg.norm() method

The numpy.linalg.norm() method is used to return the Norm of the vector over a given axis in Linear algebra in Python. Depending on the value of the ord parameter, this function can return one of the possible matrix norms or one of an unlimited number of vector norms. The Euclidean distance of a vector from the origin, A norm known as the Euclidean norm, or 2-norm, which can also be defined as the square root of the inner product of a vector with itself. axis =0 is used to find the norm along the rows and axis =1 is used to find the norm along with the columns. Below is the syntax of linalg.norm, In this the first parameter should be a 1-D or 2-D array whereas ord is the order of the norm and the axis computes the vector norms along with the axis:

Syntax: numpy.linalg.norm(x, ord, axis):

Parameters:

  • x: array of inputs Unless ord is None, x must be 1-D or 2-D if axis is None. The 2-norm of x.ravel will be returned if both axis and ord are None.
  • ord: non-zero int, inf, -inf, ‘fro’, ‘nuc’. (optional )
  • axis: {None, int, 2-tuple of ints}.If axis is an integer, it indicates the x-axis along which the vector norms should be computed. (optional )

Returns: float or ndarray. Norm of the matrix or vector is returned.

Example 1:

Here, the packages are imported and np.arrange() method is used to create an array. The .shape attribute finds the shape of the array, the .ndim attribute finds the dimension of the array, and the data type of the array is the .dtype attribute. np.linalg.norm() method is used to return the  Norm of the vector according to the axis given. axis=0 represents that we’re finding the norm of the vector along rows.

Python3




# import packages
import numpy.linalg as l
import numpy as np
# Creating an array
array = np.arange(12)
print(array)
  
# shape of the array is
print("Shape of the array is : ", array.shape)
  
# dimension of the array
print("The dimension of the array is : ", array.ndim)
  
# Datatype of the array
print("Datatype of our Array is : ", array.dtype)
  
# returning the norm of the vector over axis 0.
print(l.norm(array, axis=0))


Output:

[ 0  1  2  3  4  5  6  7  8  9 10 11]
Shape of the array is :  (12,)
The dimension of the array is :  1
Datatype of our Array is :  int64
22.494443758403985

Example 2:

In this example, the input is a matrix and the norm of the matrix is found, by specifying axis =1. It represents along with columns.

Python3




# import packages
import numpy.linalg as l
import numpy as np
# Creating an array
array = np.arange(12).reshape((3, 4))
print(array)
  
# shape of the array is
print("Shape of the array is : ", array.shape)
  
# dimension of the array
print("The dimension of the array is : ", array.ndim)
  
# Datatype of the array
print("Datatype of our Array is : ", array.dtype)
  
# returning the norm of the matrix along axis 1
print(l.norm(array, axis=1))


Output:

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
Shape of the array is :  (3, 4)
The dimension of the array is :  2
Datatype of our Array is :  int64
[ 3.74165739 11.22497216 19.13112647]

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments