Norm computes the length of the vectors, and it can be understood as “distance in mathematical terms. L2 norm first takes the square of all the elements in the vector or the array and then adds all these squared elements and returns the magnitude for the vector.
Procedure
We will follow a simple procedure to get a good understanding of the topic NumPy L2 norm. The article will explain the importance of this function, and then the syntax of this function will be explained along with the implementation of that syntax in the Python script.
Syntax
There are generally two types of norms that can be expressed in the Python script, and we can switch to either one using the same basic syntax with a little modification in the parameters of this function. The method to call this function in the Python program is given as follows:
This function has three parameters that need to be expressed while computing the norm of the vectors. The “x” parameter is the name of the vector whose length we want to find using the NumPy l2 norm function. Then comes another parameter, “order” this parameter specifies if we have to find out the L1 norm or the L2 norm of the vector, and “axis” contributes to being the last parameter of the function. It returns either the vectors or the matrix’s norm; usually, we keep its value equal to none.
Example 1
The NumPy L2 norm function computes the norm for any vector or matrix, and this could be verified by writing the program for the L2 norm and executing it in the Python compiler to verify its results. To find the magnitude of the vector using the L2 norm, we will create a new project or start with the most recent project. The library that we are required to install for this function’s implementation is given below:
“Numpy”
We will install this library in our Python shell through the terminal window. After the installation of this library, we will import two main packages from this library that we will be utilized in the program to call the L2 norm function. So, we will first import the NumPy package with the prefix or name “pn” and then import another module from this package as “linalg”.
From NumPy, we will import linalg with the name “norm”. Now, we will use these pn and norm prefixes instead of linalg and NumPy in the program. We will create a vector or array with the method as “pn. array ([2, 4, 5, 9])”. We will save this array in the variable “vector”. With this array declaration, we will step ahead and find the L2 norm of this array “vector”. So, we will give the method call to function “linalg” that we had imported from the NumPy package, and now, we will use it as the norm so that the function call will be “pn. norm (x, order)”. This function will take the value of its parameter “x” as the name of the array, i.e., vector, and the order of the norm will be given equally to “2” since we are computing the L2 norm.
The results we will get from this method will be stored in another variable, say “l2_norm”, and then it will be passed to the parameter of the print () function so that we can display the results from the L2 norm function.
Example 2
In the previous example, we implemented the L2 norm for a vector or array with a span in only one dimension. This example will try to implement the L2 norm function for the 2D array or matrix. So, we will follow the same procedure as in the examples to import the two modules from the NumPy package linalg and the NumPy as “np” and “norm”, respectively. We will use both these modules to call the L2 norm function for the 2D array. With the prefix “np”, we will call the array declaration function “np. array()” . This function call will declare a 2D array with the elements as “[[2, 6, 7], [3, 5, 1]”.
The name of the array will be given as “array_2dim”. We will use this array in the np. norm () function as its input parameter will specify the order of the function to “2” to tell the function to compute the L2 norm for this 2D array. The results from this L2 norm function will then be shown as the output by calling the print function. We have attached the code snippet for this example below:
The program’s output was displayed as the L2 norm for the 2D array.
Conclusion
This article is based on the L2 norm function using the NumPy package. We have delivered the complete concept on this topic by explaining the methods to use this function with the help of various examples that we have discussed and implemented in the Python programming language in this article. We have also discussed the application area of the topic, and we hope it will clarify concepts about this function.