Update app.py
Browse files
app.py
CHANGED
|
@@ -118,31 +118,29 @@ def explainability(_):
|
|
| 118 |
explainer = shap.TreeExplainer(model)
|
| 119 |
shap_values = explainer.shap_values(X_test)
|
| 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 |
except Exception as e:
|
| 148 |
shap_path = "./shap_error.png"
|
|
|
|
| 118 |
explainer = shap.TreeExplainer(model)
|
| 119 |
shap_values = explainer.shap_values(X_test)
|
| 120 |
|
| 121 |
+
try:
|
| 122 |
+
if isinstance(shap_values, list):
|
| 123 |
+
class_idx = 0
|
| 124 |
+
sv = shap_values[class_idx]
|
| 125 |
+
else:
|
| 126 |
+
sv = shap_values
|
| 127 |
+
|
| 128 |
+
# Use safe feature names if mismatch, fallback to dummy
|
| 129 |
+
num_features = sv.shape[1]
|
| 130 |
+
if num_features <= X_test.shape[1]:
|
| 131 |
+
feature_names = X_test.columns[:num_features]
|
| 132 |
+
else:
|
| 133 |
+
feature_names = [f"Feature_{i}" for i in range(num_features)]
|
| 134 |
+
|
| 135 |
+
X_shap_safe = pd.DataFrame(np.zeros_like(sv), columns=feature_names)
|
| 136 |
+
|
| 137 |
+
shap.summary_plot(sv, X_shap_safe, show=False)
|
| 138 |
+
shap_path = "./shap_plot.png"
|
| 139 |
+
plt.title("SHAP Summary")
|
| 140 |
+
plt.savefig(shap_path)
|
| 141 |
+
if wandb.run:
|
| 142 |
+
wandb.log({"shap_summary": wandb.Image(shap_path)})
|
| 143 |
+
plt.clf()
|
|
|
|
|
|
|
| 144 |
|
| 145 |
except Exception as e:
|
| 146 |
shap_path = "./shap_error.png"
|