-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshap_info.py
More file actions
45 lines (33 loc) · 1.11 KB
/
Copy pathshap_info.py
File metadata and controls
45 lines (33 loc) · 1.11 KB
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
#shap.py
import shap
import matplotlib.pyplot as plt
def shap_info(df, model):
features = [
"2021-2022 attendance rate - year to date_scaled",
"2020-2021 attendance rate_scaled",
"2019-2020 attendance rate_scaled",
"2021-2022 student count - year to date_scaled",
"2020-2021 student count_scaled",
"2019-2020 student count_scaled"
]
X = df[features]
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X)
shap.summary_plot(shap_values, X)
shap.summary_plot(shap_values, X, plot_type="bar")
# Beeswarm plot
plt.figure(figsize=(10,6))
shap.summary_plot(shap_values, X, show=False)
plt.tight_layout()
plt.savefig("data/shap_beeswarm.png", dpi=300)
plt.close()
# Bar plot
plt.figure(figsize=(10,6))
shap.summary_plot(shap_values, X, plot_type="bar", show=False)
plt.tight_layout()
plt.savefig("data/shap_bar.png", dpi=300)
plt.close()
print("SHAP plots saved as 'shap_beeswarm.png' and 'shap_bar.png'")
#print("printing shap info")
#print(shap_values)
return shap_values