-
Notifications
You must be signed in to change notification settings - Fork 67
/
DShap.py
658 lines (625 loc) · 28.5 KB
/
DShap.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#______________________________________PEP8____________________________________
#_______________________________________________________________________
import matplotlib
matplotlib.use('Agg')
import numpy as np
import os
import tensorflow as tf
import sys
from shap_utils import *
from Shapley import ShapNN
from scipy.stats import spearmanr
import shutil
from sklearn.base import clone
import matplotlib.pyplot as plt
import warnings
import itertools
import inspect
import _pickle as pkl
from sklearn.metrics import f1_score, roc_auc_score
class DShap(object):
def __init__(self, X, y, X_test, y_test, num_test, sources=None,
sample_weight=None, directory=None, problem='classification',
model_family='logistic', metric='accuracy', seed=None,
overwrite=False,
**kwargs):
"""
Args:
X: Data covariates
y: Data labels
X_test: Test+Held-out covariates
y_test: Test+Held-out labels
sources: An array or dictionary assiging each point to its group.
If None, evey points gets its individual value.
samples_weights: Weight of train samples in the loss function
(for models where weighted training method is enabled.)
num_test: Number of data points used for evaluation metric.
directory: Directory to save results and figures.
problem: "Classification" or "Regression"(Not implemented yet.)
model_family: The model family used for learning algorithm
metric: Evaluation metric
seed: Random seed. When running parallel monte-carlo samples,
we initialize each with a different seed to prevent getting
same permutations.
overwrite: Delete existing data and start computations from
scratch
**kwargs: Arguments of the model
"""
if seed is not None:
np.random.seed(seed)
tf.random.set_random_seed(seed)
self.problem = problem
self.model_family = model_family
self.metric = metric
self.directory = directory
self.hidden_units = kwargs.get('hidden_layer_sizes', [])
if self.model_family is 'logistic':
self.hidden_units = []
if self.directory is not None:
if overwrite and os.path.exists(directory):
tf.gfile.DeleteRecursively(directory)
if not os.path.exists(directory):
os.makedirs(directory)
os.makedirs(os.path.join(directory, 'weights'))
os.makedirs(os.path.join(directory, 'plots'))
self._initialize_instance(X, y, X_test, y_test, num_test,
sources, sample_weight)
if len(set(self.y)) > 2:
assert self.metric != 'f1', 'Invalid metric for multiclass!'
assert self.metric != 'auc', 'Invalid metric for multiclass!'
is_regression = (np.mean(self.y//1 == self.y) != 1)
is_regression = is_regression or isinstance(self.y[0], np.float32)
self.is_regression = is_regression or isinstance(self.y[0], np.float64)
if self.is_regression:
warnings.warn("Regression problem is no implemented.")
self.model = return_model(self.model_family, **kwargs)
self.random_score = self.init_score(self.metric)
def _initialize_instance(self, X, y, X_test, y_test, num_test,
sources=None, sample_weight=None):
"""Loads or creates sets of data."""
if sources is None:
sources = {i:np.array([i]) for i in range(len(X))}
elif not isinstance(sources, dict):
sources = {i:np.where(sources==i)[0] for i in set(sources)}
data_dir = os.path.join(self.directory, 'data.pkl')
if os.path.exists(data_dir):
self._load_dataset(data_dir)
else:
self.X_heldout = X_test[:-num_test]
self.y_heldout = y_test[:-num_test]
self.X_test = X_test[-num_test:]
self.y_test = y_test[-num_test:]
self.X, self.y, self.sources = X, y, sources
self.sample_weight = sample_weight
data_dic = {'X': self.X, 'y': self.y, 'X_test': self.X_test,
'y_test': self.y_test, 'X_heldout': self.X_heldout,
'y_heldout':self.y_heldout, 'sources': self.sources}
if sample_weight is not None:
data_dic['sample_weight'] = sample_weight
warnings.warn("Sample weight not implemented for G-Shapley")
pkl.dump(data_dic, open(data_dir, 'wb'))
loo_dir = os.path.join(self.directory, 'loo.pkl')
self.vals_loo = None
if os.path.exists(loo_dir):
self.vals_loo = pkl.load(open(loo_dir, 'rb'))['loo']
n_sources = len(self.X) if self.sources is None else len(self.sources)
n_points = len(self.X)
self.tmc_number, self.g_number = self._which_parallel(self.directory)
self._create_results_placeholder(
self.directory, self.tmc_number, self.g_number,
n_points, n_sources, self.model_family)
def _create_results_placeholder(self, directory, tmc_number, g_number,
n_points, n_sources, model_family):
tmc_dir = os.path.join(
directory,
'mem_tmc_{}.pkl'.format(tmc_number.zfill(4))
)
g_dir = os.path.join(
directory,
'mem_g_{}.pkl'.format(g_number.zfill(4))
)
self.mem_tmc = np.zeros((0, n_points))
self.mem_g = np.zeros((0, n_points))
self.idxs_tmc = np.zeros((0, n_sources), int)
self.idxs_g = np.zeros((0, n_sources), int)
pkl.dump({'mem_tmc': self.mem_tmc, 'idxs_tmc': self.idxs_tmc},
open(tmc_dir, 'wb'))
if model_family not in ['logistic', 'NN']:
return
pkl.dump({'mem_g': self.mem_g, 'idxs_g': self.idxs_g},
open(g_dir, 'wb'))
def _load_dataset(self, data_dir):
'''Load the different sets of data if already exists.'''
data_dic = pkl.load(open(data_dir, 'rb'))
self.X_heldout = data_dic['X_heldout']
self.y_heldout = data_dic['y_heldout']
self.X_test = data_dic['X_test']
self.y_test = data_dic['y_test']
self.X = data_dic['X']
self.y = data_dic['y']
self.sources = data_dic['sources']
if 'sample_weight' in data_dic.keys():
self.sample_weight = data_dic['sample_weight']
else:
self.sample_weight = None
def _which_parallel(self, directory):
'''Prevent conflict with parallel runs.'''
previous_results = os.listdir(directory)
tmc_nmbrs = [int(name.split('.')[-2].split('_')[-1])
for name in previous_results if 'mem_tmc' in name]
g_nmbrs = [int(name.split('.')[-2].split('_')[-1])
for name in previous_results if 'mem_g' in name]
tmc_number = str(np.max(tmc_nmbrs) + 1) if len(tmc_nmbrs) else '0'
g_number = str(np.max(g_nmbrs) + 1) if len(g_nmbrs) else '0'
return tmc_number, g_number
def init_score(self, metric):
""" Gives the value of an initial untrained model."""
if metric == 'accuracy':
hist = np.bincount(self.y_test).astype(float)/len(self.y_test)
return np.max(hist)
if metric == 'f1':
rnd_f1s = []
for _ in range(1000):
rnd_y = np.random.permutation(self.y_test)
rnd_f1s.append(f1_score(self.y_test, rnd_y))
return np.mean(rnd_f1s)
if metric == 'auc':
return 0.5
random_scores = []
for _ in range(100):
rnd_y = np.random.permutation(self.y)
if self.sample_weight is None:
self.model.fit(self.X, rnd_y)
else:
self.model.fit(self.X, rnd_y,
sample_weight=self.sample_weight)
random_scores.append(self.value(self.model, metric))
return np.mean(random_scores)
def value(self, model, metric=None, X=None, y=None):
"""Computes the values of the given model.
Args:
model: The model to be evaluated.
metric: Valuation metric. If None the object's default
metric is used.
X: Covariates, valuation is performed on a data
different from test set.
y: Labels, if valuation is performed on a data
different from test set.
"""
if metric is None:
metric = self.metric
if X is None:
X = self.X_test
if y is None:
y = self.y_test
if inspect.isfunction(metric):
return metric(model, X, y)
if metric == 'accuracy':
return model.score(X, y)
if metric == 'f1':
assert len(set(y)) == 2, 'Data has to be binary for f1 metric.'
return f1_score(y, model.predict(X))
if metric == 'auc':
assert len(set(y)) == 2, 'Data has to be binary for auc metric.'
return my_auc_score(model, X, y)
if metric == 'xe':
return my_xe_score(model, X, y)
raise ValueError('Invalid metric!')
def run(self, save_every, err, tolerance=0.01, g_run=True, loo_run=True):
"""Calculates data sources(points) values.
Args:
save_every: save marginal contrivbutions every n iterations.
err: stopping criteria.
tolerance: Truncation tolerance. If None, it's computed.
g_run: If True, computes G-Shapley values.
loo_run: If True, computes and saves leave-one-out scores.
"""
if loo_run:
try:
len(self.vals_loo)
except:
self.vals_loo = self._calculate_loo_vals(sources=self.sources)
self.save_results(overwrite=True)
print('LOO values calculated!')
tmc_run = True
g_run = g_run and self.model_family in ['logistic', 'NN']
while tmc_run or g_run:
if g_run:
if error(self.mem_g) < err:
g_run = False
else:
self._g_shap(save_every, sources=self.sources)
self.vals_g = np.mean(self.mem_g, 0)
if tmc_run:
if error(self.mem_tmc) < err:
tmc_run = False
else:
self._tmc_shap(
save_every,
tolerance=tolerance,
sources=self.sources
)
self.vals_tmc = np.mean(self.mem_tmc, 0)
if self.directory is not None:
self.save_results()
def save_results(self, overwrite=False):
"""Saves results computed so far."""
if self.directory is None:
return
loo_dir = os.path.join(self.directory, 'loo.pkl')
if not os.path.exists(loo_dir) or overwrite:
pkl.dump({'loo': self.vals_loo}, open(loo_dir, 'wb'))
tmc_dir = os.path.join(
self.directory,
'mem_tmc_{}.pkl'.format(self.tmc_number.zfill(4))
)
g_dir = os.path.join(
self.directory,
'mem_g_{}.pkl'.format(self.g_number.zfill(4))
)
pkl.dump({'mem_tmc': self.mem_tmc, 'idxs_tmc': self.idxs_tmc},
open(tmc_dir, 'wb'))
pkl.dump({'mem_g': self.mem_g, 'idxs_g': self.idxs_g},
open(g_dir, 'wb'))
def _tmc_shap(self, iterations, tolerance=None, sources=None):
"""Runs TMC-Shapley algorithm.
Args:
iterations: Number of iterations to run.
tolerance: Truncation tolerance ratio.
sources: If values are for sources of data points rather than
individual points. In the format of an assignment array
or dict.
"""
if sources is None:
sources = {i: np.array([i]) for i in range(len(self.X))}
elif not isinstance(sources, dict):
sources = {i: np.where(sources == i)[0] for i in set(sources)}
model = self.model
try:
self.mean_score
except:
self._tol_mean_score()
if tolerance is None:
tolerance = self.tolerance
marginals, idxs = [], []
for iteration in range(iterations):
if 10*(iteration+1)/iterations % 1 == 0:
print('{} out of {} TMC_Shapley iterations.'.format(
iteration + 1, iterations))
marginals, idxs = self.one_iteration(
tolerance=tolerance,
sources=sources
)
self.mem_tmc = np.concatenate([
self.mem_tmc,
np.reshape(marginals, (1,-1))
])
self.idxs_tmc = np.concatenate([
self.idxs_tmc,
np.reshape(idxs, (1,-1))
])
def _tol_mean_score(self):
"""Computes the average performance and its error using bagging."""
scores = []
self.restart_model()
for _ in range(1):
if self.sample_weight is None:
self.model.fit(self.X, self.y)
else:
self.model.fit(self.X, self.y,
sample_weight=self.sample_weight)
for _ in range(100):
bag_idxs = np.random.choice(len(self.y_test), len(self.y_test))
scores.append(self.value(
self.model,
metric=self.metric,
X=self.X_test[bag_idxs],
y=self.y_test[bag_idxs]
))
self.tol = np.std(scores)
self.mean_score = np.mean(scores)
def one_iteration(self, tolerance, sources=None):
"""Runs one iteration of TMC-Shapley algorithm."""
if sources is None:
sources = {i: np.array([i]) for i in range(len(self.X))}
elif not isinstance(sources, dict):
sources = {i: np.where(sources == i)[0] for i in set(sources)}
idxs = np.random.permutation(len(sources))
marginal_contribs = np.zeros(len(self.X))
X_batch = np.zeros((0,) + tuple(self.X.shape[1:]))
y_batch = np.zeros(0, int)
sample_weight_batch = np.zeros(0)
truncation_counter = 0
new_score = self.random_score
for n, idx in enumerate(idxs):
old_score = new_score
X_batch = np.concatenate([X_batch, self.X[sources[idx]]])
y_batch = np.concatenate([y_batch, self.y[sources[idx]]])
if self.sample_weight is None:
sample_weight_batch = None
else:
sample_weight_batch = np.concatenate([
sample_weight_batch,
self.sample_weight[sources[idx]]
])
with warnings.catch_warnings():
warnings.simplefilter("ignore")
if (self.is_regression
or len(set(y_batch)) == len(set(self.y_test))): ##FIXIT
self.restart_model()
if sample_weight_batch is None:
self.model.fit(X_batch, y_batch)
else:
self.model.fit(
X_batch,
y_batch,
sample_weight = sample_weight_batch
)
new_score = self.value(self.model, metric=self.metric)
marginal_contribs[sources[idx]] = (new_score - old_score)
marginal_contribs[sources[idx]] /= len(sources[idx])
distance_to_full_score = np.abs(new_score - self.mean_score)
if distance_to_full_score <= tolerance * self.mean_score:
truncation_counter += 1
if truncation_counter > 5:
break
else:
truncation_counter = 0
return marginal_contribs, idxs
def restart_model(self):
try:
self.model = clone(self.model)
except:
self.model.fit(np.zeros((0,) + self.X.shape[1:]), self.y)
def _one_step_lr(self):
"""Computes the best learning rate for G-Shapley algorithm."""
if self.directory is None:
address = None
else:
address = os.path.join(self.directory, 'weights')
best_acc = 0.0
for i in np.arange(1, 5, 0.5):
model = ShapNN(
self.problem, batch_size=1, max_epochs=1,
learning_rate=10**(-i), weight_decay=0.,
validation_fraction=0, optimizer='sgd',
warm_start=False, address=address,
hidden_units=self.hidden_units)
accs = []
for _ in range(10):
model.fit(np.zeros((0, self.X.shape[-1])), self.y)
model.fit(self.X, self.y)
accs.append(model.score(self.X_test, self.y_test))
if np.mean(accs) - np.std(accs) > best_acc:
best_acc = np.mean(accs) - np.std(accs)
learning_rate = 10**(-i)
return learning_rate
def _g_shap(self, iterations, err=None, learning_rate=None, sources=None):
"""Method for running G-Shapley algorithm.
Args:
iterations: Number of iterations of the algorithm.
err: Stopping error criteria
learning_rate: Learning rate used for the algorithm. If None
calculates the best learning rate.
sources: If values are for sources of data points rather than
individual points. In the format of an assignment array
or dict.
"""
if sources is None:
sources = {i:np.array([i]) for i in range(len(self.X))}
elif not isinstance(sources, dict):
sources = {i:np.where(sources==i)[0] for i in set(sources)}
address = None
if self.directory is not None:
address = os.path.join(self.directory, 'weights')
if learning_rate is None:
try:
learning_rate = self.g_shap_lr
except AttributeError:
self.g_shap_lr = self._one_step_lr()
learning_rate = self.g_shap_lr
model = ShapNN(self.problem, batch_size=1, max_epochs=1,
learning_rate=learning_rate, weight_decay=0.,
validation_fraction=0, optimizer='sgd',
address=address, hidden_units=self.hidden_units)
for iteration in range(iterations):
model.fit(np.zeros((0, self.X.shape[-1])), self.y)
if 10 * (iteration+1) / iterations % 1 == 0:
print('{} out of {} G-Shapley iterations'.format(
iteration + 1, iterations))
marginal_contribs = np.zeros(len(sources.keys()))
model.fit(self.X, self.y, self.X_test, self.y_test,
sources=sources, metric=self.metric,
max_epochs=1, batch_size=1)
val_result = model.history['metrics']
marginal_contribs[1:] += val_result[0][1:]
marginal_contribs[1:] -= val_result[0][:-1]
individual_contribs = np.zeros(len(self.X))
for i, index in enumerate(model.history['idxs'][0]):
individual_contribs[sources[index]] += marginal_contribs[i]
individual_contribs[sources[index]] /= len(sources[index])
self.mem_g = np.concatenate(
[self.mem_g, np.reshape(individual_contribs, (1,-1))])
self.idxs_g = np.concatenate(
[self.idxs_g, np.reshape(model.history['idxs'][0], (1,-1))])
def _calculate_loo_vals(self, sources=None, metric=None):
"""Calculated leave-one-out values for the given metric.
Args:
metric: If None, it will use the objects default metric.
sources: If values are for sources of data points rather than
individual points. In the format of an assignment array
or dict.
Returns:
Leave-one-out scores
"""
if sources is None:
sources = {i:np.array([i]) for i in range(len(self.X))}
elif not isinstance(sources, dict):
sources = {i:np.where(sources==i)[0] for i in set(sources)}
print('Starting LOO score calculations!')
if metric is None:
metric = self.metric
self.restart_model()
if self.sample_weight is None:
self.model.fit(self.X, self.y)
else:
self.model.fit(self.X, self.y,
sample_weight=self.sample_weight)
baseline_value = self.value(self.model, metric=metric)
vals_loo = np.zeros(len(self.X))
for i in sources.keys():
X_batch = np.delete(self.X, sources[i], axis=0)
y_batch = np.delete(self.y, sources[i], axis=0)
if self.sample_weight is not None:
sw_batch = np.delete(self.sample_weight, sources[i], axis=0)
if self.sample_weight is None:
self.model.fit(X_batch, y_batch)
else:
self.model.fit(X_batch, y_batch, sample_weight=sw_batch)
removed_value = self.value(self.model, metric=metric)
vals_loo[sources[i]] = (baseline_value - removed_value)
vals_loo[sources[i]] /= len(sources[i])
return vals_loo
def _merge_parallel_results(self, key, max_samples=None):
"""Helper method for 'merge_results' method."""
numbers = [name.split('.')[-2].split('_')[-1]
for name in os.listdir(self.directory)
if 'mem_{}'.format(key) in name]
mem = np.zeros((0, self.X.shape[0]))
n_sources = len(self.X) if self.sources is None else len(self.sources)
idxs = np.zeros((0, n_sources), int)
vals = np.zeros(len(self.X))
counter = 0.
for number in numbers:
if max_samples is not None:
if counter > max_samples:
break
samples_dir = os.path.join(
self.directory,
'mem_{}_{}.pkl'.format(key, number)
)
print(samples_dir)
dic = pkl.load(open(samples_dir, 'rb'))
if not len(dic['mem_{}'.format(key)]):
continue
mem = np.concatenate([mem, dic['mem_{}'.format(key)]])
idxs = np.concatenate([idxs, dic['idxs_{}'.format(key)]])
counter += len(dic['mem_{}'.format(key)])
vals *= (counter - len(dic['mem_{}'.format(key)])) / counter
vals += len(dic['mem_{}'.format(key)]) / counter * np.mean(mem, 0)
os.remove(samples_dir)
merged_dir = os.path.join(
self.directory,
'mem_{}_0000.pkl'.format(key)
)
pkl.dump({'mem_{}'.format(key): mem, 'idxs_{}'.format(key): idxs},
open(merged_dir, 'wb'))
return mem, idxs, vals
def merge_results(self, max_samples=None):
"""Merge all the results from different runs.
Returns:
combined marginals, sampled indexes and values calculated
using the two algorithms. (If applicable)
"""
tmc_results = self._merge_parallel_results('tmc', max_samples)
self.marginals_tmc, self.indexes_tmc, self.values_tmc = tmc_results
if self.model_family not in ['logistic', 'NN']:
return
g_results = self._merge_parallel_results('g', max_samples)
self.marginals_g, self.indexes_g, self.values_g = g_results
def performance_plots(self, vals, name=None,
num_plot_markers=20, sources=None):
"""Plots the effect of removing valuable points.
Args:
vals: A list of different valuations of data points each
in the format of an array in the same length of the data.
name: Name of the saved plot if not None.
num_plot_markers: number of points in each plot.
sources: If values are for sources of data points rather than
individual points. In the format of an assignment array
or dict.
Returns:
Plots showing the change in performance as points are removed
from most valuable to least.
"""
plt.rcParams['figure.figsize'] = 8,8
plt.rcParams['font.size'] = 25
plt.xlabel('Fraction of train data removed (%)')
plt.ylabel('Prediction accuracy (%)', fontsize=20)
if not isinstance(vals, list) and not isinstance(vals, tuple):
vals = [vals]
if sources is None:
sources = {i:np.array([i]) for i in range(len(self.X))}
elif not isinstance(sources, dict):
sources = {i:np.where(sources==i)[0] for i in set(sources)}
vals_sources = [np.array([np.sum(val[sources[i]])
for i in range(len(sources.keys()))])
for val in vals]
if len(sources.keys()) < num_plot_markers:
num_plot_markers = len(sources.keys()) - 1
plot_points = np.arange(
0,
max(len(sources.keys()) - 10, num_plot_markers),
max(len(sources.keys())//num_plot_markers, 1)
)
perfs = [self._portion_performance(
np.argsort(vals_source)[::-1], plot_points, sources=sources)
for vals_source in vals_sources]
rnd = np.mean([self._portion_performance(
np.random.permutation(np.argsort(vals_sources[0])[::-1]),
plot_points, sources=sources) for _ in range(10)], 0)
plt.plot(plot_points/len(self.X) * 100, perfs[0] * 100,
'-', lw=5, ms=10, color='b')
if len(vals)==3:
plt.plot(plot_points/len(self.X) * 100, perfs[1] * 100,
'--', lw=5, ms=10, color='orange')
legends = ['TMC-Shapley ', 'G-Shapley ', 'LOO', 'Random']
elif len(vals)==2:
legends = ['TMC-Shapley ', 'LOO', 'Random']
else:
legends = ['TMC-Shapley ', 'Random']
plt.plot(plot_points/len(self.X) * 100, perfs[-1] * 100,
'-.', lw=5, ms=10, color='g')
plt.plot(plot_points/len(self.X) * 100, rnd * 100,
':', lw=5, ms=10, color='r')
plt.legend(legends)
if self.directory is not None and name is not None:
plt.savefig(os.path.join(
self.directory, 'plots', '{}.png'.format(name)),
bbox_inches = 'tight')
plt.close()
def _portion_performance(self, idxs, plot_points, sources=None):
"""Given a set of indexes, starts removing points from
the first elemnt and evaluates the new model after
removing each point."""
if sources is None:
sources = {i:np.array([i]) for i in range(len(self.X))}
elif not isinstance(sources, dict):
sources = {i:np.where(sources==i)[0] for i in set(sources)}
scores = []
init_score = self.random_score
for i in range(len(plot_points), 0, -1):
keep_idxs = np.concatenate([sources[idx] for idx
in idxs[plot_points[i-1]:]], -1)
X_batch, y_batch = self.X[keep_idxs], self.y[keep_idxs]
if self.sample_weight is not None:
sample_weight_batch = self.sample_weight[keep_idxs]
with warnings.catch_warnings():
warnings.simplefilter("ignore")
if (self.is_regression
or len(set(y_batch)) == len(set(self.y_test))):
self.restart_model()
if self.sample_weight is None:
self.model.fit(X_batch, y_batch)
else:
self.model.fit(X_batch, y_batch,
sample_weight=sample_weight_batch)
scores.append(self.value(
self.model,
metric=self.metric,
X=self.X_heldout,
y=self.y_heldout
))
else:
scores.append(init_score)
return np.array(scores)[::-1]