-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
_tabular.py
319 lines (251 loc) · 13.5 KB
/
_tabular.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
import logging
import numpy as np
import pandas as pd
from numba import njit
from .. import utils
from .._serializable import Deserializer, Serializer
from ..utils import MaskedModel
from ..utils._exceptions import DimensionError, InvalidClusteringError
from ._masker import Masker
log = logging.getLogger('shap')
class Tabular(Masker):
"""A common base class for Independent and Partition."""
def __init__(self, data, max_samples=100, clustering=None):
"""This masks out tabular features by integrating over the given background dataset.
Parameters
----------
data : np.array, pandas.DataFrame
The background dataset that is used for masking.
max_samples : int
The maximum number of samples to use from the passed background data. If data has more
than max_samples then shap.utils.sample is used to subsample the dataset. The number of
samples coming out of the masker (to be integrated over) matches the number of samples in
the background dataset. This means larger background dataset cause longer runtimes. Normally
about 1, 10, 100, or 1000 background samples are reasonable choices.
clustering : string or None (default) or numpy.ndarray
The distance metric to use for creating the clustering of the features. The
distance function can be any valid scipy.spatial.distance.pdist's metric argument.
However we suggest using 'correlation' in most cases. The full list of options is
`braycurtis`, `canberra`, `chebyshev`, `cityblock`, `correlation`, `cosine`, `dice`,
`euclidean`, `hamming`, `jaccard`, `jensenshannon`, `kulsinski`, `mahalanobis`,
`matching`, `minkowski`, `rogerstanimoto`, `russellrao`, `seuclidean`,
`sokalmichener`, `sokalsneath`, `sqeuclidean`, `yule`. These are all
the options from scipy.spatial.distance.pdist's metric argument.
"""
self.output_dataframe = False
if isinstance(data, pd.DataFrame):
self.feature_names = data.columns
data = data.values
self.output_dataframe = True
if isinstance(data, dict) and "mean" in data:
self.mean = data.get("mean", None)
self.cov = data.get("cov", None)
data = np.expand_dims(data["mean"], 0)
if hasattr(data, "shape") and data.shape[0] > max_samples:
data = utils.sample(data, max_samples)
self.data = data
self.clustering = clustering
self.max_samples = max_samples
# # warn users about large background data sets
# if self.data.shape[0] > 100:
# log.warning("Using " + str(self.data.shape[0]) + " background data samples could cause slower " +
# "run times. Consider shap.utils.sample(data, K) to summarize the background using only K samples.")
# compute the clustering of the data
if clustering is not None:
if isinstance(clustering, str):
self.clustering = utils.hclust(data, metric=clustering)
elif isinstance(clustering, np.ndarray):
self.clustering = clustering
else:
raise InvalidClusteringError(
"Unknown clustering given! Make sure you pass a distance metric as a string, or a clustering as a numpy.ndarray."
)
else:
self.clustering = None
# self._last_mask = np.zeros(self.data.shape[1], dtype=bool)
self._masked_data = data.copy()
self._last_mask = np.zeros(data.shape[1], dtype=bool)
self.shape = self.data.shape
self.supports_delta_masking = True
# self._last_x = None
# self._data_variance = np.ones(self.data.shape, dtype=bool)
# this is property that allows callers to check what rows actually changed since last time.
# self.changed_rows = np.ones(self.data.shape[0], dtype=bool)
def __call__(self, mask, x):
mask = self._standardize_mask(mask, x)
# make sure we are given a single sample
if len(x.shape) != 1 or x.shape[0] != self.data.shape[1]:
raise DimensionError("The input passed for tabular masking does not match the background data shape!")
# if mask is an array of integers then we are doing delta masking
if np.issubdtype(mask.dtype, np.integer):
variants = ~self.invariants(x)
curr_delta_inds = np.zeros(len(mask), dtype=int)
num_masks = (mask >= 0).sum()
varying_rows_out = np.zeros((num_masks, self.shape[0]), dtype=bool)
masked_inputs_out = np.zeros((num_masks * self.shape[0], self.shape[1]))
self._last_mask[:] = False
self._masked_data[:] = self.data
_delta_masking(
mask, x, curr_delta_inds,
varying_rows_out, self._masked_data, self._last_mask, self. data, variants,
masked_inputs_out, MaskedModel.delta_mask_noop_value
)
if self.output_dataframe:
return (pd.DataFrame(masked_inputs_out, columns=self.feature_names),), varying_rows_out
return (masked_inputs_out,), varying_rows_out
# otherwise we update the whole set of masked data for a single sample
self._masked_data[:] = x * mask + self.data * np.invert(mask)
self._last_mask[:] = mask
if self.output_dataframe:
return pd.DataFrame(self._masked_data, columns=self.feature_names)
return (self._masked_data,)
# def reset_delta_masking(self):
# """ This resets the masker back to all zeros when delta masking.
# Note that the presence of this function also denotes that we support delta masking.
# """
# self._masked_data[:] = self.data
# self._last_mask[:] = False
def invariants(self, x):
"""This returns a mask of which features change when we mask them.
This optional masking method allows explainers to avoid re-evaluating the model when
the features that would have been masked are all invariant.
"""
# make sure we got valid data
if x.shape != self.data.shape[1:]:
raise DimensionError(
"The passed data does not match the background shape expected by the masker! The data of shape " + \
str(x.shape) + " was passed while the masker expected data of shape " + str(self.data.shape[1:]) + "."
)
return np.isclose(x, self.data)
def save(self, out_file):
"""Write a Tabular masker to a file stream."""
super().save(out_file)
# Increment the version number when the encoding changes!
with Serializer(out_file, "shap.maskers.Tabular", version=0) as s:
# save the data in the format it was given to us
if self.output_dataframe:
s.save("data", pd.DataFrame(self.data, columns=self.feature_names))
elif getattr(self, "mean", None) is not None:
s.save("data", (self.mean, self.cov))
else:
s.save("data", self.data)
s.save("max_samples", self.max_samples)
s.save("clustering", self.clustering)
@classmethod
def load(cls, in_file, instantiate=True):
"""Load a Tabular masker from a file stream."""
if instantiate:
return cls._instantiated_load(in_file)
kwargs = super().load(in_file, instantiate=False)
with Deserializer(in_file, "shap.maskers.Tabular", min_version=0, max_version=0) as s:
kwargs["data"] = s.load("data")
kwargs["max_samples"] = s.load("max_samples")
kwargs["clustering"] = s.load("clustering")
return kwargs
@njit
def _single_delta_mask(dind, masked_inputs, last_mask, data, x, noop_code):
if dind == noop_code:
pass
elif last_mask[dind]:
masked_inputs[:, dind] = data[:, dind]
last_mask[dind] = False
else:
masked_inputs[:, dind] = x[dind]
last_mask[dind] = True
@njit
def _delta_masking(masks, x, curr_delta_inds, varying_rows_out,
masked_inputs_tmp, last_mask, data, variants, masked_inputs_out, noop_code):
"""Implements the special (high speed) delta masking API that only flips the positions we need to.
Note that we attempt to avoid doing any allocation inside this function for speed reasons.
"""
dpos = 0
i = -1
masks_pos = 0
output_pos = 0
N = masked_inputs_tmp.shape[0]
while masks_pos < len(masks):
i += 1
# update the tmp masked inputs array
dpos = 0
curr_delta_inds[0] = masks[masks_pos]
while curr_delta_inds[dpos] < 0: # negative values mean keep going
curr_delta_inds[dpos] = -curr_delta_inds[dpos] - 1 # -value + 1 is the original index that needs flipped
_single_delta_mask(curr_delta_inds[dpos], masked_inputs_tmp, last_mask, data, x, noop_code)
dpos += 1
curr_delta_inds[dpos] = masks[masks_pos + dpos]
_single_delta_mask(curr_delta_inds[dpos], masked_inputs_tmp, last_mask, data, x, noop_code)
# copy the tmp masked inputs array to the output
masked_inputs_out[output_pos:output_pos+N] = masked_inputs_tmp
masks_pos += dpos + 1
# mark which rows have been updated, so we can only evaluate the model on the rows we need to
if i == 0:
varying_rows_out[i, :] = True
else:
# only one column was changed
if dpos == 0:
varying_rows_out[i, :] = variants[:, curr_delta_inds[dpos]]
# more than one column was changed
else:
varying_rows_out[i, :] = np.sum(variants[:, curr_delta_inds[:dpos+1]], axis=1) > 0
output_pos += N
class Independent(Tabular):
"""This masks out tabular features by integrating over the given background dataset."""
def __init__(self, data, max_samples=100):
"""Build a Independent masker with the given background data.
Parameters
----------
data : numpy.ndarray, pandas.DataFrame
The background dataset that is used for masking.
max_samples : int
The maximum number of samples to use from the passed background data. If data has more
than max_samples then shap.utils.sample is used to subsample the dataset. The number of
samples coming out of the masker (to be integrated over) matches the number of samples in
the background dataset. This means larger background dataset cause longer runtimes. Normally
about 1, 10, 100, or 1000 background samples are reasonable choices.
"""
super().__init__(data, max_samples=max_samples, clustering=None)
class Partition(Tabular):
"""This masks out tabular features by integrating over the given background dataset.
Unlike Independent, Partition respects a hierarchical structure of the data.
"""
def __init__(self, data, max_samples=100, clustering="correlation"):
"""Build a Partition masker with the given background data and clustering.
Parameters
----------
data : numpy.ndarray, pandas.DataFrame
The background dataset that is used for masking.
max_samples : int
The maximum number of samples to use from the passed background data. If data has more
than max_samples then shap.utils.sample is used to subsample the dataset. The number of
samples coming out of the masker (to be integrated over) matches the number of samples in
the background dataset. This means larger background dataset cause longer runtimes. Normally
about 1, 10, 100, or 1000 background samples are reasonable choices.
clustering : string or numpy.ndarray
If a string, then this is the distance metric to use for creating the clustering of
the features. The distance function can be any valid scipy.spatial.distance.pdist's metric
argument. However we suggest using 'correlation' in most cases. The full list of options is
`braycurtis`, `canberra`, `chebyshev`, `cityblock`, `correlation`, `cosine`, `dice`,
`euclidean`, `hamming`, `jaccard`, `jensenshannon`, `kulsinski`, `mahalanobis`,
`matching`, `minkowski`, `rogerstanimoto`, `russellrao`, `seuclidean`,
`sokalmichener`, `sokalsneath`, `sqeuclidean`, `yule`. These are all
the options from scipy.spatial.distance.pdist's metric argument.
If an array, then this is assumed to be the clustering of the features.
"""
super().__init__(data, max_samples=max_samples, clustering=clustering)
class Impute(Masker): # we should inherit from Tabular once we add support for arbitrary masking
"""This imputes the values of missing features using the values of the observed features.
Unlike Independent, Gaussian imputes missing values based on correlations with observed data points.
"""
def __init__(self, data, method="linear"):
"""Build a Partition masker with the given background data and clustering.
Parameters
----------
data : numpy.ndarray, pandas.DataFrame or {"mean: numpy.ndarray, "cov": numpy.ndarray} dictionary
The background dataset that is used for masking.
"""
if data is dict and "mean" in data:
self.mean = data.get("mean", None)
self.cov = data.get("cov", None)
data = np.expand_dims(data["mean"], 0)
self.data = data
self.method = method