Use numpy.linalg.norm:

dist = numpy.linalg.norm(a-b)

This works because the Euclidean distance is the l2 norm, and the default value of the ord parameter in numpy.linalg.norm is 2. For more theory, see Introduction to Data Mining:

Answer from u0b34a0f6ae on Stack Overflow
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ how to compute l1 and l2 norms in python?
How to compute L1 and L2 norms in python? - AskPython
February 27, 2023 - 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?
๐ŸŒ
Medium
koshurai.medium.com โ€บ demystifying-l1-norm-and-l2-norm-in-python-your-guide-to-understanding-and-implementing-6390ee0ae8fe
Demystifying L1 Norm and L2 Norm in Python: Your Guide to Understanding and Implementing | by KoshurAI | Medium
February 25, 2024 - On the other hand, the L2 norm, also known as the Euclidean norm, calculates the square root of the sum of the squared values of the vector components. Think of it as the straight-line distance between two points in Euclidean space โ€” the kind ...
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ third-party โ€บ numpy โ€บ linalg โ€บ norm
Python Numpy linalg norm() - Calculate Vector Norm | Vultr Docs
November 18, 2024 - Define a vector for which the norm is to be calculated. Use the norm() function to compute the Euclidean norm. ... This code snippet calculates the Euclidean norm (also known as L2 norm) for the vector [3, 4]. The result is 5.0, which is the ...
๐ŸŒ
KDnuggets
kdnuggets.com โ€บ 2023 โ€บ 05 โ€บ vector-matrix-norms-numpy-linalg-norm.html
Vector and Matrix Norms with NumPy Linalg Norm - KDnuggets
This function takes in a required parameter โ€“ the vector or matrix for which we need to compute the norm. In addition, it takes in the following optional parameters: ord that decides the order of the norm computed, and ยท axis that specifies the axis along which the norm is to be computed. When we donโ€™t specify the ord in the function call, the norm() function computes the L2 norm by default:
๐ŸŒ
Medium
medium.com โ€บ @nigelgebodh โ€บ a-guide-to-vector-norms-in-machine-learning-with-python-35000796da9c
A Guide to Vector Norms in Machine Learning with Python | by Nigel Gebodh | Medium
August 8, 2024 - In python we get: import numpy as np from numpy.linalg import norm x = np.array([3,4]) norm_l2 = np.sqrt(np.sum(x**2)) # norm_l2 : 5 norm_l2 = norm(x, 2) # norm_l2 : 5 ยท Example 2: Given two vectors, compute their Euclidean distance. ๐ฎ = [1, 2, 3], ๐ฏ = [4, 5, 6] Steps: Compute the error/difference between ๐ฎ and ๐ฏ ยท
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ generated โ€บ numpy.linalg.norm.html
numpy.linalg.norm โ€” NumPy v2.5.dev0 Manual
If axis is an integer, it specifies the axis of x along which to compute the vector norms. If axis is a 2-tuple, it specifies the axes that hold 2-D matrices, and the matrix norms of these matrices are computed. If axis is None then either a vector norm (when x is 1-D) or a matrix norm (when ...
Find elsewhere
๐ŸŒ
Centron
centron.de โ€บ startseite โ€บ norm of a vector in python โ€“ steps for calculation
Norm of a Vector in Python - Steps for Calculation
February 7, 2025 - Here we can see that by default the norm method returns the L2 norm. 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.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.linalg.norm.html
numpy.linalg.norm โ€” NumPy v2.4 Manual
If axis is an integer, it specifies the axis of x along which to compute the vector norms. If axis is a 2-tuple, it specifies the axes that hold 2-D matrices, and the matrix norms of these matrices are computed. If axis is None then either a vector norm (when x is 1-D) or a matrix norm (when ...
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ calculating-euclidean-distance-with-numpy
Calculating Euclidean Distance with NumPy
October 17, 2023 - For instance, the L1 norm of a vector is the Manhattan distance! With that in mind, we can use the np.linalg.norm() function to calculate the Euclidean distance easily, and much more cleanly than using other functions: distance = np.linalg.norm(point_1-point_2) print(distance) This results in the L2/Euclidean distance being printed:
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ numpy โ€บ linalg-norm
NumPy linalg.norm()
This example computes the L2 norm (Euclidean norm) of a 2D vector, which results in 5.0, representing its magnitude.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ calculate-the-euclidean-distance-using-numpy
Calculate the Euclidean distance using NumPy - GeeksforGeeks
July 15, 2025 - np.linalg.norm() function computes the norm (or magnitude) of a vector, which in the case of the difference between two points, gives us the Euclidean distance.
๐ŸŒ
CodingNomads
codingnomads.com โ€บ what-is-l2-norm
What is L2 Norm?
The x's you see in the above equation are vectors of points. Which means the L2-norm generalizes to multi-dimensions. This can feel confusing at first because you are so used to measuring distance in 2-dimensions. You naturally see distance as the space between two points, and you always measure ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 70070016 โ€บ best-way-to-calculate-l2-norm-between-two-2d-arrays-in-numpy
python 3.x - Best way to calculate l2 norm between two 2d arrays in numpy - Stack Overflow
I am trying to come up with a fast ... rows of two 2d numpy arrays. So first 2d numpy array is 7000 x 100 and second 2d numpy array is 4000 x 100. I want to get a matrix of 4000 x 7000, where each (i, j) entry is a l2 norm between ith row of second 2d numpy array and jth row of first 2d numpy array. I am assuming I probably have to use numpy.linalg.norm, but am not quite sure on how to vectorize the ...
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ l2-norm-in-python
L2 norm in Python
We used the np.power to square the differences between the elements of two arrays. We use np.sum to sum the square resulting values. Line 10: Finally, we take the square root of the l2_norm using np.sqrt this value shows the difference between the predicted values and actual value.
๐ŸŒ
MachineLearningMastery
machinelearningmastery.com โ€บ home โ€บ blog โ€บ gentle introduction to vector norms in machine learning
Gentle Introduction to Vector Norms in Machine Learning - MachineLearningMastery.com
October 17, 2021 - While writing about the L1 norm, this line doesnโ€™t seem necessary โ€œThe L2 norm of a vector can be calculated in NumPy using the norm() function with a parameter to specify the norm order, in this case 1.โ€ ยท Also, even though, not something ...
๐ŸŒ
APXML
apxml.com โ€บ courses โ€บ linear-algebra-fundamentals-machine-learning โ€บ chapter-2-vector-operations โ€บ vector-norms-l1-l2
Vector Norms: L1 and L2 Norms
As you would expect, NumPy provides a simple and efficient way to calculate vector norms using the numpy.linalg.norm() function. This function calculates the L2 norm by default, but you can specify other norms using the ord parameter.