-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation_depth.py
138 lines (114 loc) · 5.81 KB
/
simulation_depth.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
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
def base_train(iterate, epsilon, n_train, n_pub, distribution_index):
log_file_dir = "./results/depth/"
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":[0],
"min_samples_leaf":[0],
"if_prune": [0],
"X_Q":[X_Q],
"y_Q": [y_Q],
"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],
"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"],
0,
params["lamda"],
)
f.writelines(logs)
################################################################################################
method = "LDPTC-M-prune"
param_dict = {"min_samples_split":[0],
"min_samples_leaf":[0],
"if_prune": [1],
"X_Q":[X_Q],
"y_Q": [y_Q],
"max_depth": [1,2,3,4,5,6,7,8],
"min_depth": [1,2,3,4,5,6,7],
"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_depth"],
0,
)
f.writelines(logs)
if __name__ == "__main__":
n_train_vec = [10000]
num_repetitions = 200
num_jobs = 67
n_pub_vec = [50, 100, 200]
for distribution_idx in [4]:
for epsilon in [0.001, 0.5, 1, 2, 4, 8, 1000]:
print(epsilon)
for n_train in n_train_vec:
for n_pub in n_pub_vec:
print(n_train, n_pub)
Parallel(n_jobs = num_jobs)(delayed(base_train)(i, epsilon, n_train, n_pub, distribution_idx) for i in range(num_repetitions))