Free cookie consent management tool by TermsFeed Finding the Euclidean distance between points using SciPy | Pythontic.com

Finding the Euclidean distance between points using SciPy

Overview:

  • The Euclidean distance between two points in n-dimensional space is given by the formula, Distance = sqrt(sum(xi-yi)2), where i varies between 1 to n.

  • The SciPy function euclidean() from the module scipy.spatial.distance accepts two points in n-dimensional space as one-dimensional arrays and returns the Euclidean distance between them.

Example:

The SciPy example assumes the height of a person as X axis and weight of the person as Y axis finding the Euclidean distance between two points. Such calculations that abstract features of persons or entities as two dimensional points and finding distances are common in algorithms like clustering.

# Example Python program that finds the Euclidean
# distance between two points on an Euclidean plane
# (i.e.,Two dimensional plane) 
import scipy.spatial.distance as dist

# Height and weight of two persons
person1 = (70, 150)
person2 = (75, 155)

# Find the Euclidean distance between the points
dist = dist.euclidean(person1, person2)
print("Euclidean distance:")
print(dist)

Overview:

Euclidean distance:
7.0710678118654755

 


Copyright 2025 © pythontic.com