-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculation.py
33 lines (26 loc) · 867 Bytes
/
Calculation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from math import sqrt, pow
class Calculation():
@staticmethod
def calculatedistance(metric, x, y):
_metric = metric.lower()
if _metric == "hamming":
return hamming_distance(x, y)
elif _metric == "euclidean":
return euclidean_distance(x, y)
else:
return euclidean_distance(x, y)
# Euclidean distance between two vectors
def euclidean_distance(vec1, vec2):
distance = 0.0
len_vec_1 = len(vec1)
for i in range(len_vec_1):
distance += pow((vec1[i] - vec2[i]), 2)
euclidean_value = sqrt(distance)
return euclidean_value
# hamming distance between two string for binary input
def hamming_distance(string1, string2):
dist_counter = 0
for n in range(len(string1)):
if string1[n] != string2[n]:
dist_counter += 1
return dist_counter