Updated April 3, 2023

## Introduction to NumPy norm

The error of a given model in machine learning and deep learning can be evaluated by using a function called norm which can be thought of as the length of the vector to map the vector to a given positive value, and the length of the vector can be calculated using three vector norms namely vector L1 norm, vector L2 norm and vector max norm where vector L1 norm represents the L1 norm of the vector which calculates the absolute vector values sum and vector L2 norm represents the L2 norm of the vector which calculates the squared vectored values sum and finds its square root and vector max norm calculates the vector’s maximum value. In this topic, we are going to learn about the NumPy norm.

### Syntax

The syntax for NumPy norm in Python is as follows:

**1. norm() function is used to calculate the L1 norm of the vector in NumPyusing the formula:**

||v||1 = |a1| + |a2| + |a3|

where ||v||1 represents the L1 norm of the vector, which is equal to the absolute vector values sum.

and the syntax for the same is as follows:

`norm(arrayname, normorder=1);`

where arrayname is the name of the array whose L1 norm of the vector must be calculated and

normorder specifies the norm order of the vector, which is 1 for the L1 norm of a vector.

**2. norm() function is used to calculate the L2 norm of the vector in NumPy using the formula:**

||v||2 = sqrt(a1^2 + a2^2 + a3^2)

where ||v||2 represents the L2 norm of the vector, which is equal to the square root of squared vector values sum.

and the syntax for the same is as follows:

`norm(arrayname);`

where array name is the name of the array whose L2 norm of the vector must be calculated.

**3. norm() function is used to calculate the maximum value of the vector in NumPy using the formula:**

||v||inf = max(|a1|, |a2|, |a3|)

where ||v||inf represents the vector max norm which is equal to the maximum value of the vector.

and the syntax for the same is as follows:

`norm(arrayname, inf);`

where array name is the name of the array whose L2 norm of the vector must be calculated and inf represents infinity.

### Working of NumPy norm

- The error of a given model in machine learning and deep learning can be evaluated by using a function called norm which can be thought of as the length of the vector to map the vector to a given positive value.
- The length of the vector can be calculated using three vector norms, namely vector L1 norm, vector L2 norm, and vector max norm,
- The Vector L1 norm represents the L1 norm of the vector, which calculates the absolute vector values sum.
- The Vector L2 norm represents the L2 norm of the vector, which calculates the squared vectored values sum and finds its square root.
- The vector max norm is used to calculate the vector’s maximum value.

### Examples of NumPy norm

Given below are the examples of NumPy norm:

#### Example #1

Python program to demonstrate NumPynorm function to calculate the L1 norm of the vector of the newly created array:

**Code:**

```
#importing the package numpy and importing the package for norm
import numpy as nump
from numpy.linalg import norm
#Creating an array by making use of array function in NumPy and storing it in a variable called nameofthearray
nameofthearray = nump.array([1,2,3,4])
#Displaying the elements of nameofthearray followed by one line space by making use of \n
print 'The elements of the given array are:'
print nameofthearray
print '\n'
#using norm function of NumPy and passing the created array as the parameter to that function along with 1 to specify the order of norm to find the L1 norm of vector value and store it in a variable called L1norm
L1norm = norm(nameofthearray,1)
#Displaying the L1 norm of vector value stored in L1norm variable
print 'The L1 norm of vector value is:'
print L1norm
```

**Output:**

The package for NumPy is imported in the above program, and the package for using norm is imported. Then an array is created using the array function in NumPy, and it is stored in the variable called the name of the array. Then the elements of the array name of the array are displayed. Then the norm() function in NumPy is used to find the L1 norm of a vector bypassing the name of the array and the order of the norm, which is 1 as the parameter to the norm() function, and the result returned is stored in a variable called L1norm which is printed as the output on the screen. Finally, the output is shown in the snapshot above.

#### Example #2

Python program to demonstrate NumPy norm function to calculate the L2 norm of the vector of the newly created array:

**Code:**

```
#importing the package numpy and importing the package for norm
import numpy as nump
from numpy.linalg import norm
#Creating an array by making use of array function in NumPy and storing it in a variable called nameofthearray
nameofthearray = nump.array([1,2,3,4])
#Displaying the elements of nameofthearray followed by one line space by making use of \n
print 'The elements of the given array are:'
print nameofthearray
print '\n'
#using norm function of NumPy and passing the created array as the parameter to that function to find the L2 norm of vector value and store it in a variable called L1norm
L2norm = norm(nameofthearray)
#Displaying the L2 norm of vector value stored in L2norm variable
print 'The L2 norm of vector value is:'
print L2norm
```

**Output:**

The package for NumPy is imported in the above program, and the package for using norm is imported. Then an array is created using the array function in NumPy, and it is stored in the variable called nameofthearray. Then the elements of the array nameofthearray are displayed. Then the norm() function in NumPy is used to find the L2 norm of the vector bypassing the nameofthearray array as the parameter to the norm() function, and the result returned is stored in a variable called L2norm, which is printed as the output on the screen. The output is shown in the snapshot above.

#### Example #3

Python program to demonstrate NumPy norm function to calculate the maximum value of the vector of the newly created array:

**Code:**

```
#importing the package numpy and importing the package for norm
import numpy as nump
from numpy.linalg import norm
from numpy import inf
#Creating an array by making use of array function in NumPy and storing it in a variable called nameofthearray
nameofthearray = nump.array([1,2,3,4])
#Displaying the elements of nameofthearray followed by one line space by making use of \n
print 'The elements of the given array are:'
print nameofthearray
print '\n'
#using norm function of NumPy and passing the created array as the parameter to that function to find the maximum value of vector and store it in a variable called mnorm
mnorm = norm(nameofthearray,inf)
#Displaying the maximum value of vector value stored in mnorm variable
print 'The maximum value of vector is:'
print mnorm
```

**Output:**

The package for NumPy is imported in the above program, and the package for using norm is imported. Then an array is created using the array function in NumPy, and it is stored in the variable called nameofthearray. Then the elements of the array nameofthearray are displayed. Then the norm() function in NumPy is used to find the maximum value of vector bypassing the nameofthearray array as the parameter to the norm() function, and the result returned is stored in a variable called mnorm, which is printed as the output on the screen. Finally, the output is shown in the snapshot above.

### Recommended Articles

This is a guide to the NumPy norm. Here we discuss the concept of NumPynorm function in Python through definition, syntax, and working through programming examples and their outputs. You may also have a look at the following articles to learn more –