python-scipyHow can I use Python and SciPy to calculate Hamming distances?
To calculate Hamming distances in Python and SciPy, the scipy.spatial.distance.hamming
function can be used. This function takes two vectors as input and returns the Hamming distance between them as output.
Example code
from scipy.spatial import distance
vector1 = [1, 0, 0]
vector2 = [1, 1, 0]
hamming_distance = distance.hamming(vector1, vector2)
print(hamming_distance)
Output example
0.3333333333333333
The code above consists of the following parts:
-
Importing the
distance
module from thescipy.spatial
package, which contains thehamming
function used to calculate the Hamming distance. -
Declaring two vectors,
vector1
andvector2
, which will be used as input for thehamming
function. -
Calling the
hamming
function, passing the two vectors as arguments, and assigning the result to thehamming_distance
variable. -
Printing the result of the
hamming_distance
variable.
Helpful links
- scipy.spatial.distance.hamming - Documentation for the
scipy.spatial.distance.hamming
function. - Hamming Distance - Wikipedia page on Hamming Distance.
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How to use Python, XML-RPC, and NumPy together?
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
- How can I check if a certain version of Python is compatible with SciPy?
- How do I use the NumPy transpose function in Python?
- How do I use the scipy ttest_ind function in Python?
- How do I convert a Python Numpy array to a list?
- How do I use Python and SciPy to create a tutorial PDF?
- How can I use Python and Numpy to parse XML data?
See more codes...