How to compute L1 and L2 norms in python?


Hello readers! In this tutorial, we will learn how to compute the various forms of vector norms. The length or magnitude of a vector is referred to as the norm. There are several methods for calculating the length. A vector’s norm is a non-negative number.

L1 Norm of a Vector

The L1 norm is also known as the Manhattan Distance or the Taxicab norm. It is the total of the magnitudes of the vectors in a space is the L1 Norm. It is denoted by ||x|| (where x is a vector) and it is the most straightforward approach to measuring the distance between vectors, as it is the total of the absolute differences of the vectors’ components. All vector components are weighted equally in this norm.

Refer the image below to visualize the L1 norm for vector x = (-7,5)

L1 Norm
L1 Norm

The L1 norm, as shown in the diagram, is the “distance” between the origin (0,0) and the destination (-7,5). ||x|| = |-7| + |5| = 12 is the L1 norm of x.

Implementing L1 norm in python

Let us now implement the L1 norm using NumPy library in python. We will start by importing np.linalg module and declare our vector = [-8, 6, 0, -15].

from numpy.linalg import norm

x = [-8, 6, 0, -15]
print("Example vector:", x)

#calculate l1 norm
l1 = norm(x ,1)
print("L1 norm =", l1)

We have imported the norm function from np.linalg module to calculate the norm of the vector. For the L1 norm we have passed an additional parameter 1 which indicates that the L1 norm is to be calculated, By default norm() calculates L2 norm of the vector if no additional parameters are given.


Example vector: [ -8   6   0 -15]
L1 norm = 29.0

The result contains the L1 norm of our vector |-8|+|6|+|0|+|-15| = 29.0

L2 Norm of a Vector

The L2 norm, also known as the Euclidean norm, some people also use term “2 norm“, It is a measure of the magnitude of a vector in a Euclidean space. It’s defined as the square root of the sum of the squares of the individual elements of the vector. It is represented as ||x||2. The L2 norm is widely used in many fields, such as machine learning, engineering, and physics, for various applications, including optimization, regularization, and normalization. Refer the image below to visualize the L2 norm for vector x = (7,5)

L2 Norm
L2 Norm

The L2 norm, as shown in the diagram, is the direct distance between the origin (0,0) and the destination (7,5). ||x||2 = sqrt(|7|2 + |5|2) = 8.60 is the L2 norm of x.

Implementing L2 norm in python

We will calculate the L2 norm for the same variable x using np.linalg but this time we will not provide any additional parameter to the function norm() while passing the vector.

from numpy.linalg import norm

x = [-8, 6, 0, -15]
print("Example vector:", x)
l2 = norm(x)
print("L2 norm =","%.2f" % l2)

We have used "%.2f" % l1 to round up the result to 2 decimal digits only.

Also read: 5 Ways to handle precision values in Python


Example vector: [ -8   6   0 -15]
L2 norm = 18.03

The output contains L2 norm of the vector x which is calculated as:

  • ||x||2 = sqrt(|-8|2 + |6|2+|0|2 + |-15|2)
  • ||x||2 = sqrt(64+36+0+225) = sqrt(325)
  • ||x||2 = 18.02

Note that the L2 norm always comes out be smaller or equal to the L1 norm


In this tutorial, we covered the basics of the L1 and L2 norms and the different terminologies associated with them. We also learned how to compute the norms using the numpy library in python. The np.linalg module in numpy provides several functions for linear algebra computations, including the computation of vector norms. By using the norm function in np.linalg, we can easily calculate the L1 or L2 norm of a given vector.

It is important to note that the choice of the norm to use depends on the specific application and the properties required for the solution. The L1 norm is often used in cases where we need a robust solution that is insensitive to outliers, while the L2 norm is often used when we want a solution that is smoother and more predictable.

Also read: How to Compute Distance in Python? [ Easy Step-By-Step Guide ]


Numpy linear algebra library :