How to Compute Distance in Python?

Let us see the following ways to calculate the distance between two points or vectors/arrays:

  • Hamming Distance
  • Euclidean Distance
  • Manhattan Distance

Hamming Distance:

The Hamming Distance is determined between two numbers, but they are in binary format. It basically denotes the number of binary bits that differ between two integers.

Consider the following situation: we have two integers. We must determine their Hamming distance. The hamming distance is the difference in bit count between two numbers. So, if the integers are 7 and 15, they are 0111 and 1111 in binary, respectively. Because the MSB is different, the Hamming distance is 1.

Approach:

  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Calculate the XOR(^) value of both the given two numbers and store it in another variable.
  • Take a variable and initialize its value to 0.
  • Loop until the above XOR result value is greater than 0 using the while loop.
  • Perform the AND(&) operation for the above XOR result and 1, and add the result to the above-initialized variable ham_dist.
  • Store it in the same variable.
  • Perform Right shift operation by 1 for the above XOR result value.
  • Print the Hamming Distance for the given two numbers.
  • The Exit of the Program.

Below is the implementation:

# Give the first number as static input and store it in a variable.
gvn_num1 = 6
# Give the second number as static input and store it in another variable.
gvn_num2 = 10
# Calculate the XOR(^) value of both the given two numbers and
# store it in another variable.
xor_rslt = gvn_num1 ^ gvn_num2
# Take a variable and initialize its value to 0
ham_dist = 0
# Loop until the above XOR result value is greater than 0 using the while loop
while (xor_rslt > 0):
    # Perform the AND(&) operation for the above XOR result and 1, and
    # add the result to the above initialized variable ham_dist
    # store it in the same variable.
    ham_dist += xor_rslt & 1
    # Perform Right shift operation by 1 for the above XOR result value
    xor_rslt >>= 1
# Print the Hamming Distance for the given two numbers
print("The Hamming Distance for the given two numbers = ", ham_dist)

Output:

The Hamming Distance for the given two numbers =  2

Euclidean Distance

The length of a segment connecting two points in two-dimensional or three-dimensional space is calculated using the Euclidean distance between the two points. It is also referred to as simply representing the distance between two points.

Formula  to Compute Euclidean distance :

Let A(x1,y1) ,B(x2, y2) are the two points in 2-dimensional plane

Euclidean distance = √[ (x – x)2 + (y – y)2]

Similarly, you can calculate for n points.

Example

Approach:

  • Import math module using the import keyword.
  • Give the first point values as static input say P(x1,y1), and Store it in a variable.
  • Give the second point values as static input say Q(x2,y2), and Store it in another variable.
  • Calculate the Euclidean distance between the given two points with reference to the mathematical formula using built-in math.sqrt() function.
  • And Store it in another variable.
  • Print the Euclidean distance between the above given two points ‘PQ’.
  • The Exit of the program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the first point values as static input say P(x1,y1) and Store it in a variable.
x_1, y_1 = 4, 6
# Give the second point values as static input say Q(x2,y2) and Store it in another 
#variable.
x_2, y_2 = 2, 1
# Calculate the Euclidean distance between given two points with reference to the
# mathematical formula using built-in math.sqrt() function.
# And Store it in another variable.
Eucli_dist = math.sqrt((x_2-x_1)**2 + (y_2-y_1)**2)
# Print the Euclidean distance between the above given two points 'PQ'.
print("The Euclidean Distance between the above given two points 'PQ' = " + str(Eucli_dist))

Output:

The Euclidean Distance between the above given two points 'PQ' = 5.385164807134504

Manhattan Distance

The Manhattan distance between two vectors/arrays (say P and Q), is calculated as Σ|Pi – Qi| where Pi is the ith element in the first array and Qi is the ith element in the second array.

Approach:

  • Give the first list as static input and store it in a variable.
  • Give the second list as static input and store it in another variable.
  • Take a variable and initialize its value to 0.
  • Calculate the length of the given first list P using the len() function and store it in another variable.
  • Loop Until the length of the list P using the for loop.
  • Calculate the Manhattan distance using the above given mathematical formula and get the absolute value of it using the abs() function.
  • Add the result to the above initialized variable Manht_dist and store it in the same variable.
  • Print the Manhattan Distance for the given two numbers.
  • The Exit of the program.

Below is the implementation:

# Give the first list as static input and store it in a variable.
P = [3, 4, 7]
# Give the second list as static input and store it in another variable.
Q = [6, 5, 1]
# Take a variable and initialize its value to 0
Manht_dist = 0
# Calculate the length of the given first list P using the len() function and
# store it in another variable.
p_lengt = len(P)
# Loop Until the length of the list P using the for loop
for itr in range(p_lengt):
    # Calculate the Manhattan distance using the above given mathematical formula
    # and get the absolute value of it using the abs() function 
    # Add the result to the above initialized variable Manht_dist and
    # store it in the same variable
    Manht_dist += abs(P[itr] - Q[itr])
# Print the Manhattan Distance for the given two numbers
print("The Manhattan Distance for the given two numbers = ", Manht_dist)

Output:

The Manhattan Distance for the given two numbers =  10