-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimilarity_metrics.py
61 lines (48 loc) · 1.19 KB
/
similarity_metrics.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import math
import numpy as np
import scipy.spatial
def l1_norm(x1, x2):
return np.linalg.norm(
np.subtract(x1, x2),
ord=1
)
def l2_norm2(x1, x2):
return np.linalg.norm(
np.subtract(x1, x2),
ord=2
)
def l2_norm1(x1):
return np.linalg.norm(
x1,
ord=2
)
# Something buggy in this code!!!
def mahalanobis(x1, x2):
return scipy.spatial.distance.mahalanobis(
x1.ravel(), x2.ravel(), np.cov(x1.ravel(), x2.ravel())
)
def correlation_coeff(x1, x2):
x1=x1.ravel()
x2=x2.ravel()
numerator = np.mean((x1 - x1.mean()) * (x2 - x2.mean()))
denominator = x1.std() * x2.std()
if denominator == 0:
return 0
else:
result = numerator / denominator
return result
def intersection(x1, x2):
result = 0
for x, y in zip(x1, x2):
if max(x, y) == 0:
continue
result += min(x, y) / max(x, y)
return result
def cosine(x1, x2):
return 1 - scipy.spatial.distance.cosine(x1.ravel(), x2.ravel())
def linf_norm(x1, x2):
norm = np.linalg.norm(
np.subtract(x1, x2),
ord=np.inf
)
return norm/(10 ** math.ceil(math.log10(abs(norm))))