-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
test_tabular.py
212 lines (146 loc) · 6.47 KB
/
test_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
""" This file contains tests for the Tabular maskers.
"""
import tempfile
import numpy as np
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPRegressor
import shap
def test_serialization_independent_masker_dataframe():
""" Test the serialization of an Independent masker based on a data frame.
"""
X, _ = shap.datasets.california(n_points=500)
# initialize independent masker
original_independent_masker = shap.maskers.Independent(X)
with tempfile.TemporaryFile() as temp_serialization_file:
# serialize independent masker
original_independent_masker.save(temp_serialization_file)
temp_serialization_file.seek(0)
# deserialize masker
new_independent_masker = shap.maskers.Independent.load(temp_serialization_file)
mask = np.ones(X.shape[1]).astype(int)
mask[0] = 0
mask[4] = 0
# comparing masked values
assert np.array_equal(original_independent_masker(mask, X[:1].values[0])[1], new_independent_masker(mask, X[:1].values[0])[1])
def test_serialization_independent_masker_numpy():
""" Test the serialization of an Independent masker based on a numpy array.
"""
X, _ = shap.datasets.california(n_points=500)
X = X.values
# initialize independent masker
original_independent_masker = shap.maskers.Independent(X)
with tempfile.TemporaryFile() as temp_serialization_file:
# serialize independent masker
original_independent_masker.save(temp_serialization_file)
temp_serialization_file.seek(0)
# deserialize masker
new_independent_masker = shap.maskers.Independent.load(temp_serialization_file)
mask = np.ones(X.shape[1]).astype(int)
mask[0] = 0
mask[4] = 0
# comparing masked values
assert np.array_equal(original_independent_masker(mask, X[0])[0], new_independent_masker(mask, X[0])[0])
def test_serialization_partion_masker_dataframe():
""" Test the serialization of a Partition masker based on a DataFrame.
"""
X, _ = shap.datasets.california(n_points=500)
# initialize partition masker
original_partition_masker = shap.maskers.Partition(X)
with tempfile.TemporaryFile() as temp_serialization_file:
# serialize partition masker
original_partition_masker.save(temp_serialization_file)
temp_serialization_file.seek(0)
# deserialize masker
new_partition_masker = shap.maskers.Partition.load(temp_serialization_file)
mask = np.ones(X.shape[1]).astype(int)
mask[0] = 0
mask[4] = 0
# comparing masked values
assert np.array_equal(original_partition_masker(mask, X[:1].values[0])[1], new_partition_masker(mask, X[:1].values[0])[1])
def test_serialization_partion_masker_numpy():
""" Test the serialization of a Partition masker based on a numpy array.
"""
X, _ = shap.datasets.california(n_points=500)
X = X.values
# initialize partition masker
original_partition_masker = shap.maskers.Partition(X)
with tempfile.TemporaryFile() as temp_serialization_file:
# serialize partition masker
original_partition_masker.save(temp_serialization_file)
temp_serialization_file.seek(0)
# deserialize masker
new_partition_masker = shap.maskers.Partition.load(temp_serialization_file)
mask = np.ones(X.shape[1]).astype(int)
mask[0] = 0
mask[4] = 0
# comparing masked values
assert np.array_equal(original_partition_masker(mask, X[0])[0], new_partition_masker(mask, X[0])[0])
def test_serialization_impute_masker_dataframe():
""" Test the serialization of a Partition masker based on a DataFrame.
"""
X, _ = shap.datasets.california(n_points=500)
# initialize partition masker
original_partition_masker = shap.maskers.Impute(X)
with tempfile.TemporaryFile() as temp_serialization_file:
# serialize partition masker
original_partition_masker.save(temp_serialization_file)
temp_serialization_file.seek(0)
# deserialize masker
new_partition_masker = shap.maskers.Impute.load(temp_serialization_file)
mask = np.ones(X.shape[1]).astype(int)
mask[0] = 0
mask[4] = 0
# comparing masked values
assert np.array_equal(original_partition_masker(mask, X[:1].values[0])[1], new_partition_masker(mask, X[:1].values[0])[1])
def test_serialization_impute_masker_numpy():
""" Test the serialization of a Partition masker based on a numpy array.
"""
X, _ = shap.datasets.california(n_points=500)
X = X.values
# initialize partition masker
original_partition_masker = shap.maskers.Impute(X)
with tempfile.TemporaryFile() as temp_serialization_file:
# serialize partition masker
original_partition_masker.save(temp_serialization_file)
temp_serialization_file.seek(0)
# deserialize masker
new_partition_masker = shap.maskers.Impute.load(temp_serialization_file)
mask = np.ones(X.shape[1]).astype(int)
mask[0] = 0
mask[4] = 0
# comparing masked values
assert np.array_equal(original_partition_masker(mask, X[0])[0], new_partition_masker(mask, X[0])[0])
def test_imputation():
# toy data
x = np.full((5, 5), np.arange(1,6)).T
methods = ["linear", "mean", "median", "most_frequent", "knn"]
# toy background data
bckg = np.full((5, 5), np.arange(1,6)).T
for method in methods:
# toy sample to impute
x = np.arange(1, 6)
masker = shap.maskers.Impute(np.full((1,5), 1), method=method)
# only mask the second value
mask = np.ones_like(bckg[0])
mask[1] = 0
# masker should impute the original value (toy data is predictable)
imputed = masker(mask.astype(bool), x)
assert np.all(x == imputed)
def test_imputation_workflow():
# toy data
X, y = make_regression(n_samples=100)
X_train, X_test, y_train, y_test = train_test_split(X,
y,
train_size = 0.75)
# train toy model
model = MLPRegressor()
model.fit(X_train, y_train)
model.score(X_test, y_test)
background = shap.maskers.Impute(X_train)
# TypeError here prior to PR #3379
explainer = shap.Explainer(model.predict, masker=background)
shap_values = explainer(X_test)
shap.Explanation(shap_values.values,
shap_values.base_values,
shap_values.data)