Tutorials  /  Python

Norm of a Vector in Python - Steps for Calculation

Ccentron Redaktion · March 2024 ·5 min read ·Python, Tutorial

The norm of a vector refers to the length or the magnitude of a vector. There are different ways to calculate the length. The norm of a vector is a non-negative value. In this tutorial, we will learn how to calculate the different types of norms of a vector.

Norm of a vector x is denoted as: ‖x‖

The norm of a vector is a measure of its distance from the origin in the vector space.

To calculate the norm, you can either use Numpy or Scipy. Both offer a similar function to calculate the norm.

In this tutorial we will look at two types of norms that are most common in the field of machine learning.

  • L1 Norm
  • L2 Norm

How to Calculate the L1 Norm of a Vector?

L1 Norm of a vector is also known as the Manhattan distance or Taxicab norm. The notation for L1 norm of a vector x is ‖x‖1.

To calculate the norm, you need to take the sum of the absolute vector values.

Let’s take an example to understand this:

Code
a = [1,2,3,4,5]

For the array above, the L1 norm is going to be:

Code
1+2+3+4+5 = 15

Let’s take another example:

Code
a = [-1,-2,3,4,5]

The L1 norm of this array is :

Code
|-1|+|-2|+3+4+5 = 15

The L1 norm for both the vectors is the same as we consider absolute values while computing it.

Python Implementation of L1 Norm

Let’s see how can we calculate L1 norm of a vector in Python.

Using Numpy

The Python code for calculating L1 norm using Numpy is as follows :

Python
from numpy import array
from numpy.linalg import norm
arr = array([1, 2, 3, 4, 5])
print(arr)
norm_l1 = norm(arr, 1)
print(norm_l1)

Output :

Code
[1 2 3 4 5]
15.0

Let’s try calculating it for the array with negative entries in our example above.

Python
from numpy import array
from numpy.linalg import norm
arr = array([-1, -2, 3, 4, 5])
print(arr)
norm_l1 = norm(arr, 1)
print(norm_l1)

Output :
[-1 -2  3  4  5]
15.0

Using Scipy

To calculate L1 using Scipy is not very different from the implementation above.

The code for same is:

Python
from numpy import array
from scipy.linalg import norm
arr = array([-1, -2, 3, 4, 5])
print(arr)
norm_l1 = norm(arr, 1)
print(norm_l1)

Output :

Code
[-1 -2  3  4  5]
15.0

The code is exactly similar to the Numpy one.

VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

How to Calculate L2 Norm of a Vector?

The notation for the L2 norm of a vector x is ‖x‖2.

To calculate the L2 norm of a vector, take the square root of the sum of the squared vector values.

Another name for L2 norm of a vector is Euclidean distance. This is often used for calculating the error in machine learning models.

The Root Mean square error is the Euclidean distance between the actual output of the model and the expected output.

The goal of a machine learning model is to reduce this error.

Let’s consider an example to understand it.

Code
a = [1,2,3,4,5]

The L2 norm for the above is :

Code
sqrt(1^2 + 2^2 + 3^2 + 4^2 + 5^2) = 7.416

L2 norm is always a positive quantity since we are squaring the values before adding them.

Python Implementation

The Python implementation is as follows :

Python
from numpy import array
from numpy.linalg import norm
arr = array([1, 2, 3, 4, 5])
print(arr)
norm_l2 = norm(arr)
print(norm_l2)

Output :

Code
[1 2 3 4 5]
7.416198487095663

Here we can see that by default the norm method returns the L2 norm.

Performance Considerations

When calculating vector norms in Python, both NumPy and SciPy provide efficient methods. However, there are performance differences between the two, which can be significant for large datasets.

Performance Comparison: NumPy vs. SciPy

The numpy.linalg.norm function is generally faster than scipy.linalg.norm. This is because scipy.linalg.norm includes additional checks, such as using np.asarray_chkfinite, which ensures that the input array does not contain infinite (Inf) or NaN values. These checks add an extra layer of safety but also increase the computation time.

Practical Recommendations

  • Use NumPy for Maximum Performance:

    If your input data is guaranteed to be free of Inf or NaN values and performance is critical, prefer numpy.linalg.norm for faster computation.

  • Use SciPy for Additional Safety Checks:

    If there is any uncertainty about the quality of your input data or you need extra validation, scipy.linalg.norm is a safer choice due to its built-in checks—albeit at the expense of speed.

Conclusion

This tutorial was about calculating L1 and L2 norms in Python. We used Numpy and Scipy to calculate the two norms. Hope you had fun learning with us! Norm of a Vector in Python – Steps for Calculation

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren