TreeSHAPExplainer<T>

Models & Types · AiDotNet.Interpretability.Explainers

TreeSHAP explainer for computing exact SHAP values for tree-based models. Implements the exact O(TLD²) algorithm from the Lundberg paper.

Implements: IGPUAcceleratedExplainer<T>, ILocalExplainer<T, TreeSHAPExplanation<T>>

For Beginners

TreeSHAP is a fast, exact algorithm for computing SHAP values specifically designed for tree-based models (decision trees, random forests, gradient boosting).

Unlike Kernel SHAP which approximates SHAP values through sampling, TreeSHAP computes the exact Shapley values by efficiently traversing the tree structure.

Key advantages over Kernel SHAP:

  • Exact values: No approximation, mathematically precise results
  • Fast: O(TLD²) complexity where T=trees, L=leaves, D=depth
  • No background data needed: Uses the tree structure itself

This implementation follows the algorithm from: Lundberg, Lee, et al. “Consistent Individualized Feature Attribution for Tree Ensembles” arXiv:1802.03888 (2018)

TreeSHAP satisfies important properties:

  • Local accuracy: SHAP values sum to (prediction - expected_prediction)
  • Consistency: If a feature’s contribution increases, its SHAP value increases
  • Missingness: Missing features get zero attribution

Constructors

Constructor Summary
TreeSHAPExplainer(DecisionTreeNode<T> tree, int numFeatures, T expectedValue, string[] featureNames) Initializes a new TreeSHAP explainer for an ensemble of trees.
TreeSHAPExplainer(IEnumerable<DecisionTreeNode<T>> trees, int numFeatures, T expectedValue, string[] featureNames) Initializes a new TreeSHAP explainer for an ensemble of trees.

Properties

Property Summary
bool IsGPUAccelerated { get; }
bool SupportsGlobalExplanations { get; }
bool SupportsLocalExplanations { get; }
string MethodName { get; }

Methods

Method Summary
TreeSHAPExplanation<T> Explain(Vector<T> instance) Computes TreeSHAP values for an input instance.
TreeSHAPExplanation<T>[] ExplainBatch(Matrix<T> instances)
void SetGPUHelper(GPUExplainerHelper<T> helper)