Calculating Euclidean distance in Python
In the world of mathematics, the shortest distance between two points in any dimension is called the Euclidean distance. It is the square root of the sum of the squares of the differences between the two points.
In Python, the numpy, scipy modules are equipped with functions to perform mathematical operations and calculate the line segment between two points.
In this tutorial, we will discuss different ways to calculate the Euclidean distance between coordinates.
Find the Euclidean distance between two points using NumPy module
When the coordinates are in the form of arrays, you can use the numpy module to find the required distance. It has norm()
a function that returns the vector norm of an array. It can help calculate the Euclidean distance between two coordinates as shown below.
import numpy as np
a = np.array((1, 2, 3))
b = np.array((4, 5, 6))
dist = np.linalg.norm(a - b)
print(dist)
Output:
5.196152422706632
We can also implement the mathematical formula directly using the numpy module. For this method, we will use numpy.sum()
the function, which returns the sum of the elements, and numpy.square()
the function, which returns the square of the elements.
import numpy as np
a = np.array((1, 2, 3))
b = np.array((4, 5, 6))
dist = np.sqrt(np.sum(np.square(a - b)))
print(dist)
Output:
5.196152422706632
numpy.sqrt()
The function returns the square root of a value.
Another way to implement the Euclidean distance formula is to use dot()
the function. We can find the dot product of the difference and its transpose, returning the sum of the squares.
For example,
import numpy as np
a = np.array((1, 2, 3))
b = np.array((4, 5, 6))
temp = a - b
dist = np.sqrt(np.dot(temp.T, temp))
print(dist)
Output:
5.196152422706632
Use distance.euclidean()
the function to find the Euclidean distance between two points
We discussed different ways to calculate Euclidean distance using the numpy module. However, these methods can be a bit slow, so there are faster alternatives.
The scipy library has many functions for mathematical and scientific computing. distance.euclidean()
The function returns the Euclidean distance between two points.
For example,
from scipy.spatial import distance
a = (1, 2, 3)
b = (4, 5, 6)
print(distance.euclidean(a, b))
Output:
5.196152422706632
Use math.dist()
the function to find the Euclidean distance between two points
math
The module can also be used as an alternative. The module's dist()
function can return a line segment between two points.
For example,
from math import dist
a = (1, 2, 3)
b = (4, 5, 6)
print(dist(a, b))
Output:
5.196152422706632
scipy
The and math
module methods are NumPy
faster alternatives to the methods and can be used when the coordinates are in the form of tuples or lists.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert Tensor to NumPy array in Python
Publish Date:2025/05/03 Views:85 Category:Python
-
This tutorial will show you how to convert a Tensor to a NumPy array in Python. Use the function in Python Tensor.numpy() to convert a tensor to a NumPy array Eager Execution of TensorFlow library can be used to convert tensor to NumPy arra
Saving NumPy arrays as images in Python
Publish Date:2025/05/03 Views:193 Category:Python
-
In Python, numpy module is used to manipulate arrays. There are many modules available in Python that allow us to read and store images. An image can be thought of as an array of different pixels stored at specific locations with correspond
Transposing a 1D array in NumPy
Publish Date:2025/05/03 Views:98 Category:Python
-
Arrays and matrices form the core of this Python library. The transpose of these arrays and matrices plays a vital role in certain topics such as machine learning. In NumPy, it is easy to calculate the transpose of an array or a matrix. Tra
Find the first index of an element in a NumPy array
Publish Date:2025/05/03 Views:58 Category:Python
-
In this tutorial, we will discuss how to find the first index of an element in a numpy array. Use where() the function to find the first index of an element in a NumPy array The function in the numpy module where() is used to return an arra
Remove Nan values from NumPy array
Publish Date:2025/05/03 Views:118 Category:Python
-
This article discusses some built-in NumPy functions that you can use to remove nan values. Remove Nan values using logical_not() and methods in NumPy isnan() logical_not() is used to apply logical NOT to the elements of an array. isn
Normalizing a vector in Python
Publish Date:2025/05/03 Views:51 Category:Python
-
A common concept in the field of machine learning is to normalize a vector or dataset before passing it to the algorithm. When we talk about normalizing a vector, we say that its vector magnitude is 1, being a unit vector. In this tutorial,
Converting a PIL image to a NumPy array
Publish Date:2025/05/03 Views:148 Category:Python
-
This tutorial will discuss methods to convert a PIL image to a 3-dimensional NumPy array in Python. numpy.array() Convert a PIL image to a NumPy array in Python using the PIL is used to perform various operations on images in Python. The Pi
How to Convert DataFrame Column to String in Pandas
Publish Date:2025/05/02 Views:162 Category:Python
-
We will look at methods for converting Pandas DataFrame columns to strings. Pandas Series.astype(str) Method DataFrame.apply() Methods operate on the elements in a column We will use the same DataFrame below in this article. import pandas a
How to count the frequency of values in a Pandas DataFrame
Publish Date:2025/05/02 Views:84 Category:Python
-
Sometimes, when you use DataFrame , you may want to count the number of times a value occurs in a column, or in other words, calculate the frequency. There are mainly three methods used for this. Let's look at them one by one. df.groupby().