. Advertisement .
..3..
. Advertisement .
..4..
In geometry, the Euclidean distance is the length of the line segment that connects two points. Finding this distance is key to many applications of general and advanced mathematics.
This tutorial will show how to calculate Euclidean distance in Python. You can implement different libraries – all of which apply the Pythagorean theorem to do the job.
How To Calculate Euclidean Distance In Python
Using The math Module
The simplest way to carry out this operation is to use the built-in math module. It comes with the Python Standard Library. It has official support from Python, and you won’t need to install additional libraries.
Since the 3.8 version, Python’s math module has a function called dist(). It returns the Euclidean distance between two points when you give it two lists or tuples containing their Cartesian coordinates.
Example:
import math
a = [12, 2]
b = [6, 10]
distance = math.dist(a, b)
print(distance)
Output:
10.0
Using The NumPy Library
NumPy is a mathematical Python library written with large, multi-dimensional matrices and arrays in mind. Among its huge collection of high-level operations are several functions that can be used for finding Euclidean distances.
Keep in mind that you will have to numpy.array – the library’s main data type – instead of regular Python lists.
Basic Functions You can use a combination of square(), sum(), and sqrt() functions in NumPy to calculate Euclidean distances in a straightforward manner.
Example:
import numpy as np
a = np.array((12, 2))
b = np.array((6, 10))
distance = np.sum(np.square(a-b))
print(np.sqrt(distance))
Output:
10.0
dot() numpy.dot() can return the dot product of two vectors. You can make use of it to find the sum of the squares before taking its square root. Here is an example that uses that approach:
import numpy as np
a = np.array((12, 2))
b = np.array((6, 10))
c = a – b
distance = np.dot(c, c)
print(np.sqrt(distance))
Output:
10.0
linalg.norm() The linalg.norm() function in NumPy can produce the norm of both vectors and matrices. When fed with the vector connecting the two points, it should give you the length of that vector. This is what you are looking for.
Example:
import numpy as np
a = np.array((12, 2))
b = np.array((6, 10))
distance = np.linalg.norm(a – b)
print(distance)
Output:
10.0
Using The SciPy Library
SciPy is a free scientific computation library with many advanced functions for signal processing, statistics, and optimization, among other others.
It uses NumPy under the hood and includes many sub-packages. One of them (spatial) has a dedicated function (distance.euclidean) you can use to calculate Euclidean distance in Python.
Thanks to this, everything is fairly simple:
from scipy.spatial import distance
a = [12, 2]
b = [6, 10]
distance = distance.euclidean(a, b)
print(distance)
Output:
10.0
Conclusion
There are plenty of ways to calculate Euclidean distance in Python. From built-in functions to third-party libraries, you are spoilt for choice. Take factors like simplicity and extensibility into account, and you should be able to pick the most suitable choice.
Leave a comment