-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplainability.py
More file actions
42 lines (33 loc) · 1.2 KB
/
Copy pathexplainability.py
File metadata and controls
42 lines (33 loc) · 1.2 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
import shap
import matplotlib.pyplot as plt
class ExplainabilityModule:
"""
SHAP-based explainability module.
"""
def __init__(self, model):
self.model = model
self.explainer = None
def calculate_shap(self, X_train, X_instance):
"""
img_path: Path to save the SHAP plot image.
"""
# Create explainer
# TreeExplainer is best for XGBoost
self.explainer = shap.TreeExplainer(self.model)
shap_values = self.explainer.shap_values(X_instance)
return shap_values, self.explainer.expected_value
def plot_shap_summary(self, X_train):
"""
Generates SHAP summary plot figure.
"""
if not self.explainer:
self.explainer = shap.TreeExplainer(self.model)
shap_values = self.explainer.shap_values(X_train)
# Create a new figure and make it active
plt.figure()
shap.summary_plot(shap_values, X_train, show=False)
plt.tight_layout()
# Get the current figure
fig = plt.gcf()
# We don't close it here because we return it for Streamlit to render
return fig