The dist() function in Python math module

The dist() function computes the Euclidean distance between two points of the same dimension. The Python example finds the Euclidean distance between two points in a two-dimensional plane.

Math Updated 13 Nov 2019 1 min read

Function Name

dist

Function Signature

dist(p, q)

Parameters

p – Point one.

q – Point Two.

The two points should be of the same dimensions.

Overview

  • The dist() function of Python math module finds the Euclidean distance between two points.
  • Euclidean distance between the two points is given by

Formula to find Euclidean distance between two points

Application of distance in clustering

Clustering is an unsupervised learning technique that groups data based on the distance between two data points. Euclidean distance is one of the distance metrics that is used in clustering techniques. The K-means clustering algorithm uses Euclidean distance to calculate the distance between each point to the centroid of the cluster. The dist module of scipy also has a method euclidean() that finds the Euclidean distance between two points.

Example

python
# Example Python program to find the Euclidean distance between two

# points

import math

# Define point1

point1 = (2, 2)

# Define point2

point2 = (4, 8)

# Find the Euclidean distance

euclidean_distance = math.dist(point1, point2)

# Print the Euclidean distance

print("The Euclidean distance between point1(%s) and point2(%s):"%(point1, point2))

print(euclidean_distance)

Output

Output
The Euclidean distance between point1((2, 2)) and point2((4, 8)):

6.324555320336759