nonzeroexit commited on
Commit
6a89413
·
verified ·
1 Parent(s): 87f0d31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -7
app.py CHANGED
@@ -59,14 +59,22 @@ assert len(selected_features) == 138, f"Expected 138 features, got {len(selected
59
  # ---------------------------------------------------------------------------
60
  # LIME explainer
61
  # Built ONCE at startup so explanations are reproducible across requests.
62
- # The training-data argument controls how LIME perturbs features around the
63
- # input. After MinMax scaling each feature lives in [0,1], so we use a small
64
- # uniform sample with a FIXED seed — that gives stable, repeatable weights.
65
- # (If you have a saved sample of real normalized training rows, swap it in
66
- # here and explanations will reflect the true feature distribution.)
67
  # ---------------------------------------------------------------------------
68
- _rng = np.random.default_rng(seed=42)
69
- _lime_background = _rng.uniform(low=0.0, high=1.0, size=(500, len(selected_features)))
 
 
 
 
 
 
 
 
 
 
70
 
71
  explainer = LimeTabularExplainer(
72
  training_data=_lime_background,
 
59
  # ---------------------------------------------------------------------------
60
  # LIME explainer
61
  # Built ONCE at startup so explanations are reproducible across requests.
62
+ # Prefers a real normalized training sample (lime_background.joblib). Falls
63
+ # back to seeded uniform noise if that file isn't present (still stable, but
64
+ # less faithful to the true feature distribution).
 
 
65
  # ---------------------------------------------------------------------------
66
+ try:
67
+ _lime_background = joblib.load("lime_background.joblib")
68
+ if _lime_background.shape[1] != len(selected_features):
69
+ raise ValueError(
70
+ f"lime_background.joblib has {_lime_background.shape[1]} cols, "
71
+ f"expected {len(selected_features)}"
72
+ )
73
+ print(f"[LIME] Using real training sample: {_lime_background.shape}", flush=True)
74
+ except Exception as e:
75
+ print(f"[LIME] No usable lime_background.joblib ({e}); falling back to uniform noise.", flush=True)
76
+ _rng = np.random.default_rng(seed=42)
77
+ _lime_background = _rng.uniform(low=0.0, high=1.0, size=(500, len(selected_features)))
78
 
79
  explainer = LimeTabularExplainer(
80
  training_data=_lime_background,