-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpoison_comparison.py
320 lines (280 loc) · 11.1 KB
/
poison_comparison.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import sys
import time
import numpy as np
import openml
import pandas as pd
from diffprivlib.models import (
DecisionTreeClassifier as DiffprivLibDecisionTreeClassifier,
LogisticRegression as DiffPrivLibLogisticRegression,
)
from sklearn.dummy import DummyClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier
from sklearn.utils import check_random_state
from privatree.bdpt import BDPTClassifier
from privatree.dpa import DPAClassifier
from privatree.dpgdf import DPGDTClassifier
from privatree.privatree import PrivaTreeClassifier
def dpa_poison_accuracy_guarantee(poisoning_curve, data_size):
n_poison_01 = int(0.001 * data_size)
n_poison_05 = int(0.005 * data_size)
n_poison_1 = int(0.01 * data_size)
if n_poison_01 >= len(poisoning_curve):
guarantee_01 = 0
else:
guarantee_01 = poisoning_curve[n_poison_01]
if n_poison_05 >= len(poisoning_curve):
guarantee_05 = 0
else:
guarantee_05 = poisoning_curve[n_poison_05]
if n_poison_1 >= len(poisoning_curve):
guarantee_1 = 0
else:
guarantee_1 = poisoning_curve[n_poison_1]
return guarantee_01, guarantee_05, guarantee_1
def epsilon_poison_accuracy_guarantee(base_accuracy, epsilon, data_size):
"""Compute a differential privacy guarantee on poison accuracy under 0.5%, 1% and 2% of poison samples."""
n_poison_01 = int(0.001 * data_size)
n_poison_05 = int(0.005 * data_size)
n_poison_1 = int(0.01 * data_size)
return (
base_accuracy * np.exp(-epsilon * n_poison_01),
base_accuracy * np.exp(-epsilon * n_poison_05),
base_accuracy * np.exp(-epsilon * n_poison_1),
)
max_bins = 10
max_depths = [4]
epsilons = [0.001, 0.01, 0.1, 1.0]
n_splits = 5
assert len(sys.argv) == 2
benchmark = sys.argv[1]
output_filename_prefix = "out/benchmark_poisoning"
if benchmark == "categorical":
SUITE_ID = 334 # Classification on numerical and categorical features
output_filename = output_filename_prefix + "_categorical.csv"
benchmark_suite = openml.study.get_suite(SUITE_ID) # obtain the benchmark suite
task_ids = benchmark_suite.tasks
elif benchmark == "numerical":
SUITE_ID = 337 # Classification on numerical features
output_filename = output_filename_prefix + "_numerical.csv"
benchmark_suite = openml.study.get_suite(SUITE_ID) # obtain the benchmark suite
task_ids = benchmark_suite.tasks
elif benchmark == "uci":
task_ids = [
15, # breast cancer (Wisconsin)
24, # mushroom
37, # diabetes
56, # vote
959, # nursery
1590, # adult
]
output_filename = output_filename_prefix + "_uci.csv"
else:
raise ValueError(f"Unknown benchmark {benchmark}")
random_state = check_random_state(1)
results = []
for task_id in task_ids: # iterate over all tasks
if benchmark == "uci":
dataset = openml.datasets.get_dataset(
task_id
) # download the OpenML dataset directly
else:
task = openml.tasks.get_task(task_id) # download the OpenML task
dataset = task.get_dataset()
X, y, categorical_indicator, attribute_names = dataset.get_data(
dataset_format="array", target=dataset.default_target_attribute
)
# Drop rows with NaNs
keep_rows = ~np.any(np.isnan(X), axis=1)
X = X[keep_rows]
y = y[keep_rows]
print(f"Starting {dataset.name} ({X.shape})")
k_fold = StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=random_state)
for fold_i, (train_indices, test_indices) in enumerate(k_fold.split(X, y)):
X_train = X[train_indices]
y_train = y[train_indices]
X_test = X[test_indices]
y_test = y[test_indices]
# Scale the data for logistic regression
scaler = StandardScaler()
X_train_lr = scaler.fit_transform(X_train)
X_test_lr = scaler.transform(X_test)
# Compute feature ranges on the train data (these are public information)
feature_range = np.concatenate(
(X_train.min(axis=0).reshape(-1, 1), X_train.max(axis=0).reshape(-1, 1)),
axis=1,
)
bounds = (feature_range[:, 0], feature_range[:, 1])
data_norm = np.linalg.norm(X_train_lr, ord=2, axis=1).max()
# Compute number of categories if they exist (public information)
categorical_features = []
for feature_i, is_categorical in enumerate(categorical_indicator):
if not is_categorical:
categorical_features.append(0)
else:
categorical_features.append(int(X_train[:, feature_i].max() + 1))
categorical_features = np.array(categorical_features)
for epsilon in epsilons:
start_time = time.time()
logistic_regression = DiffPrivLibLogisticRegression(
random_state=random_state,
epsilon=epsilon,
data_norm=data_norm,
)
# logistic_regression.fit(X_train, y_train)
logistic_regression.fit(X_train_lr, y_train)
runtime = time.time() - start_time
# train_accuracy = logistic_regression.score(X_train, y_train)
# test_accuracy = logistic_regression.score(X_test, y_test)
train_accuracy = logistic_regression.score(X_train_lr, y_train)
test_accuracy = logistic_regression.score(X_test_lr, y_test)
results.append(
(
dataset.name,
fold_i,
"logistic regression",
None,
epsilon,
None,
train_accuracy,
test_accuracy,
runtime,
None,
*epsilon_poison_accuracy_guarantee(test_accuracy, epsilon, len(X_train)),
)
)
print(results[-1])
for max_depth in max_depths:
start_time = time.time()
tree = DecisionTreeClassifier(
max_depth=max_depth, random_state=random_state
)
tree.fit(X_train, y_train)
runtime = time.time() - start_time
train_accuracy = tree.score(X_train, y_train)
test_accuracy = tree.score(X_test, y_test)
results.append(
(
dataset.name,
fold_i,
"regular tree",
max_depth,
None,
None,
train_accuracy,
test_accuracy,
runtime,
None,
None,
None,
None,
)
)
print(results[-1])
for n_partitions in [5, 10, 50, 100, 500, 1000, 5000]:
# Skip training if there are too many partitions for the data size
if n_partitions > 0.5 * len(X_train):
continue
start_time = time.time()
dpa = DPAClassifier(
n_partitions=n_partitions, max_depth=max_depth, random_state=random_state
)
dpa.fit(X_train, y_train)
runtime = time.time() - start_time
train_accuracy = dpa.score(X_train, y_train)
test_accuracy = dpa.score(X_test, y_test)
poisoning_curve = dpa.poisoning_accuracy_curve(X_test, y_test)
results.append(
(
dataset.name,
fold_i,
"DPA",
max_depth,
None,
None,
train_accuracy,
test_accuracy,
runtime,
n_partitions,
*dpa_poison_accuracy_guarantee(poisoning_curve, len(X_train)),
)
)
print(results[-1])
for epsilon in epsilons:
start_time = time.time()
diffprivlib_tree = DiffprivLibDecisionTreeClassifier(
max_depth=max_depth,
random_state=random_state,
epsilon=epsilon,
bounds=bounds,
classes=[0, 1],
)
diffprivlib_tree.fit(X_train, y_train)
runtime = time.time() - start_time
train_accuracy = diffprivlib_tree.score(X_train, y_train)
test_accuracy = diffprivlib_tree.score(X_test, y_test)
results.append(
(
dataset.name,
fold_i,
"diffprivlib tree",
max_depth,
epsilon,
None,
train_accuracy,
test_accuracy,
runtime,
None,
*epsilon_poison_accuracy_guarantee(test_accuracy, epsilon, len(X_train)),
)
)
print(results[-1])
start_time = time.time()
private_tree = PrivaTreeClassifier(
max_depth=max_depth,
max_bins=max_bins,
epsilon=epsilon,
feature_range=feature_range,
categorical_features=categorical_features,
random_state=random_state,
)
private_tree.fit(X_train, y_train)
runtime = time.time() - start_time
train_accuracy = private_tree.score(X_train, y_train)
test_accuracy = private_tree.score(X_test, y_test)
results.append(
(
dataset.name,
fold_i,
"PrivaTree",
max_depth,
epsilon,
max_bins,
train_accuracy,
test_accuracy,
runtime,
None,
*epsilon_poison_accuracy_guarantee(test_accuracy, epsilon, len(X_train)),
)
)
print(results[-1])
results_df = pd.DataFrame(
results,
columns=[
"dataset",
"fold",
"method",
"max_depth",
"epsilon",
"max_bins",
"train accuracy",
"test accuracy",
"runtime",
"n_partitions",
"0.1% guarantee",
"0.5% guarantee",
"1% guarantee",
],
)
results_df.to_csv(output_filename, index=False)