How to Calculate Euclidean distance in Python

To calculate the euclidean distance in Python, use the math.dist() function. The math.dist() is a built-in method that returns the Euclidean distance between two points (x and y), where x and y are the coordinates of that point.

Euclidean distance is the widely used technique for finding the distance between two points. This formula can calculate the distance between two points with n coordinates.

Syntax

math.dist(x, y)

Arguments

x Required. Specifies point 1
y Required. Specifies point 2

Example

import math

x = (2, 3)
y = (3, 5)
distance = math.dist(x, y)
print(distance)

Output

2.23606797749979

This math module has a function called dist. This dist() function is used for calculating the distance between two points. In addition, the dist() function performs the Euclidian distance calculation.

Euclidian Distance Formula:

Euclidian Distance Formula

This formula can be used to find the distance between two points. This is the basic Euclidian formula.

Python Program for calculating Euclidean distance

import math

x = input()
y = input()
x1, y1 = x.split(',')
x2, y2 = y.split(',')
x1 = int(x1)
x2 = int(x2)
y1 = int(y1)
y2 = int(y2)
distance = math.sqrt(((x2 - x1) ** 2) + (y2 - y1) ** 2)
print("The Euclidean distance between the points using formula is : ", str(distance))

Output

2,3
3,5
The Euclidean distance between the points using formula is : 2.23606797749979

We imported the math function to perform the square root operation in this program. We have got the input coordinates, and we have found the distance between two points by applying the values to the Euclidian distance formula.

Using np.linalg.norm() function

To calculate Euclidean distance using the numpy library, use the np.linalg.norm() function. The np.linalg.norm() function is used to calculate one of the eight different matrix norms or one of the vector norms.

import numpy as np

x = np.array((2, 3))
y = np.array((3, 5))
distance = np.linalg.norm(x - y)
print(distance)

Output

2.23606797749979

In this program, we used the numpy package. We created two numpy arrays. Then we have passed these numpy arrays as an argument to the np.linalg.norm() function. The norm() function is present inside the module called the linalg. This norm function is used to calculate the Euclidian distance between two points.

Calculating Euclidean distance using np.dot() function

The np.dot() function calculates the dot product of two arrays. It can handle 2D arrays but considers them as matrix and will perform matrix multiplication.

import numpy as np

x = np.array((2, 3))
y = np.array((3, 5))
d = x - y
distance = np.dot(d.T, d)
distance = np.sqrt(distance)
print(distance)

Output

2.23606797749979

In this program, we found the difference between two arrays and we have stored it in d. After finding the distance between two arrays we have made a transpose.

Then, we multiply the transpose and the normal array d using the np.dot() function. Then, we found the np.sqrt() of the distance found from the dot product. That is the Euclidian distance.

That’s it for this tutorial.

Related posts

How to Perform Matrix Multiplication in Python

Python sigmoid

Leave a Comment