Member-only story
Euclidean Distance in Python
Aug 29, 2024
import math
def euclidean_distance(point1, point2):
return math.sqrt((point1[0] - point2[0])**2 + (point1[1] - point2[1])**2)
point1 = (1, 2)
point2 = (4, 6)
distance = euclidean_distance(point1, point2)
print(f"The Euclidean distance between {point1} and {point2} is {distance:.2f}")
The Euclidean distance between (1, 2) and (4, 6) is 5.00
Euclidean Distance Using NumPy
import numpy as np
def euclidean_distance_numpy(point1, point2):
return np.linalg.norm(np.array(point1) - np.array(point2))
# Example usage
point1 = (1, 2)
point2 = (4, 6)
distance = euclidean_distance_numpy(point1, point2)
print(f"The Euclidean distance (NumPy) between {point1} and {point2} is {distance:.2f}")
The Euclidean distance (NumPy) between (1, 2) and (4, 6) is 5.00