SHAPExplanation<T>

Models & Types · AiDotNet.Interpretability.Explainers

Represents a SHAP explanation for a single prediction.

Implements: IFeatureAttribution<T>

For Beginners

A SHAP explanation tells you exactly how much each feature contributed to a specific prediction.

Key concepts:

  • Baseline Value: The average prediction (what the model predicts “by default”)
  • SHAP Values: How much each feature pushed the prediction up or down from the baseline
  • Prediction: Should equal Baseline + Sum(SHAP Values)

Example: If predicting house prices:

  • Baseline: $300,000 (average house price)
  • Bedrooms: +$50,000 (having 4 bedrooms adds value)
  • Location: +$100,000 (good neighborhood)
  • Age: -$30,000 (older house reduces value)
  • Prediction: $420,000

Constructors

Constructor Summary
SHAPExplanation(Vector<T> shapValues, T baselineValue, T prediction, string[] featureNames) Initializes a new SHAP explanation.

Properties

Property Summary
T BaselineValue { get; } Gets the baseline (expected) prediction value.
T Prediction { get; } Gets the actual prediction for this instance.
Vector<T> ShapValues { get; } Gets the SHAP values for each feature.
int NumFeatures { get; } Gets the number of features.
string[] FeatureNames { get; } Gets the feature names, if available.

Methods

Method Summary
IEnumerable<ValueTuple<int, string, T>> GetSortedFeatures() Gets features sorted by absolute SHAP value (most important first).
IEnumerable<ValueTuple<int, string, T>> GetTopFeatures(int n) Gets the top N most important features by absolute SHAP value.
T GetShapValue(int featureIndex) Gets the SHAP value for a specific feature by name.
T GetShapValue(string featureName) Gets the SHAP value for a specific feature by name.
Vector<T> GetFeatureAttributions()
bool VerifyConsistency(double tolerance) Verifies that SHAP values sum to the difference between prediction and baseline.
string ToString() Returns a human-readable summary of the explanation.