forked from giangnm58/Fair-AutoML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtitanic_XGB_EOD.py
458 lines (380 loc) · 16.5 KB
/
titanic_XGB_EOD.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import sys
import os
# Get the directory path containing autosklearn
package_dir = os.path.abspath(os.path.join(os.path.dirname("Fair-AutoML"), '../..'))
# Add the directory to sys.path
sys.path.append(package_dir)
import datetime
import pickle
from ConfigSpace.configuration_space import ConfigurationSpace
from ConfigSpace.hyperparameters import CategoricalHyperparameter, UniformFloatHyperparameter, \
UniformIntegerHyperparameter, UnParametrizedHyperparameter
from sklearn.ensemble import RandomForestClassifier
import autosklearn.pipeline.components.classification
from autosklearn.Fairea.fairea import create_baseline
from autosklearn.pipeline.components.classification \
import AutoSklearnClassificationAlgorithm
from autosklearn.pipeline.constants import DENSE, UNSIGNED_DATA, PREDICTIONS, SPARSE, SIGNED_DATA
from autosklearn.util.common import check_for_bool, check_none
import numpy as np
import pandas as pd
from aif360.datasets import StandardDataset
from sklearn.linear_model import LogisticRegression
import sklearn.metrics
import autosklearn.classification
from autosklearn.upgrade.metric import disparate_impact, statistical_parity_difference, equal_opportunity_difference, average_odds_difference
import os, shutil
train_list = "data_orig_train.pkl"
test_list = "data_orig_test.pkl"
def custom_preprocessing(df):
def group_race(x):
if x == "White":
return 1.0
else:
return 0.0
# Recode sex and race
df['sex'] = df['sex'].replace({'Female': 0.0, 'Male': 1.0})
df['race'] = df['race'].apply(lambda x: group_race(x))
return df
############################################################################
# File Remover
# ============
now = str(datetime.datetime.now())[:19]
now = now.replace(":", "_")
temp_path = "titanic_xgb_eod" + str(now)
try:
os.remove("test_split.txt")
except:
pass
try:
os.remove("num_keys.txt")
except:
pass
try:
os.remove("beta.txt")
except:
pass
f = open("beta.txt", "w")
f.close()
############################################################################
# Data Loading
# ============
train = pd.read_csv('train.csv')
test = pd.read_csv('test.csv')
test.loc[:, 'Survived'] = 0
from sklearn.base import TransformerMixin
from sklearn.pipeline import Pipeline, FeatureUnion
from typing import List, Union, Dict
class SelectCols(TransformerMixin):
"""Select columns from a DataFrame."""
def __init__(self, cols: List[str]) -> None:
self.cols = cols
def fit(self, x: None) -> "SelectCols":
"""Nothing to do."""
return self
def transform(self, x: pd.DataFrame) -> pd.DataFrame:
"""Return just selected columns."""
return x[self.cols]
sc = SelectCols(cols=['Sex', 'Survived'])
sc.transform(train.sample(5))
class LabelEncoder(TransformerMixin):
"""Convert non-numeric columns to numeric using label encoding.
Handles unseen data on transform."""
def fit(self, x: pd.DataFrame) -> "LabelEncoder":
"""Learn encoder for each column."""
encoders = {}
for c in x:
v, k = zip(pd.factorize(x[c].unique()))
encoders[c] = dict(zip(k[0], v[0]))
self.encoders_ = encoders
return self
def transform(self, x) -> pd.DataFrame:
"""For columns in x that have learned encoders, apply encoding."""
x = x.copy()
for c in x:
# Ignore new, unseen values
x.loc[~x[c].isin(self.encoders_[c]), c] = np.nan
# Map learned labels
x.loc[:, c] = x[c].map(self.encoders_[c])
# Return without nans
return x.fillna(-2).astype(int)
le = LabelEncoder()
le.fit_transform(train[['Pclass', 'Sex']].sample(5))
class NumericEncoder(TransformerMixin):
"""Remove invalid values from numerical columns, replace with median."""
def fit(self, x: pd.DataFrame) -> "NumericEncoder":
"""Learn median for every column in x."""
self.encoders_ = {
c: pd.to_numeric(x[c],
errors='coerce').median(skipna=True) for c in x}
return self
def transform(self, x: pd.DataFrame) -> pd.DataFrame:
# Create a list of new DataFrames, each with 2 columns
output_dfs = []
for c in x:
new_cols = pd.DataFrame()
# Find invalid values that aren't nans (-inf, inf, string)
invalid_idx = pd.to_numeric(x[c].replace([-np.inf, np.inf],
np.nan),
errors='coerce').isnull()
# Copy to new df for this column
new_cols.loc[:, c] = x[c].copy()
# Replace the invalid values with learned median
new_cols.loc[invalid_idx, c] = self.encoders_[c]
# Mark these replacement in a new column called
# "[column_name]_invalid_flag"
new_cols.loc[:, f"{c}_invalid_flag"] = invalid_idx.astype(np.int8)
output_dfs.append(new_cols)
# Concat list of output_dfs to single df
df = pd.concat(output_dfs,
axis=1)
return df.fillna(0)
ne = NumericEncoder()
ne.fit_transform(train[['Age', 'Fare']].sample(5))
# LabelEncoding fork: Select object columns -> label encode
pp_object_cols = Pipeline([('select', SelectCols(cols=['Sex', 'Survived',
'Cabin', 'Ticket',
'SibSp', 'Embarked',
'Parch', 'Pclass',
'Name'])),
('process', LabelEncoder())])
# NumericEncoding fork: Select numeric columns -> numeric encode
pp_numeric_cols = Pipeline([('select', SelectCols(cols=['Age',
'Fare'])),
('process', NumericEncoder())])
# We won't use the next part, but typically the pipeline would continue to
# the model (after dropping 'Survived' from the training data, of course).
# For example:
pp_pipeline = FeatureUnion([('object_cols', pp_object_cols),
('numeric_cols', pp_numeric_cols)])
model_pipeline = Pipeline([('pp', pp_pipeline),
('mod', LogisticRegression())])
train_ = train
# .fit_transform on train
train_pp = pd.concat((pp_numeric_cols.fit_transform(train_),
pp_object_cols.fit_transform(train_)),
axis=1)
# .transform on test
test_pp = pd.concat((pp_numeric_cols.transform(test),
pp_object_cols.transform(test)),
axis=1)
test_pp.sample(5)
target = 'Survived'
x_columns = [c for c in train_pp if c != target]
x_train, y_train = train_pp[x_columns], train_pp[target]
x_test = test_pp[x_columns]
df = pd.concat((x_train, y_train), axis=1)
train = pd.read_pickle(train_list)
test = pd.read_pickle(test_list)
data_orig_train = StandardDataset(train,
label_name='Survived',
protected_attribute_names=['Sex'],
favorable_classes=[1],
privileged_classes=[[1]])
data_orig_test = StandardDataset(test,
label_name='Survived',
protected_attribute_names=['Sex'],
favorable_classes=[1],
privileged_classes=[[1]])
privileged_groups = [{'Sex': 1}]
unprivileged_groups = [{'Sex': 0}]
X_train = data_orig_train.features
y_train = data_orig_train.labels.ravel()
X_test = data_orig_test.features
y_test = data_orig_test.labels.ravel()
# dataset_orig = StandardDataset(df,
# label_name='Survived',
# protected_attribute_names=['Sex'],
# favorable_classes=[1],
# privileged_classes=[[1]])
#
# privileged_groups = [{'Sex': 1}]
# unprivileged_groups = [{'Sex': 0}]
#
# data_orig_train, data_orig_test = dataset_orig.split([0.7], shuffle=True)
#
# X_train = data_orig_train.features
# y_train = data_orig_train.labels.ravel()
#
# X_test = data_orig_test.features
# y_test = data_orig_test.labels.ravel()
class CustomXGBoost(AutoSklearnClassificationAlgorithm):
def __init__(self,
n_estimators,
max_depth,
learning_rate,
subsample,
min_child_weight,
seed=0,
random_state=None
):
self.n_estimators = n_estimators
self.max_depth = max_depth
self.learning_rate = learning_rate
self.subsample = subsample
self.min_child_weight = min_child_weight
self.seed = seed
self.random_state = random_state
def fit(self, X, y):
from xgboost import XGBClassifier
self.estimator = XGBClassifier(
n_estimators=self.n_estimators,
max_depth=self.max_depth,
learning_rate=self.learning_rate,
subsample=self.subsample,
min_child_weight=self.min_child_weight,
seed=self.seed,
random_state=self.random_state
)
self.estimator.fit(X, y)
return self
def predict(self, X):
if self.estimator is None:
raise NotImplementedError()
return self.estimator.predict(X)
def predict_proba(self, X):
if self.estimator is None:
raise NotImplementedError()
return self.estimator.predict_proba(X)
@staticmethod
def get_properties(dataset_properties=None):
return {'shortname': 'XG',
'name': 'XGBoost Classifier',
'handles_regression': False,
'handles_classification': True,
'handles_multiclass': True,
'handles_multilabel': False,
'handles_multioutput': False,
'is_deterministic': False,
# Both input and output must be tuple(iterable)
'input': [DENSE, SIGNED_DATA, UNSIGNED_DATA],
'output': [PREDICTIONS]}
@staticmethod
def get_hyperparameter_search_space(dataset_properties=None):
cs = ConfigurationSpace()
# The maximum number of features used in the forest is calculated as m^max_features, where
# m is the total number of features, and max_features is the hyperparameter specified below.
# The default is 0.5, which yields sqrt(m) features as max_features in the estimator. This
# corresponds with Geurts' heuristic.
n_estimators = UniformIntegerHyperparameter("n_estimators", 228, 776, default_value=300)
max_depth = UniformIntegerHyperparameter("max_depth", 3, 9,
default_value=4)
learning_rate = UniformFloatHyperparameter("learning_rate", 0.04780, 0.50805,
default_value=0.04780)
subsample = UniformFloatHyperparameter("subsample", 0.33298, 0.85932,
default_value=0.85932)
min_child_weight = UniformIntegerHyperparameter("min_child_weight", 5, 17,
default_value=5)
cs.add_hyperparameters([n_estimators, max_depth, learning_rate, subsample,
min_child_weight])
return cs
autosklearn.pipeline.components.classification.add_classifier(CustomXGBoost)
cs = CustomXGBoost.get_hyperparameter_search_space()
print(cs)
############################################################################
# Custom metrics definition
# =========================
def accuracy(solution, prediction):
metric_id = 3
protected_attr = 'Sex'
with open('test_split.txt') as f:
first_line = f.read().splitlines()
last_line = first_line[-1]
split = list(last_line.split(","))
for i in range(len(split)):
split[i] = int(split[i])
subset_data_orig_train = data_orig_train.subset(split)
if os.stat("beta.txt").st_size == 0:
import xgboost as xgb
default = xgb.XGBClassifier(learning_rate=0.01, max_depth=4, n_estimators=300, seed=0)
degrees = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]
mutation_strategies = {"0": [1, 0], "1": [0, 1]}
dataset_orig = subset_data_orig_train
res = create_baseline(default, dataset_orig, privileged_groups, unprivileged_groups,
data_splits=10, repetitions=10, odds=mutation_strategies, options=[0, 1],
degrees=degrees)
acc0 = np.array([np.mean([row[0] for row in res["0"][degree]]) for degree in degrees])
acc1 = np.array([np.mean([row[0] for row in res["1"][degree]]) for degree in degrees])
fair0 = np.array([np.mean([row[metric_id] for row in res["0"][degree]]) for degree in degrees])
fair1 = np.array([np.mean([row[metric_id] for row in res["1"][degree]]) for degree in degrees])
if min(acc0) > min(acc1):
beta = (max(acc0) - min(acc0)) / (max(acc0) - min(acc0) + max(fair0))
else:
beta = (max(acc1) - min(acc1)) / (max(acc1) - min(acc1) + max(fair1))
f = open("beta.txt", "w")
f.write(str(beta))
f.close()
else:
f = open("beta.txt", "r")
beta = float(f.read())
f.close()
# print('yyyy')
# print(beta)
beta += 0.2
if beta > 1.0:
beta = 1.0
try:
num_keys = sum(1 for line in open('num_keys.txt'))
print(num_keys)
beta -= 0.050 * int(int(num_keys) / 10)
if int(num_keys) % 10 == 0:
os.remove(temp_path + "/.auto-sklearn/ensemble_read_losses.pkl")
f.close()
except FileNotFoundError:
pass
fairness_metrics = [1 - np.mean(solution == prediction),
disparate_impact(subset_data_orig_train, prediction, protected_attr),
statistical_parity_difference(subset_data_orig_train, prediction, protected_attr),
equal_opportunity_difference(subset_data_orig_train, prediction, solution, protected_attr),
average_odds_difference(subset_data_orig_train, prediction, solution, protected_attr)]
print(fairness_metrics[metric_id], 1 - np.mean(solution == prediction),
fairness_metrics[metric_id] * beta + (1 - np.mean(solution == prediction)) * (1 - beta), beta)
return fairness_metrics[metric_id] * beta + (1 - np.mean(solution == prediction)) * (1 - beta)
############################################################################
# Second example: Use own accuracy metric
# =======================================
print("#" * 80)
print("Use self defined accuracy metric")
accuracy_scorer = autosklearn.metrics.make_scorer(
name="accu",
score_func=accuracy,
optimum=1,
greater_is_better=False,
needs_proba=False,
needs_threshold=False,
)
############################################################################
# Build and fit a classifier
# ==========================
automl = autosklearn.classification.AutoSklearnClassifier(
time_left_for_this_task=60*60,
# per_run_time_limit=500,
memory_limit=10000000,
include_estimators=['CustomXGBoost'],
ensemble_size=1,
include_preprocessors=['extra_trees_preproc_for_classification', 'polynomial', 'select_rates_classification'],
tmp_folder=temp_path,
delete_tmp_folder_after_terminate=False,
metric=accuracy_scorer
)
automl.fit(X_train, y_train)
############################################################################
# Print the final ensemble constructed by auto-sklearn
# ====================================================
print(automl.show_models())
###########################################################################
# Get the Score of the final ensemble
# ===================================
a_file = open("titanic_xgb_eod_60sp" + str(now) + ".pkl", "wb")
pickle.dump(automl.cv_results_, a_file)
a_file.close()
a_file1 = open("automl_titanic_xgb_eod_60sp" + str(now) + ".pkl", "wb")
pickle.dump(automl, a_file1)
a_file1.close()
predictions = automl.predict(X_test)
count = 0
print("EOD-Accuracy score:", sklearn.metrics.accuracy_score(y_test, predictions))
print(disparate_impact(data_orig_test, predictions, 'Sex'))
print(statistical_parity_difference(data_orig_test, predictions, 'Sex'))
print(equal_opportunity_difference(data_orig_test, predictions, y_test, 'Sex'))
print(average_odds_difference(data_orig_test, predictions, y_test, 'Sex'))