-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnn.py
128 lines (100 loc) · 5.49 KB
/
nn.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python -W ignore::DeprecationWarning
import numpy as np
import os
import knn
import warnings
warnings.filterwarnings("ignore")
from sklearn.neighbors import NearestNeighbors, LSHForest
if __name__ == '__main__':
files = (['env_shelf01', 'env_table1', 'env_table3',
'env_shelf02', 'env_kitchen1', 'env_kitchen2',
'env_kitchen_refrigerator', 'env_kitchen_microwave'])
local_path = os.getcwd()
training_set_path = os.path.join(local_path, "imp_samples/sobol_samples_1_7/")
test_set_path = os.path.join(local_path, "test_set/")
results_path = os.path.join(local_path, "metric_results/")
cfree_val = -1.0
cobs_val = 1.0
threshold = 0.0
d = cobs_val - cfree_val
dim = 7
k_values = [1, 5, 10, 15, 20]
N_values = [1000, 5000, 10000, 15000, 20000]
avg_accnn = np.zeros((len(N_values), len(k_values)))
avg_errnn = np.zeros((len(N_values), len(k_values)))
avg_accann = np.zeros((len(N_values), len(k_values)))
avg_errann = np.zeros((len(N_values), len(k_values)))
for i in range(len(files)):
print('------------------', files[i], '------------------')
accnn = np.zeros((len(N_values), len(k_values)))
errnn = np.zeros((len(N_values), len(k_values)))
accann = np.zeros((len(N_values), len(k_values)))
errann = np.zeros((len(N_values), len(k_values)))
accmnn = np.zeros((len(N_values), len(k_values)))
errmnn = np.zeros((len(N_values), len(k_values)))
accmann = np.zeros((len(N_values), len(k_values)))
errmann = np.zeros((len(N_values), len(k_values)))
for j in range(len(N_values)):
training_set_size = N_values[j]
test_set_size = training_set_size / 10
print("training_set_size:", training_set_size)
fn = 'sobol_' + files[i] + '_' + str(training_set_size) + '.npz'
n = np.load(os.path.join(training_set_path,fn))
training_set = n['samples']
training_set_ccr = n['ccr']
sjw = n['sjw']
S = np.cov(training_set.transpose())
inv_cov = np.linalg.inv(S)
fn1 = files[i] + '_' + str(N_values[j]) + '.npz'
n1 = np.load(os.path.join(test_set_path, fn1))
test_set = n1['test_set']
ccr = n1['ccr']
lshf = LSHForest()
lshf.fit(training_set)
nbrs = NearestNeighbors()
nbrs.fit(training_set)
cprnn = np.ones((test_set_size, len(k_values)))
cprann = np.ones((test_set_size, len(k_values)))
cprmnn = np.ones((test_set_size, len(k_values)))
cprmann = np.ones((test_set_size, len(k_values)))
idx = 0
while idx < test_set_size:
query = test_set[idx]
for t in range(len(k_values)):
n_neighbors = k_values[t]
cprnn[idx,t] = knn.nn_predictor(query, training_set, training_set_ccr, nbrs, n_neighbors,
method='r', weights=sjw)
cprann[idx,t] = knn.nn_predictor(query, training_set, training_set_ccr, lshf, n_neighbors,
method='r', weights=sjw)
cprmnn[idx,t] = knn.nn_predictor(query, training_set, training_set_ccr, nbrs, n_neighbors,
method='r', weights=sjw, inv_cov=inv_cov)
cprmann[idx,t] = knn.nn_predictor(query, training_set, training_set_ccr, lshf, n_neighbors,
method='r', weights=sjw, inv_cov=inv_cov)
# print(cprmann[idx,t] , cprann[idx,t]
# raw_input("s")
idx = idx + 1
for t in range(len(k_values)):
accnn[j,t] = 1.0*np.sum((cprnn[:,t] > threshold) == (ccr == cobs_val)) / test_set_size
accann[j,t] = 1.0*np.sum((cprann[:,t] > threshold) == (ccr == cobs_val)) / test_set_size
errnn[j,t] = np.sum(np.absolute(cprnn[:,t] - ccr)) / d / test_set_size
errann[j,t] = np.sum(np.absolute(cprann[:,t] - ccr)) / d / test_set_size
accmnn[j,t] = 1.0*np.sum((cprmnn[:,t] > threshold) == (ccr == cobs_val)) / test_set_size
accmann[j,t] = 1.0*np.sum((cprmann[:,t] > threshold) == (ccr == cobs_val)) / test_set_size
errmnn[j,t] = np.sum(np.absolute(cprmnn[:,t] - ccr)) / d / test_set_size
errmann[j,t] = np.sum(np.absolute(cprmann[:,t] - ccr)) / d / test_set_size
# print(accnn[j,t] , errnn[j,t], accann[j,t] , errann[j,t]
# print(accmnn[j,t] , errmnn[j,t], accmann[j,t] , errmann[j,t]
avg_accnn = avg_accnn + accnn
avg_errnn = avg_errnn + errnn
avg_accann = avg_accann + accann
avg_errann = avg_errann + errann
np.savez(os.path.join(results_path, files[i]+'_NN_w'),
accnn=accnn, accann=accann, errnn=errnn, errann=errann)
np.savez(os.path.join(results_path, files[i]+'_NN_m_w'),
accnn=accmnn, accann=accmann, errnn=errmnn, errann=errmann)
print(accnn, accmnn)
avg_accnn = avg_accnn / len(files)
avg_errnn = avg_errnn / len(files)
avg_accann = avg_accann / len(files)
avg_errann = avg_errann / len(files)
print("average_values:", avg_accnn, avg_accann, avg_errnn, avg_errann)