-
Notifications
You must be signed in to change notification settings - Fork 0
/
SHAP.py
56 lines (36 loc) · 1.55 KB
/
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
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 1 15:31:38 2022
@author: Arpan
"""
import shap
from Modelling import X, xgb_mod, X_train
import matplotlib as plt
# Generate the Tree explainer and SHAP values
explainer = shap.TreeExplainer(xgb_mod)
shap_values = explainer.shap_values(X)
expected_value = explainer.expected_value
print(shap_values)
print()
print("Expected Values" , expected_value)
############## visualizations #############
# Generate summary dot plot
shap.summary_plot(shap_values, X,title="SHAP summary plot")
# Generate summary bar plot
shap.summary_plot(shap_values, X,plot_type="bar")
# Generate waterfall plot
shap.plots._waterfall.waterfall_legacy(expected_value, shap_values[79], features=X.loc[79,:], feature_names=X.columns, max_display=15, show=True)
# Generate dependence plot
shap.dependence_plot("worst concave points", shap_values, X, interaction_index="mean concave points")
# Generate multiple dependence plots
for name in X_train.columns:
shap.dependence_plot(name, shap_values, X)
shap.dependence_plot("worst concave points", shap_values, X, interaction_index="mean concave points")
# Generate force plot - Multiple rows
shap.force_plot(explainer.expected_value, shap_values[:100,:], X.iloc[:100,:])
plt.savefig('tmp.svg')
plt.close()
#Generate force plot - Single
shap.force_plot(explainer.expected_value, shap_values[0,:], X.iloc[0,:])
# Generate Decision plot
shap.decision_plot(expected_value, shap_values[20],link='logit' ,features=X.loc[20,:], feature_names=(X.columns.tolist()),show=True,title="Decision Plot")