forked from andreArtelt/TwoStageMultiinstCFs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiments.py
170 lines (138 loc) · 7.09 KB
/
experiments.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
import pickle
import random
from joblib import Parallel, delayed
import numpy as np
from xgboost import XGBClassifier
from sklearn.model_selection import GridSearchCV, KFold
from sklearn.metrics import roc_auc_score, f1_score, confusion_matrix
from imblearn.over_sampling import RandomOverSampler
from imblearn.under_sampling import RandomUnderSampler
from warren.cf_dice import DiceExplainer
from warren.groupcf_warren import compute_groupcf as compute_groupcf_warren
from ours.cf_clustering import cluster_instances
from ours.ea_mixedvar_groupcf import compute_mixedvar_groupcf
from kanamori.groupcf_kanamori import GroupCF
from datasets import load_attrition_data, load_lawSchool_dataset, load_creditCardClients_dataset
def run_exp(multiinst_method, dataset, cluster_method, k_folds=5):
results_accuracies = []
results_global_cfs = []
results_local_cfs = []
f_out_path = f"exp-results/{multiinst_method}_{dataset}_{cluster_method}.pickle"
print(f"Config: {multiinst_method, dataset, cluster_method}")
# Load data
if dataset == "attrition":
X, y, _, features_desc, features_type, features_range = load_attrition_data()
elif dataset == "credit":
X, y, _, features_desc, features_type, features_range = load_creditCardClients_dataset()
elif dataset == "lawschool":
X, y, _, features_desc, features_type, features_range = load_lawSchool_dataset()
feature_idx_whitelist = range(X.shape[1])
# Downsample some data sets for better performance
sampling = RandomUnderSampler()
X, y = sampling.fit_resample(X, y)
idx = random.sample(range(X.shape[0]), k=min(X.shape[0], 500))
X, y = X[idx, :], y[idx]
# Cross validation
kf = KFold(n_splits=k_folds, shuffle=True)
for train_index, test_index in kf.split(X):
try:
# Split data into training and test set
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
# Deal with imbalanced data sets
sampling = RandomOverSampler()
X_train, y_train = sampling.fit_resample(X_train, y_train)
# Fit classifier
xgb = XGBClassifier(objective='binary:logistic', silent=True)
parameters = {'nthread':[4],
'learning_rate': [0.03],
'max_depth': [4,5],
'min_child_weight': [20],
'max_delta_step': [8],
'gamma':[0.5,1.5, 2],
'subsample': [0.2,1],
'reg_alpha': [0.7],
'n_estimators': [600]
}
xgb_grid = GridSearchCV(xgb,
parameters,
cv = 2,
scoring = 'roc_auc',
n_jobs = 5,
verbose=False)
xgb_grid.fit(X_train,y_train)
clf = xgb_grid.best_estimator_
# Evaluate model
y_train_pred = clf.predict(X_train)
ypred_prop = clf.predict_proba(X_test)[:, 1]
ypred = clf.predict(X_test)
auc = roc_auc_score(y_test, ypred_prop)
f1 = f1_score(y_test, ypred)
results_accuracies.append(f1)
print(auc, f1)
cnf_matrix = confusion_matrix (y_test, ypred)
print(cnf_matrix)
# Consider all negatively classified instances
y_target = 0
X_instances = X_test[ypred == y_target,:]
print(f"Set D: {X_instances.shape}")
# Compute individual counterfactuals
dice_expl = DiceExplainer(clf, X_train, y_train)
X_cfs = []
X_idx = []
for i in range(X_instances.shape[0]):
try:
X_cfs.append(dice_expl.compute_counterfactual(X_instances[i, :], 1 - y_target)[0].flatten())
X_idx.append(i)
except:
pass
X_instances = X_instances[X_idx,:] # Remove instances for which no counterfactual was found!
X_cfs = np.array(X_cfs)
X_othersamples = X_train[y_train_pred == 1 - y_target,:]
# Cluster instances
clustering = cluster_instances(X_instances, X_cfs, method=cluster_method).labels_
print(f"Clustering: {clustering}")
# Compute multi-instance counterfactuals
def compute_multiinstance_cf(X_inst):
if multiinst_method == "warren":
delta_cf, cf_score = compute_groupcf_warren(clf, X_train, y_train, X_inst, 1 - y_target, X_othersamples)
cf_size = len(delta_cf) / X_inst.shape[1]
return delta_cf, cf_score, cf_size
elif multiinst_method == "ours":
delta_cf, err_rate = compute_mixedvar_groupcf(X_inst, 1-y_target, clf=clf, features_type=features_type, features_range=features_range,
features_idx_whitelist=feature_idx_whitelist)
cf_size = len(list(filter(lambda i: np.abs(delta_cf[i]) >= 1e-5, range(len(delta_cf))))) / X_inst.shape[1]
return delta_cf, 1. - err_rate, cf_size
elif multiinst_method == "kanamori":
expl = GroupCF(clf, features_desc, X_train, y_train, 1-y_target)
delta_cf, cf_score = expl.compute_explanation(X_inst)
cf_size = len(list(filter(lambda i: np.abs(delta_cf[i]) >= 1e-5, range(len(delta_cf))))) / X_inst.shape[1]
return delta_cf, cf_score, cf_size
cluster_size = len(X_instances) # Global
_, cf_score, cf_size = compute_multiinstance_cf(X_instances)
print(f"Global: {cluster_size, cf_score, cf_size}")
results_global_cfs.append((cluster_size, cf_score, cf_size))
local_cfs = []
for l in np.unique(clustering): # Local
idx = clustering == l
cluster_size = np.sum(idx)
delta_cf, cf_score, cf_size = compute_multiinstance_cf(X_instances[idx,:])
print(f"Local: {cluster_size, cf_score, cf_size}")
local_cfs.append((cluster_size, cf_score, cf_size))
results_local_cfs.append(local_cfs)
except Exception as ex:
print(ex)
# Store results
with open(f_out_path, "wb") as f_out:
pickle.dump({"results_accuracies": results_accuracies,
"results_global_cfs": results_global_cfs,
"results_local_cfs": results_local_cfs}, f_out)
if __name__ == "__main__":
configs = []
for multiinst_method in ["ours"]:#, "warren", "kanamori"]:
for dataset in ["credit", "attrition", "lawschool"]:
for cluster_method in ["dbscan-cf", "dbscan-xorig"]:
configs.append({"multiinst_method": multiinst_method,
"dataset": dataset,
"cluster_method": cluster_method})
Parallel(n_jobs=4)(delayed(run_exp)(**param_config) for param_config in configs)