-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
343 lines (269 loc) · 9.53 KB
/
utils.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
"""
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]>
utils.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 numpy as np
import pandas as pd
import pickle
from sklearn.base import clone
from tqdm import tqdm
def generate_groups_wo_label(nb_attributs):
"""
Generates all possible combinations of attributes.
Parameters
----------
nb_attributs : int
Number of attributs in the dataset
Returns
-------
two-dimensional list
List of groups of index, representing all the combinations for the
number of attributs
"""
return [
[j for j in range(nb_attributs) if ((i & (1 << j)) > 0)]
for i in range(2 ** nb_attributs)
]
def generate_subgroups_group(group):
"""
Generates all possible subgroups from a group of attributes.
Parameters
----------
group : list
list of attributs.
Returns
-------
two-dimensional list
list of all the subgroups from the attributs list.
"""
sub_ids = generate_groups_wo_label(len(group))
sub_ids = [i for i in sub_ids if len(i) > 0]
return [[group[i] for i in index] for index in sub_ids]
def compute_subgroups_correlation(groups):
subgroups_list = []
for group in groups:
subgroups_list.extend(
[
subgroup
for subgroup in generate_subgroups_group(group)
if subgroup not in subgroups_list
]
)
return subgroups_list
def sorted_groups(groups):
return [sorted(group) for group in groups]
def check_all_attributs_groups(groups, nb_attributs):
"""
Check if all attributs are in at least one group.
Parameters
----------
groups : two-dimension list
List of groups defined by a coalitional method.
nb_attributs : int
Number of attribut in the dataset.
Returns
-------
groups : two-dimension list
Checked list of groups, with all attributs.
"""
for i in range(nb_attributs):
flag = False
for group in groups:
flag = flag | (i in group)
if not flag:
groups.append([i])
return sorted_groups(groups)
def remove_inclusions(groups):
return [
groups[i]
for i in range(len(groups))
if not any([set(groups[i]).issubset(x) for x in groups[:i] + groups[(i + 1) :]])
]
def train_models(model, X, y, groups, problem_type, fvoid, progression_bar=True):
"""
Trains the model with all the attributs, compute the
and an array of model, each one wo one group of attribut
Parameters
----------
model : Scikit-learn model
Model to train.
X : Pandas.Dataframe
Dataframe of the input datas.
y : Pandas.Dataframe
dataframe of the expected prediction from the model.
groups : two-dimensional list
List of all possible attributs subgroups.
fvoid : float
Prediction when all attributs are unknown. If None, the default value is used : expected value for each class for classification, mean label for regression.
progression_bar : boolean, default=True
If True, progression bar are shown during computing explanations
Returns
-------
pretrained_models : dictionary {tuple : pickle object}
Dictionary of the Pre-trained models (serialized with pickle). The key
is the tuple of attributs the model has been train on. The item is the pre-trained model
"""
n_variables = X.shape[1]
pretrained_models = {}
complete_group = [i for i in range(X.shape[1])]
pretrained_models[tuple(complete_group)] = pickle.dumps(model)
for group in tqdm(groups, desc="Train", disable=not progression_bar):
model_clone = clone(model)
if len(group) == 0:
if fvoid is None:
if problem_type == "Classification":
fvoid = y.value_counts(normalize=True).sort_index().values
elif problem_type == "Regression":
fvoid = y.mean()
pretrained_models[tuple(group)] = fvoid
elif len(group) < n_variables:
model_clone.fit(X[X.columns[group]].values, y.values.flatten())
pickle_model = pickle.dumps(model_clone)
group.sort()
pretrained_models[tuple(group)] = pickle_model
return pretrained_models
def explain_groups_w_retrain(
pretrained_models, X, problem_type, look_at, progression_bar=True
):
"""
Computes for each instance, the influences of all attribute groups used to pre-train models.
Parameters
----------
pretrained_models : dict {tuple : pickle object}
Dictionary of all the Pre-trained models (serialized with pickle).
X : Pandas.Dataframe
Dataframe of the input datas.
problem_type : {"classification", "regression"}
Type of machine learning problem.
look_at : int
class to look at when computing influences in case of classification problem.
progression_bar : boolean, default=True
If True, progression bar are shown during computing explanations
Returns
-------
raw_influences : dict {int : dict {tuple : float}}
Influence of each group of attributs for each instances.
The key is the instance index, the value is a dictionary with attributs group tuple and the influence of the group.
"""
raw_influences = {}
for i in X.index:
raw_influences[i] = {}
all_attributes = tuple([i for i in range(X.shape[1])])
preds = pickle.loads(pretrained_models.get(all_attributes)).predict(X)
fvoid = pretrained_models.get(())
for group in tqdm(
pretrained_models.keys(), desc="Raw influences", disable=not progression_bar
):
if len(group) == 0:
for i in X.index:
raw_influences[i][group] = 0.0
else:
model = pickle.loads(pretrained_models.get(group))
X_groups = X[X.columns[list(group)]]
if problem_type == "Classification":
preds_proba = pd.DataFrame(
model.predict_proba(X_groups), index=X_groups.index
)
for i in X.index:
look_at_i = look_at
if look_at == None:
look_at_i = preds[i]
raw_influences[i][group] = (
preds_proba.loc[i, look_at_i] - fvoid[look_at_i]
)
elif problem_type == "Regression":
preds_ = pd.DataFrame(model.predict(X_groups), index=X_groups.index)
for i in X.index:
raw_influences[i][group] = preds_.loc[i, 0] - fvoid
return raw_influences
def standard_penalisation(s, n):
"""
Compute the penalisation values for complete method.
Parameters
----------
n : int
length of the subgroup.
s : int
length of the group.
Returns
-------
Float
the penalisation for the subgroup.
"""
return (np.math.factorial(s) * np.math.factorial(n - s - 1)) / (
np.math.factorial(n)
)
def kdepth_penalisation(s, n, k):
"""
Compute the penalisation values for k-depth method.
Parameters
----------
n : int
length of the subgroup.
s : int
length of the group.
k : int
max cardinal of the subgroups.
Returns
-------
Float
the penalisation for the subgroup.
"""
return (np.math.factorial(s) * np.math.factorial(n - s - 1)) / (
k * np.math.factorial(n - 1)
)
def coal_penalisation(s, n):
"""
Compute the penalisation values for coalitional method.
Parameters
----------
n : int
length of the subgroup.
s : int
length of the group.
Returns
-------
Float
the penalisation for the subgroup.
"""
return np.math.factorial(s) * np.math.factorial(n - s - 1)
def influence_calcul(pena, raw_infs, group, i):
"""
Compute the influence of a attribut for one group, regardless the method.
Parameters
----------
pena : float
Penalisation of the group, regarding the method.
raw_infs : dict {tuple : float}
Influence of each group of attributs for one instance.
group : list
Attributs in the group to study.
i : int
Attribut to study.
Returns
-------
float
Influence of the attribut i in the group.
"""
group_clone = list(group).copy()
group_clone.remove(i)
return (
pena * (raw_infs.get(tuple(group)))
if len(group_clone) == 0
else pena * (raw_infs.get(tuple(group)) - raw_infs.get(tuple(group_clone)))
)