OCDocker.OCScore.Analysis.SHAP.Explain module

SHAP computation helpers (background selection, explainer setup, shape wrangling) for Analysis workflows.

Usage:

from OCDocker.OCScore.Analysis.SHAP.Explain import compute_shap_values

OCDocker.OCScore.Analysis.SHAP.Explain.compute_shap_values(neural, X_background, X_eval, explainer='deep', background_size=None, eval_size=None, stratify_by=None, rng_seed=0)[source]

Compute SHAP values for a neural network model using Deep or Kernel explainer.

Parameters:
  • neural (object) – Object with a .NN attribute that is a PyTorch neural network model.

  • X_background (pd.DataFrame) – Background dataset for SHAP. Should contain the same features as X_eval.

  • X_eval (pd.DataFrame) – Evaluation dataset for which to compute SHAP values.

  • explainer (str) – Type of SHAP explainer to use: “deep” or “kernel”. Default is “deep”.

  • background_size (int, optional) – Number of samples to draw from X_background. If None, use all. Default is None.

  • eval_size (int, optional) – Number of samples to draw from X_eval. If None, use all. Default is None.

  • stratify_by (list of str, optional) – Column names to stratify sampling by. If None or empty, no stratification is done. Default is None.

  • rng_seed (int) – Random seed for reproducibility. Default is 0.

Returns:

SHAP values as a 2D array of shape (n_samples, n_features).

Return type:

np.ndarray