modeva.TestSuite.explain_shap#

TestSuite.explain_shap(dataset: str = 'test', sample_index: int = 0, baseline_dataset: str = 'train', baseline_sample_index: int = None, baseline_sample_size: int = 500, random_state: int = 0)#

Generate SHAP (SHapley Additive exPlanations) values for local model explanation.

This method uses either Kernel SHAP or Baseline SHAP to explain model predictions for a specific sample. If baseline_sample_index is provided, Baseline SHAP is used with the specified reference point. Otherwise, Kernel SHAP is used with randomly sampled background data.

Parameters:
  • dataset ({"main", "train", "test"}, default="test") – Dataset containing the sample to be explained.

  • sample_index (int, default=0) – Index of the sample to be explained within the selected dataset.

  • baseline_dataset ({"main", "train", "test"}, default="train") – Dataset to use for background/reference data.

  • baseline_sample_index (int, optional) – Index of the baseline sample to use as reference point. If None, random samples will be drawn as background data.

  • baseline_sample_size (int, default=500) – Number of background samples to use when baseline_sample_index is None. Controls computation speed vs. accuracy trade-off.

  • random_state (int, default=0) – Random seed for reproducible background sampling.

Returns:

A result object containing:

  • key: “explain_shap”

  • data: Name of the dataset used

  • model: Name of the model used

  • inputs: Input parameters used for the analysis

  • value: Dictionary containing

    • ”Name”: List of feature names;

    • ”Value”: List of feature values;

    • ”Effect”: List of feature contributions measured by SHAP.

  • value: Contains the core numerical results (feature names, values, and SHAP contributions)

  • table: Provides a tabular summary combining all the information

  • options: Dictionary of visualizations configuration for a horizontal bar plot where x-axis is SHAP value, and y-axis is the feature name. Run results.plot() to show this plot.

Return type:

ValidationResult

Notes

For classification tasks, SHAP values are computed for the positive class (class 1) probability. For regression tasks, SHAP values are computed for the predicted value.

Examples

Local Explainability

Local Explainability