HomeLinuxCalculation of Hamming Distance in Python

Calculation of Hamming Distance in Python


Hamming distance” is a crucial idea in coding that calculates the distinction between two binary strings. In different phrases, it’s the variety of positions the place the corresponding symbols differ. It’s broadly utilized in error detection and correction, DNA sequencing, and cryptography. On this Python weblog, we are going to talk about what Hamming distance is, and learn how to calculate it in Python utilizing quite a few examples.

How one can Calculate Python Hamming Distance?

Python offers the next methods to compute/decide the Hamming distance between two strings:

Technique 1: Calculate Hamming Distance in Python Utilizing the Constructed-in Perform “hamming()”

The built-in operate “hamming()” from the “scipy.spatial.distance” module is used to calculate the Hamming distance in Python. This operate accepts two arrays as a parameter and returns their Hamming distance.

Syntax

from scipy.spatial.distance import hamming
hamming_distance = hamming(u, v)

Instance
Let’s overview the next instance code:

from scipy.spatial.distance import hamming
value1 = [1, 0, 1, 0, 1]
value2 = [0, 1, 1, 0, 1]
hamming_distance = hamming(value1, value2)
print(hamming_distance)

Within the above code:

  • The “hamming” operate is imported from the “scipy.spatial.distance” module and the 2 arrays of binary knowledge are initialized, respectively.
  • After that, the “hamming()” operate takes the 2 arrays as its arguments and calculates the Hamming distance between the 2 handed arrays comprising binary values.

Output

Within the above output, the hamming distance between the 2 arrays of binary values has been calculated.

Word: Within the above instance, the “hamming()” operate returned the worth of “0.4”. This worth can’t be interpreted as a result of the worth yields the “proportion” of various values. To learn how many objects are completely different, we have to multiply the size of the array by the variety of objects.

Instance
The below-given instance explains the acknowledged idea:

from scipy.spatial.distance import hamming
value1 = [1, 0, 1, 0, 1]
value2 = [0, 1, 1, 0, 1]
hamming_distance = hamming(value1, value2)*len(value1)
print(hamming_distance)

On this code snippet, the hamming distance between these two arrays is calculated utilizing the “hamming()” operate, after which multiplied by the size of the previous array to get the full variety of completely different parts between them(arrays).

Output

The hamming distance has been calculated appropriately within the above consequence.

Word: The rationale for multiplying the consequence by the size of the enter vectors is to normalize the Hamming distance by the size of the vectors, which provides us a price between “0” and “1”.

Technique 2: Calculate Hamming Distance in Python Through Loops

One other strategy to calculate “Hamming” distance is by utilizing “loops”. We are able to examine the corresponding symbols within the two strings and depend the variety of variations.

Instance
The next code instance will provide you with a fast overview:

def hamming_distance(str1, str2):
    distance = 0
    for i in vary(len(str1)):
        if str1[i] != str2[i]:
            distance += 1
    return distance
value1 = “110101”
value2 = “101011”
print(hamming_distance(value1, value2))

Within the above code block:

  • The user-defined operate named “hamming_distance()” is outlined.
  • This operate accepts two strings as an argument and retrieves the “Hamming” distance between them.
  • The Hamming distance is calculated by evaluating every corresponding character in each the handed strings and counting the variety of positions the place they differ.
  • After defining the operate, the 2 strings containing binary values are initialized.
  • Lastly, the user-defined operate is invoked by giving the 2 strings as arguments.

Output

The hamming distance between two binary strings has been computed efficiently within the above code snippet.

Technique 3: Calculate Hamming Distance in Python Utilizing “Listing Comprehension”

We are able to additionally calculate “Hamming” distance utilizing “Listing Comprehension”. The tactic described right here is extra easy and stylish than loops.

Instance
Right here is the code:

value1 = “110101”
value2 = “101011”
print(sum([1 for i, j in zip(value1, value2) if i != j]))

In accordance with the above traces of code:

  • The 2 binary strings have been initialized in this system.
  • The “Listing Comprehension” method is utilized to check every pair of characters in each the strings and counts the variety of positions the place they differ.
  • The “zip()” operate is used to pair up the characters in each the strings on the similar place.
  • The situation “if i != j” checks if the 2 characters are completely different. In that case, the worth “1” is appended to the record.
  • Lastly, the “sum()” operate provides up all of the “1’s” within the record to investigate and provides us the full variety of positions the place the strings differ.

Output

The above consequence implies that the hamming distance has been calculated efficiently.

Conclusion

To calculate the “hamming distance” in Python, varied strategies comparable to utilizing the built-in operate “hamming()”, “loops”, or “Listing Comprehension” can be utilized. Hamming distance is an important idea in coding and has quite a few functions in numerous fields. This put up introduced varied methods to find out the Python hamming distance utilizing applicable examples.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments