-
Notifications
You must be signed in to change notification settings - Fork 1
/
kde_tools.py
104 lines (82 loc) · 3.01 KB
/
kde_tools.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import math
import numpy as np
from scipy.special import ndtr
from sklearn.preprocessing import MinMaxScaler
def P_L2(query, data, n, bandwidth):
real_kde = []
for q in query:
val = 0
for xi in data:
l2_norm = np.linalg.norm(q - xi)
try:
np.seterr(divide='ignore')
p = 1 - 2 * ndtr(-bandwidth / l2_norm) - 2.0 / (np.sqrt(2 * math.pi) * (bandwidth / l2_norm)) * (
1 - np.exp(-0.5 * (bandwidth ** 2) / (l2_norm ** 2)))
except:
p = 1
val += p
real_kde.append(val / n)
return real_kde
def P_L1(query, data, n, bandwidth):
real_kde = []
for q in query:
val = 0
for xi in data:
l1_norm = np.linalg.norm(q - xi, ord=1)
if l1_norm == 0:
p = 1
else:
p = (2 / np.pi) * np.arctan(bandwidth / l1_norm) - (l1_norm / (np.pi * bandwidth)) * np.log1p((bandwidth ** 2) / (l1_norm ** 2))
val += p
real_kde.append(val / n)
return real_kde
def P_Ang(query, data, n):
real_kde = []
for q in query:
val = 0
for xi in data:
norm1 = np.linalg.norm(q)
norm2 = np.linalg.norm(xi)
cosine_similarity = np.dot(q, xi) / (norm1 * norm2)
theta = np.arccos(np.clip(cosine_similarity, -1.0, 1.0))
try:
np.seterr(divide='ignore')
p = 1 - theta / np.pi
except:
p = 1
val += p
real_kde.append(val / n)
return real_kde
def main_P_L2(d, bandwidth):
main_real_kde = 1 - 2 * ndtr(-bandwidth / d) - 2.0 / (np.sqrt(2 * math.pi) * (bandwidth / d)) * (
1 - np.exp(-0.5 * (bandwidth ** 2) / (d ** 2)))
return main_real_kde
def main_P_L1(d, bandwidth):
main_real_kde = (2 / np.pi) * np.arctan(bandwidth / d) - (d / (np.pi * bandwidth)) * np.log1p(1 + (bandwidth ** 2) / (d ** 2))
return main_real_kde
def main_P_Ang(d_theta):
main_real_kde = 1 - d_theta / np.pi
return main_real_kde
def l2kernel_kde(query, data, n, bandwidth):
kde_result = np.array(P_L2(query, data, n, bandwidth))
return kde_result
def l1kernel_kde(query, data, n, bandwidth):
kde_result = np.array(P_L1(query, data, n, bandwidth))
return kde_result
def angkernel_kde(query_data, data, n):
kde_result = np.array(P_Ang(query_data, data, n))
return kde_result
def rescale(data, left, right):
scaler = MinMaxScaler(feature_range=(left, right))
rescaled_data = scaler.fit_transform(data)
data_min = np.min(data, axis=0)
data_max = np.max(data, axis=0)
return rescaled_data, data_min, data_max
def inverse_rescale(data, data_min, data_max, left, right):
Result = []
for xi in data:
inverse_rescale = []
for dmax, dmin, d in zip(data_max, data_min, xi):
inverse_rescale.append(((d - left) / (right - left)) * (dmax - dmin) + dmin)
Result.append(inverse_rescale)
return Result