-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest_shap.py
159 lines (116 loc) · 5.61 KB
/
test_shap.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
# pylint: disable=import-error, wrong-import-position, wrong-import-order, duplicate-code, unused-import
"""SHAP explainer test suite"""
from common import *
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
import pytest
from trustyai.explainers import SHAPExplainer
from trustyai.model import feature, Model
from trustyai.utils.data_conversions import numpy_to_prediction_object
from trustyai.utils import TestModels
from trustyai.visualizations import plot
def test_no_variance_one_output():
"""Check if the explanation returned is not null"""
model = TestModels.getSumSkipModel(0)
background = np.array([[1.0, 2.0, 3.0] for _ in range(2)])
prediction_outputs = model.predictAsync(numpy_to_prediction_object(background, feature)).get()
shap_explainer = SHAPExplainer(background=background)
for i in range(2):
explanation = shap_explainer.explain(inputs=background[i], outputs=prediction_outputs[i].outputs, model=model)
for _, saliency in explanation.saliency_map().items():
for feature_importance in saliency.getPerFeatureImportance()[:-1]:
assert feature_importance.getScore() == 0.0
def test_shap_arrow():
"""Basic SHAP/Arrow test"""
np.random.seed(0)
data = pd.DataFrame(np.random.rand(101, 5))
background = data.iloc[:100]
to_explain = data.iloc[100:101]
model_weights = np.random.rand(5)
predict_function = lambda x: np.dot(x.values, model_weights)
model = Model(predict_function, dataframe_input=True)
shap_explainer = SHAPExplainer(background=background)
explanation = shap_explainer.explain(inputs=to_explain, outputs=model(to_explain), model=model)
answers = [-.152, -.114, 0.00304, .0525, -.0725]
for _, saliency in explanation.saliency_map().items():
for i, feature_importance in enumerate(saliency.getPerFeatureImportance()[:-1]):
assert answers[i] - 1e-2 <= feature_importance.getScore() <= answers[i] + 1e-2
def shap_plots(block):
"""Test SHAP plots"""
np.random.seed(0)
data = pd.DataFrame(np.random.rand(101, 5))
background = data.iloc[:100]
to_explain = data.iloc[100:101]
model_weights = np.random.rand(5)
predict_function = lambda x: np.stack([np.dot(x.values, model_weights), 2 * np.dot(x.values, model_weights)], -1)
model = Model(predict_function, dataframe_input=True)
shap_explainer = SHAPExplainer(background=background)
explanation = shap_explainer.explain(inputs=to_explain, outputs=model(to_explain), model=model)
plot(explanation, block=block)
plot(explanation, block=block, render_bokeh=True)
plot(explanation, block=block, output_name='output-0')
plot(explanation, block=block, output_name='output-0', render_bokeh=True)
@pytest.mark.block_plots
def test_shap_plots_blocking():
shap_plots(block=True)
def test_shap_plots():
shap_plots(block=False)
def test_shap_as_df():
np.random.seed(0)
data = pd.DataFrame(np.random.rand(101, 5))
background = data.iloc[:100].values
to_explain = data.iloc[100:101].values
model_weights = np.random.rand(5)
predict_function = lambda x: np.stack([np.dot(x, model_weights), 2 * np.dot(x, model_weights)], -1)
model = Model(predict_function, disable_arrow=True)
shap_explainer = SHAPExplainer(background=background)
explanation = shap_explainer.explain(inputs=to_explain, outputs=model(to_explain), model=model)
for out_name, df in explanation.as_dataframe().items():
assert "Mean Background Value" in df
assert "output" in out_name
assert all([x in str(df) for x in "01234"])
def test_shap_as_html():
np.random.seed(0)
data = pd.DataFrame(np.random.rand(101, 5))
background = data.iloc[:100].values
to_explain = data.iloc[100:101].values
model_weights = np.random.rand(5)
predict_function = lambda x: np.stack([np.dot(x, model_weights), 2 * np.dot(x, model_weights)], -1)
model = Model(predict_function, disable_arrow=True)
shap_explainer = SHAPExplainer(background=background)
explanation = shap_explainer.explain(inputs=to_explain, outputs=model(to_explain), model=model)
assert True
def test_shap_numpy():
np.random.seed(0)
data = np.random.rand(101, 5)
model_weights = np.random.rand(5)
predict_function = lambda x: np.stack([np.dot(x, model_weights), 2 * np.dot(x, model_weights)], -1)
fnames = ['f{}'.format(x) for x in "abcde"]
onames = ['o{}'.format(x) for x in "12"]
model = Model(predict_function,
feature_names=fnames,
output_names=onames
)
shap_explainer = SHAPExplainer(background=data[1:])
explanation = shap_explainer.explain(inputs=data[0], outputs=model(data[0]), model=model)
for oname in onames:
assert oname in explanation.as_dataframe().keys()
for fname in fnames:
assert fname in explanation.as_dataframe()[oname]['Feature'].values
# deliberately make strange plot to test pre and post-function plot editing
def test_shap_edit_plot():
np.random.seed(0)
data = pd.DataFrame(np.random.rand(101, 5))
background = data.iloc[:100].values
to_explain = data.iloc[100:101].values
model_weights = np.random.rand(5)
predict_function = lambda x: np.stack([np.dot(x, model_weights), 2 * np.dot(x, model_weights)], -1)
model = Model(predict_function, disable_arrow=True)
shap_explainer = SHAPExplainer(background=background)
explanation = shap_explainer.explain(inputs=to_explain, outputs=model(to_explain), model=model)
plt.figure(figsize=(32,2))
plot(explanation, call_show=False)
plt.ylim(0, 123)
plt.show()