Home » Statistics » How to Calculate Hamming Distance in Python (With Examples)

How to Calculate Hamming Distance in Python (With Examples)

The Hamming distance between two vectors is the number of positions at which the corresponding bits are different.

For example, assume we have two code:

sent code: 1100101
received code: 1101111

Let’s compare these two codes and we have observed that at two locations they are different. So, the hamming distance between these two codes is d(1100101,1101111) is 2. We will calculate Hamming Distance in python with steps by steps instructions and examples.


Scipy for Hamming Distance

We will be using scipy library available in python to calculate hamming distance.

If you don’t have scipy library installed then use the below command on windows command prompt for scipy library installation.

pip install scipy

How to Calculate Hamming Distance?

scipy library provide scipy.spatial.distance.hamming() function to calculate hamming distance.

scipy.spatial.distance.hamming(u,v,w=None)

where Parameters : 

u : input array.

v : input array.

w : It is the weight array for each value in u and v array.By default its value is None,which gives each value a weight of 1.

Returns: Hamming distance between u and v array.

Note: 1. The above function returns the hamming distance in terms of percentage. We will multiple by length of one of the array in order to obtain actual distance.

2. Hamming Distance is only defined if the two vectors are of same length.

Cool Tip: Learn How to plot Euclidean Distance in python !

How to calculate Hamming distance between two Integer Vectors

Let’s assume we have below vectors:

a = [3,2,5,4,8]
b = [3,1,4,4,4]

Hamming distance can be calculated using the below python code.

# import modules
from scipy.spatial.distance import hamming

# define vectors
a = [3,2,5,4,8]
b = [3,1,4,4,4]

#calculate Hamming distance between the two vectors
result = hamming(a,b) * len(a)

#print the Hamming distance between the two vectors
print('Hamming distance between a & b vectors:',result)

In the above code, import scipy package to calculates the hamming distance.

scipy.spatial.distance.hamming() accepts the a and b array as input parameters and returns hamming distance percentage and after multiplying it with a length of the array we get the actual distance.

The output of the above hamming distance python code is shown below:

#Output
Hamming distance between a & b vectors: 3.0

Cool Tip: How to calculate Euclidean Distance in python !

How to calculate Hamming distance between two String arrays

Lets assume we have below strings:

a = [‘one’,’three’,’four’,’six’,’seven’]
b = [‘one’,’two’,’five’,’six’,’seven’]

Hamming distance can be calculated using the below python code.

# import modules
from scipy.spatial.distance import hamming

# define string arrays
a = ['one','three','four','six','seven']
b = ['one','two','five','six','seven']

#calculate Hamming distance between the two vectors
result = hamming(a,b) * len(a)

#print the Hamming distance between the two vectors
print('Hamming distance between a & b vectors:',result)

In the above code, import scipy package then calculates the hamming distance.

scipy.spatial.distance.hamming() accepts the a and b array as input parameters and returns hamming distance percentage and after multiplying it with a length of the array we get the actual distance.

The output of the above hamming distance python code is shown below:

#Output
Hamming distance between a & b vectors: 2.0

How to calculate Hamming distance between two Binary arrays

Lets assume we have below binary arrays:

a = [1,1,0,1,0,1,0,1,0,0]
b = [1,0,0,0,0,1,0,0,1,1]

Hamming distance can be calculated using the below python code.

# import modules
from scipy.spatial.distance import hamming

# define binary arrays
a = [1,1,0,1,0,1,0,1,0,0]
b = [1,0,0,0,0,1,0,0,1,1]

#calculate Hamming distance between the two binary arrays
result = hamming(a,b) * len(a)

#print the Hamming distance between the two binary arrays
print('Hamming distance between a & b binary arrays:',result)

In the above code, import scipy package to calculates the hamming distance.

scipy.spatial.distance.hamming() accepts the a and b array as input parameters and returns hamming distance percentage and after multiplying it with a length of the array we get the actual distance.

The output of the above hamming distance python code is shown below:

#Output
Hamming distance between a & b binary arrays: 5.0

How to calculate Hamming distance between two Numerical arrays

Let’s assume we have below arrays:

a = [11,22,13,14,25,20]
b = [11,22,13,24,25,26]

Hamming distance can be calculated using the below python code.

# import modules
from scipy.spatial.distance import hamming

# define arrays
a = [11,22,13,14,25,20]
b = [11,22,13,24,25,26]

#calculate Hamming distance between the two arrays
result = hamming(a,b) * len(a)

#print the Hamming distance between the two arrays
print('Hamming distance between a & b arrays:',result)

In the above code, import scipy package to calculates the hamming distance.

scipy.spatial.distance.hamming() accepts the a and b array as input parameters and returns hamming distance percentage and after multiplying it with a length of the array we get the actual distance.

The output of the above hamming distance python code is shown below:

#Output
Hamming distance between a & b arrays: 2.0

Conclusion

I hope you find the above article on how to calculate Hamming Distance in python code useful and educational.