How to calculate hamming distance in Python?

By Shivang Yadav Last updated : November 21, 2023

Hamming Distance

Hamming distance is a measure of the difference between two vectors. It is defined as the number of positions at which the corresponding elements.

Calculation of Hamming Distance

Python provides a built-in method hamming() present in the scipy library. The function returns the percentage of corresponding elements that differ between the two given vector arrays.

Python program to calculate hamming distance

# Program to calculate Hamming distance
from scipy.spatial.distance import hamming

vecA = [1, 4, 6, 8, 9]
vecB = [3, 4, 6, 2, 7]

print("Vector A", vecA)
print("Vector B", vecB)

ham = hamming(vecA, vecB) * len(vecA)

print("Hamming Distance Between vectors", ham)

Output

The output of the above program is:

Vector A [1, 4, 6, 8, 9]
Vector B [3, 4, 6, 2, 7]
Hamming Distance Between vectors 3.0

Python SciPy Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.