| by Arround The Web | No comments

## Numpy Linalg Norm

In machine learning, it is occasionally necessary to determine or quantify the matrix or vector size. It can be achieved using a linalg norm technique in Numpy. We will demonstrate how to use the np linalg norm technique to determine the norms of the matrix or vector throughout the entire article.

## What is the Numpy linalg Norm?

The matrix norm or vector norms are computed using the NumPy linalg norm technique. The purpose of this function, which is also included in the NumPy library, is to compute the norms. As a result, one of the vector norms or eight of the matrix norms can be calculated using it. Although matrix norms are nothing more than a vector norm’s extension, they nonetheless exist. Additionally, it can compute infinite vector norms.

## Syntax of Numpy linalg Norm

The linalg norm function’s syntax is shown below following the Python standards.

The four input parameters for this function are listed below:

• x is an input array.
• ord stands for ‘order.’
• The matrix norms of the matrices are calculated if the axis is a 2-tuple.
• keepdims receives either a ‘True’ or ‘False’ value. If the value is true, the normed over axes are kept in the result as dimensions with size 1. Otherwise, the outcome keeps the axes that were normed over.

Now, you can discover some examples on resolving issues in the next sections.

## Example 1:

Let’s use the numpy.linalg.norm() function for identifying the norm of a vector or even a matrix. This function is used consistently to return a float or an array of norm values when the array is used as an input. Look at the code.

Here, we imported the vector as ‘vec = numpy.arrange(10)’ before importing the numpy module. The constructed array was then supplied as a parameter to the numpy.linalg.norm() function, which returned the result and was saved in the output variable. We printed the outcome in the final statement as you can see below.

import numpy

vec = numpy.arange(10)

res = numpy.linalg.norm(vec)

print(res)

The code generated the following output:

## Example 2:

Let’s acquire a 1-D array and NumPy linalg.norm() now. Here, we’ll demonstrate how to compute vectors or a matrix’s norm in a one-dimensional NumPy array using the numpy.linalg.norm() method. To begin, let’s build an array using numpy.array() method. See the code in the sample below.

import numpy

arr_d = numpy.array([6, 8, 12, 16, 22, 28, 32])

res_arr = numpy.linalg.norm(arr_d)

print(res_arr)

We have imported the necessary module, numpy, into the code. Then, we produced a 1-D array, which is visible in the second line of the program’s code. There are 7 values in the array. These are [6, 8, 12, 16, 22, 28, 32]. Next, we apply the linalg.norm() method to the newly constructed 1-D array that we gave to this function and then report the outcome. In the code’s final line, we utilized the print statement to do this.

Here you can see the result.

## Example 3:

In the example above, we have utilized a 1-D array and the NumPy linalg.norm() function. Let’s attempt the same function now with a 2-D array. The 2-D array is used here as input and doing that the function produces a float or maybe an array of norm values. See the code explained below.

After importing the numpy module, we have constructed a 2-D array in this case. This array includes the values [1, 5, 11] and [4, 8, 12]. Now, using the 2-D array we prepared earlier, we obtain the linalg.norm(). Finally, we used the print command to display the outcome. The full source code is provided below.

import numpy

new_arr = numpy.array([[1, 5, 11], [4, 8, 12]])

res_arr = numpy.linalg.norm(new_arr)

print(res_arr)

The following result is generated from the above code:

## Example 4:

A NumPy array’s matrix norm can also be calculated along with a chosen axis. We will use the axis=0 option via the linalg.norm() function to get the matrix norm for rows. Similarly, use axis=1 to determine the matrix norm for each column. The axis argument will be given as a 2-tuple of integer values.

To help you get the concept better, we will divide the code into many portions and explain each one separately. See the code below that is attached.

Here, we constructed the array and imported the numpy module. The values in the array ‘array _one’ are [5, 9, 11], and [6, 10, 22]. After that, we obtain the values returned by linalg.norm() over the column and for each of the two rows. As you can see, the method receives the newly generated array along with an axis that is set to 1. We displayed the outcome in the final line after saving it in a different array called “res_arr.”

The result is given below.

Let’s now see how the linalg.norm() function is applied to rows. We will demonstrate how to obtain the linalg.norm() values for each of the three columns and each row using the provided code below. As you can see, the function’s parameters are the newly generated array and the axis, which is set to 0.

We have now shown the outcome.

Instead, we’ll explain how to use a 2-d array along with the axis to determine a vector’s numpy norm. The remaining code is the same as the code above, with the exception that since we are using a 2-D array, the axis is set to (0,1). Here is the code:

Below, the resulting screenshot is attached.

The complete code program is attached below.

import numpy

array_one = numpy.array([[5, 9, 11], [6, 10, 22]])

res_arr = numpy.linalg.norm(array_one, axis = 1)

print(res_arr)

array_two = numpy.linalg.norm(array_one, axis = 0)

print(array_two)

array_three = numpy.linalg.norm(array_one,axis= (0,1))

print(array_three)

## Conclusion

This tutorial was created to review Numpy linalg norm. We have provided details about the Python function numpy.linalg.norm() to find a matrix or vector norm. It provides one of the infinite vector norms and it depends on what was given as an input.

Source: linuxhint.com