Freate16 commited on
Commit
66203d2
·
1 Parent(s): 8a273c5

Patch XGBoost SHAP base_score bug

Browse files
Files changed (1) hide show
  1. chatbot/dual_inference.py +17 -5
chatbot/dual_inference.py CHANGED
@@ -200,11 +200,23 @@ def run_xgboost_inference(glake_id: str, weather_seq_df: pd.DataFrame):
200
  X = xgb_input[features_used]
201
  prob = float(model.predict_proba(X)[0, 1])
202
 
203
- explainer = shap.TreeExplainer(model)
204
- shap_values = explainer.shap_values(X)
205
-
206
- feature_impacts = list(zip(features_used, shap_values[0]))
207
- positive_drivers = sorted([f for f in feature_impacts if f[1] > 0], key=lambda x: x[1], reverse=True)[:3]
 
 
 
 
 
 
 
 
 
 
 
 
208
 
209
  return {
210
  "probability": prob,
 
200
  X = xgb_input[features_used]
201
  prob = float(model.predict_proba(X)[0, 1])
202
 
203
+ try:
204
+ # Fix known SHAP bug with XGBoost 2.0+ where base_score is saved as a string array e.g. "[0.39]"
205
+ import json
206
+ booster = model.get_booster()
207
+ config = json.loads(booster.save_config())
208
+ base_score = config.get('learner', {}).get('learner_model_param', {}).get('base_score', '')
209
+ if isinstance(base_score, str) and base_score.startswith('[') and base_score.endswith(']'):
210
+ config['learner']['learner_model_param']['base_score'] = base_score.strip('[]')
211
+ booster.load_config(json.dumps(config))
212
+
213
+ explainer = shap.TreeExplainer(model)
214
+ shap_values = explainer.shap_values(X)
215
+ feature_impacts = list(zip(features_used, shap_values[0]))
216
+ positive_drivers = sorted([f for f in feature_impacts if f[1] > 0], key=lambda x: x[1], reverse=True)[:3]
217
+ except Exception as e:
218
+ print(f"Warning: SHAP explainability skipped due to error: {e}")
219
+ positive_drivers = []
220
 
221
  return {
222
  "probability": prob,