Aditya Das Gupta commited on
Commit
9ba3939
·
verified ·
1 Parent(s): 5c4cb43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -4
app.py CHANGED
@@ -15,7 +15,8 @@ scaler = joblib.load('scaler.pkl')
15
  # For this example, we'll create a dummy background dataset.
16
  # In a real scenario, you'd use a sample from your actual training data.
17
  dummy_data = np.zeros((100, len(scaler.get_feature_names_out())))
18
- explainer = shap.TreeExplainer(model, dummy_data)
 
19
 
20
 
21
  # --- Step 2: Create a Prediction Function ---
@@ -53,12 +54,13 @@ def predict_failure(air_temp, process_temp, rotational_speed, torque, tool_wear,
53
 
54
  # --- Generate SHAP plot ---
55
  shap_values = explainer.shap_values(input_df_reordered)
56
- # We are interested in the explanation for the 'Failure' class (class 1)
57
  plt.figure()
58
- shap.force_plot(explainer.expected_value[1], shap_values[1], input_df_reordered, matplotlib=True, show=False)
 
59
  plt.tight_layout()
60
  shap_plot_path = "shap_plot.png"
61
- plt.savefig(shap_plot_path)
62
  plt.close()
63
 
64
 
 
15
  # For this example, we'll create a dummy background dataset.
16
  # In a real scenario, you'd use a sample from your actual training data.
17
  dummy_data = np.zeros((100, len(scaler.get_feature_names_out())))
18
+ # We wrap the model's prediction function to ensure SHAP gets the probability of the positive class (Failure)
19
+ explainer = shap.TreeExplainer(lambda x: model.predict_proba(x)[:, 1], dummy_data)
20
 
21
 
22
  # --- Step 2: Create a Prediction Function ---
 
54
 
55
  # --- Generate SHAP plot ---
56
  shap_values = explainer.shap_values(input_df_reordered)
57
+ # We create the force plot for the single prediction
58
  plt.figure()
59
+ # Use explainer.expected_value (now a scalar) and shap_values[0] for the single instance
60
+ shap.force_plot(explainer.expected_value, shap_values[0], input_df_reordered, matplotlib=True, show=False, text_rotation=15)
61
  plt.tight_layout()
62
  shap_plot_path = "shap_plot.png"
63
+ plt.savefig(shap_plot_path, dpi=150, bbox_inches='tight')
64
  plt.close()
65
 
66