-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShapUtils.py
105 lines (77 loc) · 4.33 KB
/
ShapUtils.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
#SHAP tools file
import numpy as np
import shap
import sklearn
import tensorflow as tf
import matplotlib.pyplot as plt
from shap.plots import waterfall
from shap import kmeans
import pandas as pd
def predict(data, model):
return model.predict([data[:,i] for i in range(data.shape[1])]).flatten()
def make_shap_plots(model, data, outpath):
"""
Creates shap summary, bar, heatmap, violin, and layer violin plots.
"""
def predict_disc1(data):
return model.predict(data)[0][:,0]
def predict_disc2(data):
return model.predict(data)[0][:,1]
# Need to be conservative about the number of events to make plots
# For each event, Shap will remove one variable at a time and rerun inferencing
# Modify numEvents below to change the number of points in each plot
numEvents = 100
inputs = data["inputs"]
inputs = inputs[:numEvents,:]
names = data["vars"]
#Making shap values for disc1
explainer = shap.KernelExplainer(predict_disc1, inputs, feature_names=names)
shap_values = explainer.shap_values(inputs[:numEvents,:], nsamples=500)
explanation = shap.Explanation(values=shap_values, base_values=explainer.expected_value, data=inputs, feature_names=names)
#making the plots for disc1
shap.summary_plot(shap_values, features=inputs[:numEvents,:], feature_names=names, max_display=50)
save_plot("{}/summary_plot_disc1_plot.png".format(outpath))
shap.summary_plot(shap_values, features=inputs[:numEvents,:], feature_names=names, plot_type="bar", max_display=50)
save_plot("{}/bar_plot_disc1_plot.png".format(outpath))
shap.plots.heatmap(explanation, max_display=15)
save_plot("{}/heatmap_plot_disc1_plot.png".format(outpath))
shap.summary_plot(shap_values, features=inputs[:numEvents,:], feature_names=names, plot_type="violin", max_display=50)
save_plot("{}/violin_plot_disc1_plot.png".format(outpath))
shap.plots.violin(shap_values, features=inputs[:numEvents,:], feature_names=names, plot_type="layered_violin", max_display=50)
save_plot("{}/layered_violin_plot_disc1_plot.png".format(outpath))
#Making shap values for disc2
explainer = shap.KernelExplainer(predict_disc2, inputs, feature_names=names)
shap_values = explainer.shap_values(inputs[:numEvents,:], nsamples=500)
explanation = shap.Explanation(values=shap_values, base_values=explainer.expected_value, data=inputs, feature_names=names)
#making the plots for disc2
shap.summary_plot(shap_values, features=inputs[:numEvents,:], feature_names=names, max_display=50)
save_plot("{}/summary_plot_disc2_plot.png".format(outpath))
shap.summary_plot(shap_values, features=inputs[:numEvents,:], feature_names=names, plot_type="bar", max_display=50)
save_plot("{}/bar_plot_disc2_plot.png".format(outpath))
shap.plots.heatmap(explanation, max_display=15)
save_plot("{}/heatmap_plot_disc2_plot.png".format(outpath))
shap.summary_plot(shap_values, features=inputs[:numEvents,:], feature_names=names, plot_type="violin", max_display=50)
save_plot("{}/violin_plot_disc2_plot.png".format(outpath))
shap.plots.violin(shap_values, features=inputs[:numEvents,:], feature_names=names, plot_type="layered_violin", max_display=50)
save_plot("{}/layered_violin_plot_disc2_plot.png".format(outpath))
#heatmap plots with 20 predictions
numEvents = 20
inputs = data["inputs"]
inputs = inputs[:numEvents,:]
names = data["vars"]
explainer = shap.KernelExplainer(predict_disc1, inputs, feature_names=names)
shap_values = explainer.shap_values(inputs[:numEvents,:], nsamples=500)
explanation = shap.Explanation(values=shap_values, base_values=explainer.expected_value, data=inputs, feature_names=names)
shap.plots.heatmap(explanation, max_display=15)
save_plot("{}/heatmap20_plot_disc1_plot.png".format(outpath))
explainer = shap.KernelExplainer(predict_disc2, inputs, feature_names=names)
shap_values = explainer.shap_values(inputs[:numEvents,:], nsamples=500)
explanation = shap.Explanation(values=shap_values, base_values=explainer.expected_value, data=inputs, feature_names=names)
shap.plots.heatmap(explanation, max_display=15)
save_plot("{}/heatmap20_plot_disc2_plot.png".format(outpath))
def save_plot(name):
"""
saves plot as 'name'.png
"""
plt.savefig(name, bbox_inches='tight', format='png')
plt.close()