-
Notifications
You must be signed in to change notification settings - Fork 1
/
algorithms.py
419 lines (339 loc) · 17.2 KB
/
algorithms.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
import numpy as np
from numba import njit
import kernel_methods
import sobol_sphere
@njit
def mask_dataset(mask, X_background, X_foreground):
n_samples = mask.shape[0]
n_features = mask.shape[1]
n_foreground = X_foreground.shape[0]
n_background = X_background.shape[0]
masked_dataset = np.zeros(
(n_foreground, n_samples, n_background, n_features))
for foreground_idx in range(n_foreground):
for sample_idx in range(n_samples):
for background_idx in range(n_background):
masked_dataset[foreground_idx][sample_idx][background_idx] = X_background[
background_idx]
masked_dataset[foreground_idx][sample_idx][background_idx][mask[sample_idx]] = \
X_foreground[foreground_idx][mask[sample_idx]]
return masked_dataset.reshape((n_foreground * n_samples * n_background, n_features))
# base class
class ShapleyEstimator:
def max_evals(self, num_features):
return np.iinfo(np.int64).max
class Owen(ShapleyEstimator):
def __init__(self, runs=2):
self.runs = runs
def shap_values(self, X_background, X_foreground, predict_function, n_samples):
n_features = X_background.shape[1]
phi = np.zeros((X_foreground.shape[0], n_features))
assert n_samples % self.min_samples(n_features) == 0
q_splits = (n_samples // (n_features * self.runs)) - 1
s = []
for _ in range(self.runs):
for q_num in range(q_splits + 1):
q = q_num / q_splits
s.append(np.array(np.random.binomial(1, q, n_features)))
mask = np.array(s, dtype=bool)
for j in range(n_features):
mask_j_on = mask.copy()
mask_j_on[:, j] = 1
masked_dataset_on = mask_dataset(mask_j_on, X_background, X_foreground)
mask_j_off = mask_j_on
mask_j_off[:, j] = 0
masked_dataset_off = mask_dataset(mask_j_off, X_background, X_foreground)
item = predict_function(masked_dataset_on) - predict_function(masked_dataset_off)
item = item.reshape((X_foreground.shape[0], X_background.shape[0] * mask.shape[0]))
phi[:, j] = np.mean(item, axis=1)
return phi
def min_samples(self, n_features):
return 2 * (self.runs * n_features)
class OwenHalved(ShapleyEstimator):
def __init__(self, runs=2):
self.runs = runs
def shap_values(self, X_background, X_foreground, predict_function, n_samples):
n_features = X_background.shape[1]
phi = np.zeros((X_foreground.shape[0], n_features))
assert n_samples % self.min_samples(n_features) == 0
q_splits = (n_samples // (n_features * self.runs)) - 1
s = []
for _ in range(self.runs):
for q_num in range(q_splits // 2 + 1):
q = q_num / q_splits
b = np.array(np.random.binomial(1, q, n_features))
s.append(b)
if q != 0.5:
s.append(1 - b)
mask = np.array(s, dtype=bool)
for j in range(n_features):
mask_j_on = mask.copy()
mask_j_on[:, j] = 1
masked_dataset_on = mask_dataset(mask_j_on, X_background, X_foreground)
mask_j_off = mask_j_on
mask_j_off[:, j] = 0
masked_dataset_off = mask_dataset(mask_j_off, X_background, X_foreground)
item = predict_function(masked_dataset_on) - predict_function(masked_dataset_off)
item = item.reshape((X_foreground.shape[0], X_background.shape[0] * mask.shape[0]))
phi[:, j] = np.mean(item, axis=1)
return phi
def min_samples(self, n_features):
return 2 * (self.runs * n_features)
@njit
def _accumulate_samples(phi, predictions, j, weights=None):
if weights == None:
weights = np.full(predictions.shape[1], 1 / predictions.shape[1])
for foreground_idx in range(predictions.shape[0]):
for sample_idx in range(predictions.shape[1]):
phi[foreground_idx, j[sample_idx]] += predictions[foreground_idx][
sample_idx] * weights[sample_idx]
def estimate_shap_given_permutations(X_background, X_foreground, predict_function, p, weights=None):
n_features = X_background.shape[1]
phi = np.zeros((X_foreground.shape[0], n_features))
n_permutations = p.shape[0]
mask = np.zeros((n_permutations, n_features), dtype=bool)
masked_dataset = mask_dataset(mask, X_background, X_foreground)
pred_off = predict_function(masked_dataset)
for j in p.T:
mask[range(n_permutations), j] = True
masked_dataset = mask_dataset(mask, X_background, X_foreground)
pred_on = predict_function(masked_dataset)
predictions = (pred_on - pred_off).reshape(
(X_foreground.shape[0], mask.shape[0], X_background.shape[0]))
predictions = np.mean(predictions, axis=2)
_accumulate_samples(phi, predictions, j, weights)
pred_off = pred_on
return phi
class MonteCarlo(ShapleyEstimator):
def shap_values(self, X_background, X_foreground, predict_function, n_samples):
n_features = X_background.shape[1]
assert n_samples % self.min_samples(n_features) == 0
# allowed to take 2 * samples as it reuses predictions
samples_per_feature = 2 * (n_samples // self.min_samples(n_features))
p = np.zeros((samples_per_feature, n_features), dtype=np.int64)
for i in range(samples_per_feature):
p[i] = np.random.permutation(n_features)
return estimate_shap_given_permutations(X_background, X_foreground, predict_function, p)
def min_samples(self, n_features):
return n_features + 1
def get_antithetic_permutations(n, d):
p = np.zeros((n, d), dtype=np.int64)
# Forward samples
for i in range(n // 2):
p[i] = np.random.permutation(d)
# Reverse samples
for i in range(n // 2):
p[i + n // 2] = np.flip(p[i])
return p
class MonteCarloAntithetic(ShapleyEstimator):
def shap_values(self, X_background, X_foreground, predict_function, n_samples):
n_features = X_background.shape[1]
assert n_samples % self.min_samples(n_features) == 0
# allowed to take 2 * samples as it reuses predictions
samples_per_feature = 2 * (n_samples // (n_features + 1))
p = get_antithetic_permutations(samples_per_feature, n_features)
return estimate_shap_given_permutations(X_background, X_foreground, predict_function, p)
def min_samples(self, n_features):
return 2 * (n_features + 1)
class BayesianQuadrature(ShapleyEstimator):
def __init__(self, kernel):
self.kernel = kernel
def shap_values(self, X_background, X_foreground, predict_function, n_samples):
n_features = X_background.shape[1]
samples_per_feature = 2 * (n_samples // (n_features + 1))
p = np.zeros((samples_per_feature, n_features), dtype=np.int64)
for i in range(samples_per_feature):
p[i] = np.random.permutation(n_features)
weights = kernel_methods.compute_bayesian_weights(p, self.kernel)
return estimate_shap_given_permutations(X_background, X_foreground, predict_function, p,
weights)
def min_samples(self, n_features):
return n_features + 1
class SequentialBayesianQuadrature(ShapleyEstimator):
def __init__(self, kernel, num_trials=25):
self.kernel = kernel
self.num_trials = num_trials
def shap_values(self, X_background, X_foreground, predict_function, n_samples):
n_features = X_background.shape[1]
assert n_samples % self.min_samples(n_features) == 0
# allowed to take 2 * samples as it reuses predictions
samples_per_feature = 2 * (n_samples // (n_features + 1))
p, w = kernel_methods.sequential_bayesian_quadrature(samples_per_feature, n_features,
self.kernel, self.num_trials)
return estimate_shap_given_permutations(X_background, X_foreground, predict_function, p,
w)
def min_samples(self, n_features):
return n_features + 1
def max_evals(self, n_features):
return (n_features + 1) * 100
# sample with l ones and i off
def draw_castro_stratified_samples(n_samples, n_features, i, l):
mask = np.zeros((n_samples, n_features - 1), dtype=bool)
mask[:, 0:l] = True
for sample_idx in range(n_samples):
mask[sample_idx] = np.random.permutation(mask[sample_idx])
mask = np.insert(mask, i, False, axis=1)
return mask
def allocate_samples(variance, n_samples):
variance_flat = variance.flatten()
samples_out = np.zeros_like(variance_flat, dtype=int)
sum_variance = variance_flat.sum()
for i in range(len(variance_flat)):
if (sum_variance > 0.0):
samples_out[i] = np.round(variance_flat[i] / sum_variance * n_samples)
sum_variance -= variance_flat[i]
n_samples -= samples_out[i]
return samples_out.reshape(variance.shape)
class Stratified(ShapleyEstimator):
def shap_values(self, X_background, X_foreground, predict_function, n_samples):
n_features = X_background.shape[1]
phi = np.zeros((X_foreground.shape[0], n_features))
assert n_samples % self.min_samples(n_features) == 0
m_i_l_exp = n_samples // self.min_samples(n_features)
for foreground_idx in range(X_foreground.shape[0]):
stratified_phi = np.zeros((n_features, n_features))
stratified_phi_count = np.zeros((n_features, n_features))
stratified_variance = np.zeros((n_features, n_features))
# Uniformly sample strata
for i in range(n_features):
for l in range(0, n_features):
mask_off = draw_castro_stratified_samples(m_i_l_exp, n_features, i, l)
masked_dataset_off = mask_dataset(mask_off, X_background, X_foreground[
foreground_idx:foreground_idx + 1])
pred_off = predict_function(masked_dataset_off)
mask_on = mask_off
mask_on[:, i] = True
masked_dataset_on = mask_dataset(mask_on, X_background, X_foreground[
foreground_idx:foreground_idx + 1])
pred_on = predict_function(masked_dataset_on)
# Average background samples
y = (pred_on - pred_off).reshape(m_i_l_exp, X_background.shape[0]).mean(axis=1)
stratified_phi[i, l] += y.sum()
stratified_phi_count[i, l] += m_i_l_exp
stratified_variance[i, l] = np.var(y, ddof=1)
stratified_proportional_samples = allocate_samples(stratified_variance, n_samples // 2)
# Sample highest variance regions
for i in range(n_features):
for l in range(0, n_features):
m_i_l_st = stratified_proportional_samples[i][l]
if m_i_l_st == 0:
continue
mask_off = draw_castro_stratified_samples(m_i_l_st, n_features, i, l)
masked_dataset_off = mask_dataset(mask_off, X_background, X_foreground[
foreground_idx:foreground_idx + 1])
pred_off = predict_function(masked_dataset_off)
mask_on = mask_off
mask_on[:, i] = True
masked_dataset_on = mask_dataset(mask_on, X_background, X_foreground[
foreground_idx:foreground_idx + 1])
pred_on = predict_function(masked_dataset_on)
# Average background samples
y = (pred_on - pred_off).reshape(m_i_l_st, X_background.shape[0]).mean(axis=1)
stratified_phi[i, l] += y.sum()
stratified_phi_count[i, l] += m_i_l_st
stratified_phi /= stratified_phi_count
phi[foreground_idx] = stratified_phi.mean(axis=1).T
return phi
def min_samples(self, n_features):
return 2 * n_features ** 2
def correlation(a, b):
a_mean = a.mean()
b_mean = b.mean()
cov = 0.0
var = 0.0
for i in range(len(a)):
cov += (a[i] - a_mean) * (b[i] - b_mean)
var += (b[i] - b_mean) * (b[i] - b_mean)
return cov / var
class ControlVariate(ShapleyEstimator):
def __init__(self, sampling_algorithm, training_samples):
self.sampling_algorithm = sampling_algorithm
self.training_samples=training_samples
def shap_values(self, X_background, X_foreground, predict_function, n_samples):
n_features = X_background.shape[1]
# Train tree model
from sklearn import tree
import shap
from sklearn.utils import resample
tree_training_X = resample(np.vstack((X_background, X_foreground)), n_samples=self.training_samples)
for i in range(n_features):
tree_training_X[:, i] = np.random.permutation(tree_training_X[:, i])
tree_training_y = predict_function(tree_training_X)
tree_model = tree.DecisionTreeRegressor().fit(tree_training_X, tree_training_y)
# estimate correlation using predictions on background set
beta = correlation(tree_model.predict(X_background), predict_function(X_background))
beta=0.05
difference_predict = lambda X: predict_function(X) - beta * tree_model.predict(X)
tree_explainer = shap.TreeExplainer(tree_model, X_background)
tree_phi = tree_explainer.shap_values(X_foreground, check_additivity=False)
phi = self.sampling_algorithm.shap_values(X_background, X_foreground, difference_predict,
n_samples)
return phi + tree_phi * beta
def min_samples(self, n_features):
return self.sampling_algorithm.min_samples(n_features)
class KernelHerding(ShapleyEstimator):
def __init__(self, kernel, max_trials=25):
self.kernel = kernel
self.max_trials = max_trials
def shap_values(self, X_background, X_foreground, predict_function, n_samples):
n_features = X_background.shape[1]
assert n_samples % self.min_samples(n_features) == 0
samples_per_feature = 2 * (n_samples // self.min_samples(n_features))
p = kernel_methods.kernel_herding(samples_per_feature, n_features, self.kernel,
self.max_trials)
return estimate_shap_given_permutations(X_background, X_foreground, predict_function, p)
def min_samples(self, n_features):
return n_features + 1
def _sample_sphere(ndim):
vec = np.random.randn(ndim)
vec /= np.linalg.norm(vec, axis=0)
return vec
def get_orthogonal_vectors(n_orthogonal_samples, n_features):
A = np.zeros((n_orthogonal_samples, n_features))
n = np.ones(n_features)
n /= np.linalg.norm(n)
for i in range(n_orthogonal_samples):
A[i] = _sample_sphere(n_features)
A[i, :] -= np.dot(n, A[i, :]) * n
for k in range(i):
A[i, :] -= np.dot(A[k, :], A[i, :]) * A[k, :]
A[i, :] = A[i, :] / np.linalg.norm(A[i, :])
return A
def _gram_schmidt_permutations(n_orthogonal_samples, n_features):
assert n_orthogonal_samples < n_features
A = get_orthogonal_vectors(n_orthogonal_samples, n_features)
p = np.zeros(A.shape, dtype=np.int64)
for i in range(n_orthogonal_samples):
p[i] = np.argsort(A[i])
return p
def _orthogonal_permutations(n_samples, n_features):
p = np.zeros((n_samples, n_features), dtype=np.int64)
k = n_features - 1
i = 0
while i < n_samples // 2:
n_orthogonal_samples = min(min(k, n_features - 1), n_samples // 2 - i)
p[i:i + n_orthogonal_samples] = _gram_schmidt_permutations(n_orthogonal_samples, n_features
)
i += n_orthogonal_samples
# Reverse samples
for i in range(n_samples // 2):
p[i + n_samples // 2] = np.flip(p[i])
return p
class OrthogonalSphericalCodes(ShapleyEstimator):
def shap_values(self, X_background, X_foreground, predict_function, n_samples):
n_features = X_background.shape[1]
assert n_samples % self.min_samples(n_features) == 0
samples_per_feature = 2 * (n_samples // self.min_samples(n_features))
p = _orthogonal_permutations(samples_per_feature, n_features)
return estimate_shap_given_permutations(X_background, X_foreground, predict_function, p)
def min_samples(self, n_features):
return n_features + 1
class Sobol(ShapleyEstimator):
def shap_values(self, X_background, X_foreground, predict_function, n_samples):
n_features = X_background.shape[1]
assert n_samples % self.min_samples(n_features) == 0
samples_per_feature = 2 * (n_samples // self.min_samples(n_features))
p = sobol_sphere.sobol_permutations(samples_per_feature, n_features)
return estimate_shap_given_permutations(X_background, X_foreground, predict_function, p)
def min_samples(self, n_features):
return n_features + 1