In machine learning, vectors and matrixes are used extensively to represent features, targets and other parameters and hyperparameters. Calculating the length or magnitude of the vector used in a particular model is often required in several matrix operations or direct implementations in regularization methods. The length of a vector is a positive number, that speaks on the extent of the vector in space. In this article, you will know about vector norm and the method to apply them in python by using the Linear Algebra module of the NumPy library. In general, three types of norms are used,

- L1 norm
- L2 norm
- Vector Max Norm
L1 Norm :
This one is also known as Taxicab Norm or Manhattan Norm, represented as ||V||_1 , where V is the representation for the vector. L1 norm is the sum of the absolute value of the scalars it involves, For example, Suppose you have vector a = a_1i+b_1j+c_1k , then the L1 norm can be calculated by, ||a||_1 = |a_1|+|b_1|+|c_1| , where |x| is the magnitude of x . Mathematically, it’s same as calculating the Manhattan distance of the vector from the origin of the vector space. In python, NumPy library has a Linear Algebra module, which has a method named norm(), that takes two arguments to function, first-one being the input vector v, whose norm to be calculated and the second one is the declaration of the norm (i.e. 1 for L1, 2 for L2 and inf for vector max).
# importing array module from the NumPy library
# This module can transform a list passed as an argument, to an array in python
from numpy import array
# from the linalg (linear algebra) module of the library
# importing a method named norm
from numpy.linalg import norm
# passing a list of integers to make a vector
v = array([1,2,3])
# creating an object to store the value(float) returned by the method
# passing the input vector v and 1 as arguement(1 represents L1 norm)
l1 = norm(v,1)
# printing out the result
print(l1)
L2 Norm :
This one is also known as Euclidian Norm, represented as ||V||_2 , where V is the representation for the vector. L2 norm is the square root of the sum of the squares of the scalars it involves, For example, Suppose you have vector a = a_1i+b_1j+c_1k , then the L2 norm can be calculated by, ||a||_2 = \(\sqrt(a_1^2 + b_1^2 +c_1^2)\) .Mathematically, it’s same as calculating the Euclidian distance of the vector coordinates from the origin of the vector space, resulting in a positive value. You can use NumPy for this purpose too.
# importing array module from the NumPy library
# This module can transform a list passed as an argument, to an array in python
from numpy import array
# from the linalg (linear algebra) module of the library
# importing a method named norm
from numpy.linalg import norm
# passing a list of integers to make a vector
v = array([1,2,3])
# creating an object to store the value(float) returned by the method
# passing the input vector v and 2 as arguement(2 represents L2 norm)
l2 = norm(v,2)
# printing out the result
print(l2)
Vector Max Norm :
This one is represented as ||V||_\(\infty\) , where V is the representation for the vector. Vector Max norm is the maximum of the absolute values of the scalars it involves, For example, Suppose you have vector a = a_1i+b_1j+c_1k , then the Vector Max norm can be calculated by, ||a||_\(\infty\) = max(|a_1|,|b_1|,|c_1|) , where |x| is the magnitude of x . Mathematically, it’s same as calculating the maximum of the Manhattan distances of the vector from the origin of the vector space.
# importing array module from the NumPy library
# This module can transform a list passed as an argument, to an array in python
# inf is the infinity module, that contains the infinity
from numpy import array,inf
# from the linalg (linear algebra) module of the library
# importing a method named norm
from numpy.linalg import norm
# passing a list of integers to make a vector
v = array([1,2,3])
# creating an object to store the value(float) returned by the method
# passing the input vector v and inf as arguement(inf represents Vector Max norm)
vec_max = norm(v,inf)
# printing out the result
print(vec_max)