np linalg norm method to find Norms of Arrays

np linalg norm : A Numpy method to Find Norms of Arrays

In machine learning sometimes We have to calculate or measure the size of the matrix or vector. Numpy has a method that allows you to do so. In this entire article, I will show how to calculate the norms of the matrix or vector using the np linalg norm method.

 

Formulae of the Norm in Machine Learning

Suppose you have a vector x then the formulae of the linear norm is the below.

Formulae of the norm in Machine Learning
Formulae of the norm in Machine Learning

Finding the norms using np linalg norm method.

Numpy provides us a method to calculate the norms of a NumPy array. It is numpy.linalg.norm(). Just follow the steps given below.

Step 1: Import the necessary library.

The first essential step is to import the NumPy library. Make sure you must have installed the NumPy on pycharm as I am doing all the code on Pycharm only.

import numpy as np 

Step 2: Create a Numpy array or matrix.

Let’s create a NumPy matrix for calculating the norms of it. You can create a matrix using the numpy.array() method. Use the code given below.

1-D Numpy array

array_1d = np.array([10,20,30,40,50,60,70,80,90,100])

2-D Numpy Array

array_2d = np.array([[10, 20, 30, 40], [50, 60, 70, 80]])

Step 3:  Calculate the np linalg norm of the array

To calculate the norm of the array you have to use the numpy.linalg.norm() method. Let’s calculate the norms for each array created in step 2.

1-D Numpy array

norm_1d = np.linalg.norm(array_1d)

2-D Numpy Array

norm_2d = np.linalg.norm(array_2d)

You can also calculate the vector or matrix norm of the matrix by passing the axis value 0 or 1.

When the axis value is 0, then you will get three vector norms for each column.

norm_axis_0 = np.linalg.norm(array_2d, axis=0)

In the same case when the value of the axis parameter is 1, then you will get the vector norms for each row.

norm_axis_1 = np.linalg.norm(array_2d, axis=1)

There are two great terms in the norms of the matrix one is Frobenius(fro) and nuclear norm. By default np linalg norm method calculates nuclear norms.  But You can easily calculate Frobenius norms using passing the abbreviation of it that fro. Use the code given below.

fro_norms = np.linalg.norm(array_2d,"fro")

Full Code

import numpy as np


def main():
    # 1-D Array
    array_1d = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
    norm_1d = np.linalg.norm(array_1d)
    print("The value of the norm of 1-D array is: ", norm_1d)

    # 2-D Array
    array_2d = np.array([[10, 20, 30, 40], [50, 60, 70, 80]])
    norm_2d = np.linalg.norm(array_2d)
    print("The value of the norm of 2-D array is: ", norm_2d)

    # vector norm each rows and columns
    norm_axis_0 = np.linalg.norm(array_2d, axis=0)
    print("The value of vector norm with axis=0 is: ", norm_axis_0)
    norm_axis_1 = np.linalg.norm(array_2d, axis=1)
    print("The value of vector norm with axis=1 is: ", norm_axis_1)

    # Frobenius matrix norm.
    fro_norms = np.linalg.norm(array_2d, "fro")
    print("Frobenius Norm: ", fro_norms)


if __name__ == '__main__':
     main() 

Output

Output for the various norms calculation
Output for the various norms calculation

End Notes

Norm is a great way to measure the size of the matrix or vector. Numpy provides np linalg norm method for that. The next thing you can use norms is to normalize an array. Hope you have understood this article. Even if you have any queries regarding it then you can contact us for more information.

Source:

Offical numpy.linalg.norm() documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner