What is the L2 norm of a vector?
The norm of a vector v is mathematically defined as follows:
When q=1, the vector norm is called the L1 norm or the Manhattan norm. So, the L1 norm of a vector is mathematically defined as follows:
And when q=2, the vector norm is called the L2 norm. So, the L2 norm of a vector is mathematically defined as follows:
This L2 norm of a vector is also called the Euclidian norm. Let’s look at an example. Let’s say v is a vector that has the following components:
So, the L2 norm of the vector v is given by:
How to calculate the L2 norm of a vector using Python?
We can use the following Python code to calculate the L2 norm of a vector using NumPy.
import numpy from numpy.linalg import norm v = numpy.array([1, 2, -3]) l2 = norm(v) print("Vector v: \n", v) print("L1 norm of the vector v: ", l2)
Here, v is a vector. We are using the norm() function from numpy.linalg to calculate the L2 norm of vector v. The output of the mentioned program will be:
Vector v: [ 1 2 -3] L1 norm of the vector v: 3.7416573867739413






0 Comments