Created
February 24, 2019 08:30
-
-
Save evdokimovm/5849b8f7c0afdc9440d1615596909bc4 to your computer and use it in GitHub Desktop.
distance formulas euclidean manhattan hamming
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def euclidean_distance(pt1, pt2): | |
distance = 0 | |
for i in range(len(pt1)): | |
distance += (pt1[i] - pt2[i]) ** 2 | |
return distance ** 0.5 | |
def manhattan_distance(pt1, pt2): | |
distance = 0 | |
for i in range(len(pt1)): | |
distance += abs(pt1[i] - pt2[i]) | |
return distance | |
def hamming_distance(pt1, pt2): | |
distance = 0 | |
for i in range(len(pt1)): | |
if pt1[i] != pt2[i]: | |
distance += 1 | |
return distance | |
print(hamming_distance([1, 2], [1, 100])) | |
print(hamming_distance([5, 4, 9], [1, 7, 9])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment