-
Notifications
You must be signed in to change notification settings - Fork 3
/
dynamic_shap_plots.py
358 lines (305 loc) · 15.1 KB
/
dynamic_shap_plots.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
from shap_plots import shap_summary_plot, shap_dependence_plot
import plotly.tools as tls
import dash_core_components as dcc
import pandas as pd
from sklearn.model_selection import train_test_split
import numpy as np
import xgboost
import shap
import matplotlib
import plotly.graph_objs as go
try:
import matplotlib.pyplot as pl
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.ticker import MaxNLocator
except ImportError:
pass
from sklearn import preprocessing
cdict1 = {
'red': ((0.0, 0.11764705882352941, 0.11764705882352941),
(1.0, 0.9607843137254902, 0.9607843137254902)),
'green': ((0.0, 0.5333333333333333, 0.5333333333333333),
(1.0, 0.15294117647058825, 0.15294117647058825)),
'blue': ((0.0, 0.8980392156862745, 0.8980392156862745),
(1.0, 0.3411764705882353, 0.3411764705882353)),
'alpha': ((0.0, 1, 1),
(0.5, 1, 1),
(1.0, 1, 1))
} # #1E88E5 -> #ff0052
red_blue = LinearSegmentedColormap('RedBlue', cdict1)
def matplotlib_to_plotly(cmap, pl_entries):
h = 1.0/(pl_entries-1)
pl_colorscale = []
for k in range(pl_entries):
C = list(map(np.uint8, np.array(cmap(k*h)[:3])*255))
pl_colorscale.append([k*h, 'rgb'+str((C[0], C[1], C[2]))])
return pl_colorscale
red_blue = matplotlib_to_plotly(red_blue, 255)
def summary_plot_plotly_fig(dataset, target='target column', max_display = 20):
data = pd.read_csv(dataset, encoding="ISO-8859-1")
X = data.drop(['target column'], axis=1)
y = data[target]
y = y/max(y)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=7)
X_train.fillna((-999), inplace=True)
X_test.fillna((-999), inplace=True)
_, shap_values, feature_names = train_model_and_return_shap_values(X, y, target)
mpl_fig = shap_summary_plot(shap_values, pd.DataFrame(X_train, columns=X.columns), feature_names=feature_names, max_display=20)
plotly_fig = tls.mpl_to_plotly(mpl_fig)
plotly_fig['layout'] = {'xaxis': {'title': 'SHAP value (impact on model output)'}}
feature_order = np.argsort(np.sum(np.abs(shap_values), axis=0)[:-1])
feature_order = feature_order[-min(max_display, len(feature_order)):]
text = [feature_names[i] for i in feature_order]
text = iter(text)
for i in range(1, len(plotly_fig['data']), 2):
t = text.__next__()
plotly_fig['data'][i]['name'] = ''
plotly_fig['data'][i]['text'] = t
plotly_fig['data'][i]['hoverinfo'] = 'text'
colorbar_trace = go.Scatter(x=[None],
y=[None],
mode='markers',
marker=dict(
colorscale=red_blue,
showscale=True,
cmin=-5,
cmax=5,
colorbar=dict(thickness=5, tickvals=[-5, 5], ticktext=['Low', 'High'], outlinewidth=0)
),
hoverinfo='none'
)
plotly_fig['layout']['showlegend'] = False
plotly_fig['layout']['hovermode'] = 'closest'
plotly_fig['layout']['height']=600
plotly_fig['layout']['width']=500
plotly_fig['layout']['xaxis'].update(zeroline=True, showline=True, ticklen=4, showgrid=False)
plotly_fig['layout']['yaxis'].update(dict(visible=False))
plotly_fig.add_trace(colorbar_trace)
plotly_fig.layout.update(
annotations=[dict(
x=1.18,
align="right",
valign="top",
text='Feature value',
showarrow=False,
xref="paper",
yref="paper",
xanchor="right",
yanchor="middle",
textangle=-90,
font=dict(family='Calibri', size=14)
)
],
margin=dict(t=20)
)
return plotly_fig
def train_model_and_return_shap_values(X, y, target):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=7)
X_train.fillna((-999), inplace=True)
X_test.fillna((-999), inplace=True)
# Some of values are float or integer and some object. This is why we need to cast them:
for f in X_train.columns:
if X_train[f].dtype=='object':
lbl = preprocessing.LabelEncoder()
lbl.fit(list(X_train[f].values))
X_train[f] = lbl.transform(list(X_train[f].values))
for f in X_test.columns:
if X_test[f].dtype=='object':
lbl = preprocessing.LabelEncoder()
lbl.fit(list(X_test[f].values))
X_test[f] = lbl.transform(list(X_test[f].values))
X_train=np.array(X_train)
X_test=np.array(X_test)
X_train = X_train.astype(float)
X_test = X_test.astype(float)
d_train = xgboost.DMatrix(X_train, label=y_train, feature_names=list(X))
d_test = xgboost.DMatrix(X_test, label=y_test, feature_names=list(X))
# train the model
params = {
"eta": 0.01,
"subsample": 0.5,
"base_score": np.mean(y_train),
"silent": 1
}
model = xgboost.train(params, d_train, 5000, evals = [(d_test, "test")], verbose_eval=None, early_stopping_rounds=50)
feature_names = model.feature_names
shap_values = shap.TreeExplainer(model).shap_values(pd.DataFrame(X_train, columns=X.columns))
return model, shap_values, feature_names
def dependence_plot_to_plotly_fig(dataset, target='target column', max_display=10):
data = pd.read_csv(dataset, encoding="ISO-8859-1")
X = data.drop(['target column'], axis=1)
y = data[target]
y = y/max(y)
xgb_full = xgboost.DMatrix(X, label=y)
# create a train/test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=7)
xgb_train = xgboost.DMatrix(X_train, label=y_train)
xgb_test = xgboost.DMatrix(X_test, label=y_test)
# use validation set to choose # of trees
params = {
# "eta": 0.002,
# "max_depth": 3,
# "subsample": 0.5,
"silent": 1
}
model_train = xgboost.train(params, xgb_train, 3000, evals = [(xgb_test, "test")], verbose_eval=None)
# train final model on the full data set
params = {
# "eta": 0.002,
# "max_depth": 3,
# "subsample": 0.5,
"silent": 1
}
model = xgboost.train(params, xgb_full, 1500, evals = [(xgb_full, "test")], verbose_eval=None)
features = model.feature_names
shap_values = shap.TreeExplainer(model).shap_values(X)
feature_order = np.argsort(np.sum(np.abs(shap_values), axis=0)[:-1])
feature_order = feature_order[-min(max_display, len(feature_order)):]
features = [features[i] for i in feature_order[::-1]]
lis = []
for i in features:
mpl_fig, interaction_index = shap_dependence_plot(i, shap_values, X)
plotly_fig = tls.mpl_to_plotly(mpl_fig)
# The x-tick labels start by default from 0, which is not necessarily the min value of the feature.
# So, we need to increment the x-tick labels by 1. But while doing so, the y-axis gets shifted.
# To prevent that, we need to manually control the x-axis range from r_min to r_max
new_x = []
for j in plotly_fig['data'][0]['x']:
new_x.append(j)
r_min = min(plotly_fig['data'][0]['x'])
r_max = max(plotly_fig['data'][0]['x'])
plotly_fig['layout']['xaxis'].update(range=[r_min-1, r_max+1])
plotly_fig['data'][0]['x'] = tuple(new_x)
# Define the colorbar
colorbar_trace = go.Scatter(x=[None],
y=[None],
mode='markers',
marker=dict(
colorscale=red_blue,
showscale=True,
colorbar=dict(thickness=5, outlinewidth=0),
color=[min(X[X.columns[interaction_index]]), max(X[X.columns[interaction_index]])],
),
hoverinfo='none'
)
plotly_fig['layout']['showlegend'] = False
plotly_fig['layout']['hovermode'] = 'closest'
plotly_fig['layout']['height']=380
plotly_fig['layout']['width']=450
plotly_fig['layout']['xaxis'].update(zeroline=True,
showline=True,
ticklen=4,
showgrid=False,
tickmode='linear')
title = plotly_fig['layout']['yaxis']['title']
plotly_fig['layout']['yaxis'].update(title=title.split(' -')[0])
plotly_fig.add_trace(colorbar_trace)
plotly_fig.layout.update(
annotations=[dict(
x=1.23,
align="right",
valign="top",
text=X.columns[interaction_index],
showarrow=False,
xref="paper",
yref="paper",
xanchor="right",
yanchor="middle",
textangle=-90,
font=dict(family='Calibri', size=14)
)
],
margin=dict(t=50, b=50, l=50, r=80)
)
lis.append(plotly_fig)
return lis, features
def interaction_plot_to_plotly_fig(dataset, target_col='target column', max_display=10):
data = pd.read_csv(dataset, encoding="ISO-8859-1")
X = data.drop(['target column'], axis=1)
y = data[target_col]
y = y/max(y)
xgb_full = xgboost.DMatrix(X, label=y)
# create a train/test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=7)
xgb_train = xgboost.DMatrix(X_train, label=y_train)
xgb_test = xgboost.DMatrix(X_test, label=y_test)
# use validation set to choose # of trees
params = {
# "eta": 0.002,
# "max_depth": 3,
# "subsample": 0.5,
"silent": 1
}
model_train = xgboost.train(params, xgb_train, 3000, evals = [(xgb_test, "test")], verbose_eval=None)
# train final model on the full data set
params = {
# "eta": 0.002,
# "max_depth": 3,
# "subsample": 0.5,
"silent": 1
}
model = xgboost.train(params, xgb_full, 1500, evals = [(xgb_full, "test")], verbose_eval=None)
features = model.feature_names
shap_values = shap.TreeExplainer(model).shap_values(X)
feature_order = np.argsort(np.sum(np.abs(shap_values), axis=0)[:-1])
feature_order = feature_order[-min(max_display, len(feature_order)):]
features = [features[i] for i in feature_order[::-1]]
shap_interaction_values = shap.TreeExplainer(model).shap_interaction_values(X)
lis = []
for i in features:
for j in features:
mpl_fig = pl.figure()
ax = mpl_fig.add_subplot(111)
_, interaction_index = shap_dependence_plot ( (i, j), shap_interaction_values, X.iloc[:2000,:] )
plotly_fig = tls.mpl_to_plotly(mpl_fig)
r_min = min(plotly_fig['data'][0]['x'])
r_max = max(plotly_fig['data'][0]['x'])
plotly_fig['layout']['xaxis'].update(range=[r_min-1, r_max+1])
plotly_fig['layout']['showlegend'] = False
plotly_fig['layout']['hovermode'] = 'closest'
plotly_fig['layout']['height']=380
plotly_fig['layout']['width']=450
plotly_fig['layout']['xaxis'].update(zeroline=True,
showline=True,
ticklen=4,
showgrid=False,
tickmode='linear')
plotly_fig['layout']['yaxis'].update(showline=True)
if i!=j:
# plotly_fig['layout']['height']=380
plotly_fig['layout']['width']=480
plotly_fig['layout']['yaxis']['title'] = "SHAP interaction value for {} and {}".format(i.split('-')[0], j.split('-')[0])
# Define the colorbar
colorbar_trace = go.Scatter(x=[None],
y=[None],
mode='markers',
marker=dict(
colorscale=red_blue,
showscale=True,
colorbar=dict(thickness=5, outlinewidth=0),
color=[min(X[X.columns[interaction_index]]), max(X[X.columns[interaction_index]])],
),
hoverinfo='none'
)
plotly_fig.add_trace(colorbar_trace)
plotly_fig.layout.update(
annotations=[dict(
x=1.23,
align="right",
valign="top",
text=X.columns[interaction_index],
showarrow=False,
xref="paper",
yref="paper",
xanchor="right",
yanchor="middle",
textangle=-90,
font=dict(family='Calibri', size=14)
)
],
margin=dict(t=30, b=30, l=60, r=80)
)
else:
plotly_fig['layout']['yaxis']['title'] = "SHAP main effect value for {}".format(i.split('-')[0])
lis.append(plotly_fig)
return lis, features