Connecting the Dots: Understanding Point Distances with Python

Shubham Sangole
CodeX
Published in
4 min readMay 16, 2024
credits: https://www.thetechedvocate.org/

Understanding the distance between two points is fundamental in various fields such as mathematics, physics, engineering, computer science, and even daily life scenarios like navigation. This blog post dives deep into the concept, exploring mathematical formulas and practical implementation in Python.

What is the Distance Between Two Points?

In geometry, the distance between two points is the length of the shortest path connecting them. This concept is not only intuitive but also critical for more complex calculations in various domains, including computer graphics, data science, and geographical information systems.

Mathematical Formulations

Euclidean Distance

The most commonly used distance metric is the Euclidean distance, named after the ancient Greek mathematician Euclid. It represents the straight-line distance between two points in Euclidean space.

For two points 𝑃(𝑥1,𝑦1) and 𝑄(𝑥2,𝑦2) in a 2-dimensional plane, the Euclidean distance 𝑑 is calculated using the Pythagorean theorem:

In a 3-dimensional space, for points 𝑃(𝑥1,𝑦1,𝑧1) and 𝑄(𝑥2,𝑦2,𝑧2), the distance is:

Manhattan Distance

Also known as the L1 distance or taxicab distance, the Manhattan distance measures the sum of the absolute differences of their coordinates. This metric is particularly useful in grid-like pathfinding algorithms.

For 2-dimensional points 𝑃(𝑥1,𝑦1) and 𝑄(𝑥2,𝑦2):

In 3-dimensional space, it extends to:

Minkowski Distance

The Minkowski distance is a generalized metric that includes both the Euclidean and Manhattan distances as special cases. It is defined for a real number 𝑝≥1 and for points 𝑃(𝑥1,𝑦1,…,𝑥𝑛) and 𝑄(𝑥2,𝑦2,…,𝑥𝑛):

  • For 𝑝=1, it becomes the Manhattan distance.
  • For 𝑝=2, it becomes the Euclidean distance.

Hamming Distance

The Hamming distance is used for comparing two strings of equal length. It measures the number of positions at which the corresponding symbols are different. This distance is widely used in error detection and correction algorithms.

For two binary strings 𝑃 and 𝑄:

where 𝑃𝑖≠𝑄𝑖 is 1 if 𝑃𝑖​ and 𝑄𝑖 are different, and 0 otherwise.

Practical Implementation in Python

Let’s implement these distance measures in Python. We will use functions to calculate each type of distance.

Euclidean Distance

import math

def euclidean_distance(point1, point2):
return math.sqrt(sum((x - y) ** 2 for x, y in zip(point1, point2)))

# Example usage
point1 = (1, 2)
point2 = (4, 6)

print("Euclidean Distance:", euclidean_distance(point1, point2))

Manhattan Distance

def manhattan_distance(point1, point2):
return sum(abs(x - y) for x, y in zip(point1, point2))

# Example usage
point1 = (1, 2)
point2 = (4, 6)

print("Manhattan Distance:", manhattan_distance(point1, point2))

Minkowski Distance

def minkowski_distance(point1, point2, p):
return sum(abs(x - y) ** p for x, y in zip(point1, point2)) ** (1 / p)

# Example usage
point1 = (1, 2)
point2 = (4, 6)
p = 3

print(f"Minkowski Distance with p={p}:", minkowski_distance(point1, point2, p))

Hamming Distance

def hamming_distance(str1, str2):
if len(str1) != len(str2):
raise ValueError("Strings must be of the same length")
return sum(ch1 != ch2 for ch1, ch2 in zip(str1, str2))

# Example usage
str1 = "1011101"
str2 = "1001001"

print("Hamming Distance:", hamming_distance(str1, str2))

Applications of Distance Metrics

Machine Learning

Distance metrics are crucial in machine learning algorithms, particularly in clustering (e.g., K-means) and classification (e.g., K-nearest neighbours) methods. The choice of distance metric can significantly impact the performance and outcome of these algorithms.

Image Processing

In image processing, Euclidean and Manhattan distances are used to compare pixel values, detect edges, and measure similarity between images.

Natural Language Processing

Hamming distance is employed in natural language processing for spell-checking, DNA sequence analysis, and error detection in data transmission.

Geographic Information Systems

In GIS, distance metrics help in calculating the shortest path between locations, determining proximity, and performing spatial analysis.

Conclusion

Understanding and implementing the distance between two points is a fundamental skill that spans numerous fields and applications. By mastering various distance metrics and their Python implementations, you can tackle a wide array of practical problems effectively. Whether you are working on machine learning, image processing, or geographical analysis, the knowledge of distance metrics is an invaluable tool in your computational arsenal.

--

--

Shubham Sangole
CodeX
Writer for

Data-Muncher | On a Data Science voyage to explore new learnings and exciting possibilities.