-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation_nq_acc.py
188 lines (151 loc) · 8.09 KB
/
simulation_nq_acc.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import numpy as np
import os
import time
import scipy
import math
import pandas as pd
from itertools import product
import argparse
from joblib import Parallel, delayed
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import log_loss
from distribution import TransferDistribution
from LDPCP import LDPTreeClassifier
parser = argparse.ArgumentParser(description='Simulation of LPCT')
parser.add_argument('-nt', type=int, help='n train', default= 1000)
parser.add_argument('-dis', type=int, help='distribution index', default = 4)
args = parser.parse_args()
def base_train(iterate, epsilon, n_train, n_pub, distribution_index):
log_file_dir = "./results/nq_acc/"
np.random.seed(iterate)
sample_generator = TransferDistribution(distribution_index).returnDistribution()
n_test = 2000
X_P, y_P, X_Q, y_Q = sample_generator.generate(n_train, n_pub)
X_P_test, y_P_test, _, _ = sample_generator.generate(n_test, 10)
################################################################################################
method = "LDPTC-M"
param_dict = {"min_samples_split":[1],
"min_samples_leaf":[1, 5, 10, 50, 100],
"max_depth":[1, 2, 3, 4, 5, 6, 7, 8],
"lamda": [ 0.01, 0.1, 0.5, 1, 2, 5, 10, 50, 100, 200, 300, 400, 500, 750, 1000, 1250, 1500, 2000, 4000, 8000],
"X_Q":[X_Q],
"y_Q": [y_Q],
"epsilon": [epsilon],
"splitter": ['igmaxedge'],
"estimator":["laplace"],
}
for param_values in product(*param_dict.values()):
params = dict(zip(param_dict.keys(), param_values))
time_start = time.time()
model = LDPTreeClassifier(**params).fit(X_P, y_P)
y_hat = model.predict(X_P_test)
eta_hat = model.predict_proba(X_P_test)
accuracy = (y_hat == y_P_test).mean()
bce = - log_loss(y_P_test, eta_hat)
time_end = time.time()
time_used = time_end - time_start
log_file_name = "{}.csv".format(method)
log_file_path = os.path.join(log_file_dir, log_file_name)
with open(log_file_path, "a") as f:
logs= "{},{},{},{},{},{},{},{},{},{},{},{}\n".format(distribution_index,
method,
iterate,
epsilon,
n_train,
n_pub,
accuracy,
bce,
time_used,
params["max_depth"],
params["min_samples_leaf"],
params["lamda"],
)
f.writelines(logs)
################################################################################################
method = "LDPTC-M-P"
param_dict = {"min_samples_split":[1],
"min_samples_leaf":[1, 5, 10, 50, 100],
"max_depth":[1,2,3,4,5,6],
"lamda": [1],
"X_Q":[X_Q],
"y_Q": [y_Q],
"epsilon": [epsilon],
"splitter": ['igmaxedge'],
"estimator":["laplace"],
}
for param_values in product(*param_dict.values()):
params = dict(zip(param_dict.keys(), param_values))
time_start = time.time()
model = LDPTreeClassifier(**params).fit(X_P, y_P)
y_P_hat, _ = model.separate_predict(X_P_test)
eta_P_hat, _ = model.separate_predict_proba(X_P_test)
accuracy = (y_P_hat == y_P_test).mean()
bce = - log_loss(y_P_test, eta_P_hat)
time_end = time.time()
time_used = time_end - time_start
log_file_name = "{}.csv".format(method)
log_file_path = os.path.join(log_file_dir, log_file_name)
with open(log_file_path, "a") as f:
logs= "{},{},{},{},{},{},{},{},{},{},{},{}\n".format(distribution_index,
method,
iterate,
epsilon,
n_train,
n_pub,
accuracy,
bce,
time_used,
params["max_depth"],
params["min_samples_leaf"],
params["lamda"],
)
f.writelines(logs)
################################################################################################
method = "LDPTC-M-Q"
param_dict = {"min_samples_split":[1],
"min_samples_leaf":[1, 5, 10, 50, 100],
"max_depth":[1,2,3,4,5,6],
"lamda": [1],
"X_Q":[X_Q],
"y_Q": [y_Q],
"epsilon": [epsilon],
"splitter": ['igmaxedge'],
"estimator":["laplace"],
}
for param_values in product(*param_dict.values()):
params = dict(zip(param_dict.keys(), param_values))
time_start = time.time()
model = LDPTreeClassifier(**params).fit(X_P, y_P)
_, y_Q_hat = model.separate_predict(X_P_test)
_, eta_Q_hat = model.separate_predict_proba(X_P_test)
accuracy = (y_Q_hat == y_P_test).mean()
bce = - log_loss(y_P_test, eta_Q_hat)
time_end = time.time()
time_used = time_end - time_start
log_file_name = "{}.csv".format(method)
log_file_path = os.path.join(log_file_dir, log_file_name)
with open(log_file_path, "a") as f:
logs= "{},{},{},{},{},{},{},{},{},{},{},{}\n".format(distribution_index,
method,
iterate,
epsilon,
n_train,
n_pub,
accuracy,
bce,
time_used,
params["max_depth"],
params["min_samples_leaf"],
params["lamda"],
)
f.writelines(logs)
if __name__ == "__main__":
n_pub_vec = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 120, 140, 160, 180, 200]
num_repetitions = 300
num_jobs = 50
for epsilon in [0.5, 1, 2, 4, 8, 1000]:
print(epsilon)
for n_pub in n_pub_vec:
Parallel(n_jobs = num_jobs)(delayed(base_train)(i, epsilon, args.nt, n_pub, args.dis) for i in range(num_repetitions))