-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_based_method.py
280 lines (207 loc) · 8.29 KB
/
model_based_method.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
"""
Model-based coalitional explanation method (https://hal.archives-ouvertes.fr/hal-03138314)
Copyright (C) 2020 Gabriel Ferrettini <[email protected]>
Copyright (C) 2020 Julien Aligon <[email protected]>
Copyright (C) 2020 Chantal Soulé-Dupuy <[email protected]>
model_based_method.py
Copyright (C) 2020 Elodie Escriva, Kaduceo <[email protected]>
Copyright (C) 2020 Jean-Baptiste Excoffier, Kaduceo <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import random
import pandas as pd
from tqdm import tqdm
from utils import train_models, explain_groups_w_retrain, influence_calcul
from utils import check_all_attributs_groups, compute_subgroups_correlation
from utils import generate_subgroups_group, coal_penalisation
def random_permutation(groups, nb_att, preds):
nb_inst = len(preds)
nb_class = len(set(preds))
perm = {}
class_indices = [
[j for j in range(nb_inst) if preds[j] == i] for i in range(nb_class)
]
indices = list(range(nb_inst))
for group in groups:
permutation = [-1] * nb_inst
# if group is not a singleton, permutation within class
if len(group) > 1:
for class_i in class_indices:
temp_class = class_i.copy()
for i in class_i:
rand_int = random.randrange(len(temp_class))
permutation[i] = temp_class[rand_int]
temp_class.pop(rand_int)
# if the group is a singleton, fully random permutation
else:
temp_id = indices.copy()
for i in indices:
rand_int = random.randrange(len(temp_id))
permutation[i] = temp_id[rand_int]
temp_id.pop(rand_int)
for i in group:
perm[i] = permutation
return perm
def fidelity(datas, model, groups):
nb_att = datas.shape[1]
nb_inst = datas.shape[0]
nb_min = 2500
# Data augmentation if nb_inst < nb_min
new_dataset = datas.copy()
if nb_inst < nb_min:
for i in range(nb_min - nb_inst):
new_inst = {}
for j in range(nb_att):
rand_int = random.randrange(nb_inst)
new_inst[datas.columns[j]] = datas.iloc[rand_int, j]
new_dataset = new_dataset.append(new_inst, ignore_index=True)
old_preds = model.predict(new_dataset)
permutation = random_permutation(groups, nb_att, old_preds)
temp_dataset = pd.DataFrame(columns=new_dataset.columns)
for i in range(nb_inst):
temp_inst = {}
for j in range(nb_att):
temp_inst[new_dataset.columns[j]] = new_dataset.iloc[permutation[j][i], j]
temp_dataset = temp_dataset.append(temp_inst, ignore_index=True)
new_preds = model.predict(temp_dataset)
count_diff = sum(1 for i in range(nb_inst) if new_preds[i] != old_preds[i])
fid = (nb_inst - count_diff) / nb_inst
return fid
def max_fidelity(S, A, R, datas, model):
i_max = 0
fid_max = 0
for i in R:
R_temp = R.copy()
R_temp.remove(i)
groups_temp = []
temp = [i]
groups_temp += [R_temp] if len(R_temp) else []
groups_temp += [temp] if len(temp) else []
groups_temp += [A] if len(A) else []
groups_temp += [[i] for group in S for i in group] if len(S) else []
fid = fidelity(datas, model, groups_temp)
if fid_max < fid:
i_max = i
fid_max = fid
return i_max, fid_max
def model_grouping(datas, model, threshold):
nb_att = datas.shape[1]
R = [i for i in range(nb_att)]
L_m = [[i] for i in range(nb_att)]
A = []
S = [] # relevant_groups
delta = fidelity(datas, model, L_m) + threshold
while len(R) != 0 or len(A) != 0:
S_temp = [[]]
if len(S) > 0:
S_temp = [[i] for group in S for i in group]
if len(R) > 0:
S_temp.append(R)
if len(A) == 0 and fidelity(datas, model, S_temp) < delta:
S_temp = [[i] for i in R]
for att in S_temp:
if att not in S:
S.append(att)
R = []
A = []
else:
indice_max, fid_max = max_fidelity(S, A, R, datas, model)
if len(R) == 1 or fid_max < delta:
S.append(R)
R = A.copy()
A = []
else:
R.remove(indice_max)
A.append(indice_max)
S = check_all_attributs_groups(S, nb_att)
return S
def compute_instance_coal_inf(raw_instance_inf, columns, relevant_groups):
"""Coalitional method for one instance, when attributs overlap in groups (Ferrettini et al. 2020)"""
influences = dict([(c, 0) for c in columns])
denoms_shap = dict([(c, 0) for c in columns])
for group in relevant_groups:
subgroups = generate_subgroups_group(group)
for subgroup in subgroups:
for i in subgroup:
pena = coal_penalisation(len(subgroup) - 1, len(group))
denoms_shap[columns[i]] += pena
influences[columns[i]] += influence_calcul(
pena, raw_instance_inf, subgroup, i
)
for i in columns:
influences[i] /= denoms_shap[i]
return influences
def compute_coal_model_influences(
raw_groups_influences, X, relevant_groups, progression_bar
):
"""Coalitional method for all instances, when attributs overlap in groups
(Ferrettini et al. 2020)"""
coalitional_influences = pd.DataFrame(columns=X.columns)
for instance in tqdm(
X.index, desc="Model coalition influences", disable=not progression_bar
):
raw_infs = raw_groups_influences[instance]
influences = compute_instance_coal_inf(raw_infs, X.columns, relevant_groups)
coalitional_influences = coalitional_influences.append(
pd.Series(influences, name=instance)
)
return coalitional_influences
def modelbased_method(
X, y, model, threshold, problem_type, fvoid=None, look_at=None, progression_bar=True
):
groups = model_grouping(X, model, threshold) if X.shape[1] != 1 else [[0]]
groups = compute_subgroups_correlation(groups) + [[]]
pretrained_models = train_models(
model, X, y, groups, problem_type, fvoid, progression_bar
)
raw_groups_influences = explain_groups_w_retrain(
pretrained_models, X, problem_type, look_at, progression_bar
)
coalition_influences = compute_coal_model_influences(
raw_groups_influences, X, groups, progression_bar
)
return coalition_influences, pretrained_models, groups
def compute_influences(
X,
pretrained_models,
problem_type,
groups,
look_at=None,
progression_bar=True,
):
"""
Compute the influences based on the model-based coalitional method for the instances in parameter.
Parameters
----------
X : pandas.DatFrame
The training input samples.
pretrained_models : dictionary {tuple : pickle object}
Models trained to compute explanations.
problem_type :{"classification", "regression"}
Type of machine learning problem.
groups : list
Groups of attributs used to compute explanations
look_at : int, default=None
Class to look at when computing influences in case of classification problem.
If None, prediction is used.
Returns
-------
complete_influences : two-dimensional list
Influences for each attributs and each instances in the dataset.
"""
raw_groups_influences = explain_groups_w_retrain(
pretrained_models, X, problem_type, look_at, progression_bar
)
influences = compute_coal_model_influences(
raw_groups_influences, X, groups, progression_bar
)
return influences