shapiq.tree.InterventionalTreeExplainerΒΆ
- class shapiq.tree.InterventionalTreeExplainer(model, data, class_index=None, *, debug=False, max_order=2, index='SII', index_func=None, bool_tree=False, weight_fn=None)[source]ΒΆ
Bases:
objectAny-order interventional Shapley-interaction explainer for tree models.
Extends interventional TreeSHAP to compute exact Shapley interactions of arbitrary order over a single decision tree or a tree ensemble (as validated by
shapiq.tree.validation.validate_tree_model()). Each coalitionβs contribution is decomposed against a reference background dataset using theE/Rpartition (features fixed by the explained point vs. by the reference), and the recursion is offloaded to one of two C++ kernels:Dense flatten path β
compute_interactions_flatten()formax_order <= 3when the dense buffer fits within_DENSE_FLATTEN_MAX_RESULT_SIZE.Sparse path β
compute_interactions_batched_sparse()for higher orders or wide-feature trees.
Indices supported via the C path are listed in
INDICES_C_IMPLEMENTATION_CAPABLE. Custom weight functions are accepted viaweight_fnand routed through a precomputed lookup table.The baseline value is computed from the validated trees by summing per-tree predictions over the reference data.
validate_tree_modelalready scales sklearn ensemble trees by1/n_estimatorsand extracts class-specific raw scores for XGBoost / LightGBM classifiers, so this single path lands on the same scale as the kernel for every supported model.- Variables:
tree β Validated tree (or list of trees) from
validate_tree_model().reference_data β Background dataset (shape
(n_ref, n_features)) used to define interventional baselines, cast tofloat32.baseline_value β Mean tree-prediction over
reference_data(scalar); written as the order-0 entry of the returned interactions.max_order β Maximum interaction order computed.
index β Interaction index (e.g.
"SII"); replaced with"CUSTOM"whenweight_fnis supplied.n_players β Number of features.
- Parameters:
Initialize the InterventionalTreeExplainer.
- Parameters:
model (
object) β A fitted tree or tree ensemble compatible withshapiq.tree.validation.validate_tree_model()(sklearn, XGBoost, LightGBM, or a precomputed leaf-matrix list).data (
ndarray) β Background dataset of shape(n_ref, n_features)defining the interventional baseline.class_index (
int|None) β Class index for classifiers. For binarypredict_probamodels, defaults to1if left asNone. Ignored for regressors.debug (
bool) β IfTrue, the C++ kernel prints debug information. Defaults toFalse.max_order (
int) β Maximum interaction order to compute. Defaults to2.index (
str) β Interaction index; one ofINDICES_C_IMPLEMENTATION_CAPABLE. Replaced with"CUSTOM"whenweight_fnis supplied. Defaults to"SII".index_func (
Callable|None) β Reserved for a Python-side custom index function. Defaults toNone.bool_tree (
bool) β IfTrue, the tree is treated as boolean (coalitions in \(\\{0, 1\\}^n\)) and preprocessed once with the BitSet DFS C++ helper instead of per-explanation. Used byProxySHAP. Defaults toFalse.weight_fn (
Callable[[int,int,int],float] |None) β Optional custom weight callable with signatureweight_fn(coalition_size, interaction_size, n_players) -> float. When supplied, overridesindexand triggers building a precomputed lookup table. Defaults toNone.
- explain_function(x, **_)[source]ΒΆ
Compute interaction values for a single instance.
Routes to the dense flatten C kernel for low-order, narrow-feature cases and to the sparse batched C kernel otherwise (see the class docstring). The empty interaction
()is always populated withself.baseline_valuebefore constructing the result.- Parameters:
- Return type:
- Returns:
InteractionValuescarrying the requested interactionindex,max_order=self.max_order, andmin_order=1as the declared lower bound on computed orders. Note that the underlying interactions dict still contains the empty-set entry()set toself.baseline_value, even thoughmin_orderreports1.