In this post, we will learn how to compute Euclidean distance between two points in 2-dimension and also in 3-dimension using Numpy. Euclidean distance is one of the most commonly used distance measure and it is defined as the straight-line distance between two points in Euclidean space.
Euclidean distance is calculated as the square root of the sum of the squares of the differences between the coordinates of the two points. For example, in 2-dimension the distance between two points (x1,y1) and (x2,y2) is
Euclidean_distance_2d = sqrt((x1-x2)^2 + (y1-y2)^2)
We will use two approaches, both relying Numpy’s features, to compute Euclidean distance between points.
Let us load the Numpy module.
import numpy as np # find Numpy version np.__version__ 1.24.0
Compute Euclidean distance in Numpy
In the first approach, we will use the above Euclidean distance formula and compute the distance using Numpy functions np.square(), np.sum(), and np.sqrt().
Let us first define points in 2-dimensional space.
# Define the points point1 = np.array([1, 2]) point2 = np.array([4, 6]) print(point1) print(point2) [1,2] [4,6]
Here (x1,y1) is [1,2] and (x2,y2) is [4,6]. We can compute the square of the difference between points using np.square() as shown below.
square_diff = np.square(point1 - point2) square_diff array([ 9, 16])
It results us an array containing square differences. We can add all the square differences using np.sum()
# sum of the square differences sum_square_diff = np.sum(square_diff) sum_square_diff 25
Now by taking square root on the squared differences, we get the Eucledian distance. In our example, Eucledian distance betwee the data points in 2D is 5
# sum of the square differences euclian_dist_2d = np.sqrt(sum_square_diff) euclian_dist_2d 5.0
Euclidean Distance with linalg.norm
Another way to compute Eucledian distance between two points in Numpy is to use the linalg.norm function from the numpy.linalg module. This function takes an array of points and returns the Euclidean norm (or distance) of each point.
Let us use the same 2d points in the previous example and show how to use linalg.norm to compute the Euclidean distance between two points in 2D:
# Define the points point1 = np.array([1, 2]) point2 = np.array([4, 6]) # Calculate the Euclidean distance in 2D distance = np.linalg.norm(point1 - point2) print(distance) 5
We can see that we get the same Eucledian distance using. linalg.norm function as expectecd.
In the second example, we use linalg.norm to compute Euclidean distance between points in 3-dimension.
# Define the points point1 = np.arange(3) point2 = np.arange(7,10) print(point1) print(point2) [0 1 2] [7 8 9]
# Calculate the Euclidean distance in 3D distance_3d = np.linalg.norm(point1 - point2) print(distance_3d) 12.12435565298214
Computing Euclidean distance between multiple points
We can also use linalg.norm to compute the Euclidean distance between multiple sets of points.
For example, suppose we have two sets of points in 2-dimension, and we want to compute the distance between each pair of points.
# Define the points points1 = np.array([[1, 2], [3, 4], [5, 6]]) points2 = np.array([[4, 6], [2, 1], [7, 8]]) print(points1) print(points2)
We can use linalg.norm() function in Numpy to compute the distances
# Calculate the Euclidean distance between each pair of points distances = np.linalg.norm(points1 - points2, axis=1) print(distances) # Output: [5.0, 5.0, 5.0]