-
Notifications
You must be signed in to change notification settings - Fork 0
/
coalitional_methods.py
507 lines (401 loc) · 15 KB
/
coalitional_methods.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
"""
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]>
coalitional_methods.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 sys
import pandas as pd
import numpy as np
import itertools
from tqdm import tqdm
from statsmodels.stats.outliers_influence import variance_inflation_factor
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from utils import train_models, explain_groups_w_retrain, influence_calcul
from utils import check_all_attributs_groups, compute_subgroups_correlation
from utils import remove_inclusions, generate_subgroups_group, coal_penalisation
def compute_vifs(datas):
"""
Compute Variance Inflation Factor for each attribut in the dataset.
Parameters
----------
datas : pandas.DataFrame
Dataframe of the input datas.
Returns
-------
pandas.Series
VIF for each attributs.
"""
return pd.Series(
[
variance_inflation_factor(datas.assign(const=1).values, i)
for i in range(datas.shape[1])
],
index=datas.columns,
)
def vif_grouping(datas, threshold, reverse=False):
"""
Generate groups of attributs based on VIF or reverse VIF method.
Parameters
----------
datas : pandas.DataFrame
Dataframe of the input datas.
threshold : float
Correlation threshold between two attributes.
reverse : boolean, default=False
Define the method to use : reverse or not.
Returns
-------
groups : two-dimensional list
Groups of (un)correlated attributs based on the (reverse) VIF method.
"""
true_j = 0
groups = []
vifs = compute_vifs(datas)
for i in range(datas.shape[1]):
group = [i]
datas_clone = datas.drop(datas.columns[i], axis=1)
new_vifs = compute_vifs(datas_clone)
for j in range(datas_clone.shape[1]):
true_j = j + 1 if j >= 1 else j
vif_formula = (
(new_vifs[j] > vifs[true_j] * (1 - threshold * 0.05))
if reverse
else (new_vifs[j] < vifs[true_j] * (0.4 + threshold))
)
if vif_formula:
group.append(true_j)
group.sort()
if not group in groups:
groups.append(group)
groups = check_all_attributs_groups(groups, datas.shape[1])
return groups
def spearman_grouping(datas, threshold, reverse=False):
"""
Generate groups of attributs based on Spearman or reverse Spearman methods
Parameters
----------
datas : pandas.DataFrame
Dataframe of the input datas.
threshold : float
Correlation threshold between two attributes.
reverse : boolean, default=false
Define the method to use : reverse or not.
Returns
-------
groups : two-dimensional list
Groups of (un)correlated attributs based on the (reverse) Spearman method.
"""
groups = []
spearman_matrix = datas.corr(method="spearman")
for i in range(datas.shape[1]):
group = [i]
max_ = max(abs(spearman_matrix).iloc[i].drop([spearman_matrix.columns[i]]))
min_ = min(abs(spearman_matrix).iloc[i].drop([spearman_matrix.columns[i]]))
for j in range(spearman_matrix.shape[1]):
if reverse:
group_condition = (
min_ < 0.5
and j != i
and abs(spearman_matrix.iloc[i, j]) < min_ + max_ * threshold
)
else:
group_condition = (
max_ > 0.1
and j != i
and np.abs(spearman_matrix.iloc[i, j]) > max_ * (1 - threshold)
)
if group_condition:
group.append(j)
group.sort()
if not group in groups:
groups.append(group)
groups = check_all_attributs_groups(groups, datas.shape[1])
return groups
def pca_grouping(datas, threshold, reverse=False):
"""
Generate groups of attributs based on PCA method.
Parameters
----------
datas : pandas.DataFrame
Dataframe of the input datas.
threshold : float
Correlation threshold between two attributes.
Returns
-------
groups : two-dimensional list
Groups of correlated attributs based on the PCA method.
"""
groups = []
pca = PCA().fit(datas)
eigenvectors = pca.components_
for vector in eigenvectors:
group = []
max_vect = max(abs(vector))
for k in range(len(vector)):
if abs(vector[k]) == max_vect:
group.append(k)
elif abs(vector[k]) > max_vect * (1 - threshold):
group.append(k)
group.sort()
if not group in groups:
groups.append(group)
groups = check_all_attributs_groups(groups, datas.shape[1])
return groups
def compute_number_distinct_subgroups(groups):
subgroups_list = compute_subgroups_correlation(groups)
subgroups_list.sort()
return sum(1 for x in itertools.groupby(subgroups_list))
def find_alpha_rate(coal_function, n_rate, X, max_iterations=100):
"""
Compute the alpha to achieve the wanted complexity rate.
Bisection method is used to find the appropriate alpha-threshold.
Parameters
----------
coal_function : String
Name of the coalitional method to use.
n_rate : int
Number of subgroups needed to achieve complexity rate.
X : pandas.DataFrame
Dataframe of the input datas.
max_iterations : int, default=100
Maximum number of iteration for the bisection.
Returns
-------
subgroups_best : two-dimensional list
Groups of attributs compute with the best alpha and the coalition method.
alpha_best : float
Best alpha-threshold find by the bisection method
"""
alpha = 0.5
subgroups = coal_function(X, threshold=alpha)
nb_subgroups = compute_number_distinct_subgroups(subgroups)
(alpha_best, subgroups_best, nb_subgroups_best) = (alpha, subgroups, nb_subgroups)
if nb_subgroups == n_rate:
return subgroups, alpha
i = 0
while i < max_iterations:
alpha = alpha + alpha / 2 if nb_subgroups < n_rate else alpha - alpha / 2
subgroups = coal_function(X, threshold=alpha)
nb_subgroups = compute_number_distinct_subgroups(subgroups)
if nb_subgroups == n_rate:
return subgroups, alpha
if abs(nb_subgroups - n_rate) < abs(nb_subgroups_best - n_rate):
(alpha_best, subgroups_best, nb_subgroups_best) = (
alpha,
subgroups,
nb_subgroups,
)
i += 1
return subgroups_best, alpha_best
def complexity_coal_groups(X, rate, grouping_function, reverse):
"""
Compute attributs groups based on the method and the complexity rate in parameter.
Parameters
----------
X : pandas.DataFrame
Dataframe of the input datas.
rate : float
Complexity percentage.
grouping_function : string
Name of the coalitional method to use.
reverse : bool
Type of method to use for Spearman and VIF coalition method.
Returns
-------
coal_groups : two-dimensional list
Groups of correlated attributs compute with the selected method.
"""
n_total = 2 ** X.shape[1] - 1
n_rate = int(np.round(n_total * rate, 0))
coal_groups, alpha = find_alpha_rate(
coal_function=lambda X_, threshold: remove_inclusions(
grouping_function(X_, threshold, reverse)
),
n_rate=n_rate,
X=X,
)
return coal_groups
def compute_instance_coal_inf(raw_instance_inf, columns, relevant_groups):
"""
Compute the influence of each attribut for one instance, based on the coalitional method.
Attributs can overlap in groups.
Parameters
----------
raw_instance_inf : dict {tuple : float}
Influence of each group of attributs for one instance.
columns : list
Names of attributs in the dataset.
relevant_groups : list
Groups of attributs defined by the coalition method.
Returns
-------
influences : dict {string : float}
Influence of each attribut for the instance. Key is the name of the attributs, value is the numeric influence.
"""
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_coalitional_influences(
raw_influences, X, relevant_groups, progression_bar=True
):
"""Coalitional method for all instances, when attributs overlap in groups.
Parameters
----------
raw_influences : dict {int : dict {tuple : float}}
Influence of each group of attributs for all instances.
X : pandas.DataFrame
The training input samples.
relevant_groups : list
Groups of attributs defined by the coalition method.
progression_bar : boolean, default=True
If True, progression bar are shown during computing explanations
Returns
-------
coalitional_influences : pandas.DataFrame
Influences for each attributs and each instances in the dataset.
"""
coalitional_influences = pd.DataFrame(columns=X.columns)
for instance in tqdm(
X.index, desc="Coalitional influences", disable=not progression_bar
):
raw_infs = raw_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 coalitional_method(
X,
y,
model,
rate,
problem_type,
fvoid=None,
look_at=None,
method="spearman",
reverse=False,
complexity=False,
scaler=False,
progression_bar=True,
):
"""
Compute the influences based on the method in parameters.
Parameters
----------
X : pandas.DataFrame
The training input samples.
y : pandas.DataFrame
The target values (class labels in classification, real numbers in regression).
model : pandas.DataFrame
Model to train and explain.
rate : float
Number to use for computing coalitional groups.
problem_type : {"classification", "regression"}
Type of machine learning problem.
fvoid : float, default=None
Prediction when all attributs are unknown. If None, the default value is used (expected value for each class for classification, mean label for regression).
look_at : int, default=None
Class to look at when computing influences in case of classification problem.
If None, prediction is used.
method : {"pca", "spearman", "vif"}, default="spearman"
Name of the coalition method to compute attributs groups.
reverse : boolean, default=False
Type of method to use for Spearman and VIF coalition method.
complexity : boolean, default=False
Approach to calculating the threshold for coalition methods.
If False, rate parameter is use as alpha-threshold.
If True, rate is use as complexity rate to compute the alpha-threshold.
scaler : boolean, default=False
If True, a Standard Scaler is apply to data before compute PCA coalitional method.
progression_bar : boolean, default=True
If True, progression bar are shown during computing explanations
Returns
-------
coalition_influences : two-dimensional list
Influences for each attributs and each instances in the dataset.
"""
methods = {"pca": pca_grouping, "spearman": spearman_grouping, "vif": vif_grouping}
if method not in methods.keys():
sys.stderr.write("ERROR: Invalid method.\n")
return
if X.shape[1] == 1:
groups = [[0]]
else:
if method == "pca" and scaler:
X = StandardScaler().fit_transform(X)
if complexity:
groups = complexity_coal_groups(X, rate, methods[method], reverse)
else:
groups = methods[method](X, rate, reverse)
subgroups = compute_subgroups_correlation(groups) + [[]]
pretrained_models = train_models(
model, X, y, subgroups, problem_type, fvoid, progression_bar
)
raw_groups_influences = explain_groups_w_retrain(
pretrained_models, X, problem_type, look_at, progression_bar
)
coalition_influences = compute_coalitional_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 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_coalitional_influences(
raw_groups_influences, X, groups, progression_bar
)
return influences