shapiq.explainer.TreeExplainer¶

class shapiq.explainer.TreeExplainer(model, *, mode='pathdependent', reference_dataset=None, max_order=1, min_order=0, index='SV', class_index=None, **kwargs)[source]¶

Bases: Explainer

The TreeExplainer class for tree-based models.

The explainer for tree-based models using the TreeSHAPIQ algorithm. For details, refer to Muschalik et al. (2024) [Mus24].

TreeSHAP-IQ is an algorithm for computing Shapley Interaction values for tree-based models. It is based on the Linear TreeSHAP algorithm by Yu et al. (2022) [Yu22], but extended to compute Shapley Interaction values up to a given order. TreeSHAP-IQ needs to visit each node only once and makes use of polynomial arithmetic to compute the Shapley Interaction values efficiently.

The TreeExplainer can be used with a variety of tree-based models, including scikit-learn, XGBoost, LightGBM, and CatBoost. The explainer can handle both regression and classification models.

References

[Yu22]

Peng Yu, Chao Xu, Albert Bifet, Jesse Read. (2022). Linear Tree Shap. In: Proceedings of 36th Conference on Neural Information Processing Systems. https://openreview.net/forum?id=OzbkiUo24g

[Mus24]

Maximilian Muschalik, Fabian Fumagalli, Barbara Hammer, & Eyke Hüllermeier (2024). Beyond TreeSHAP: Efficient Computation of Any-Order Shapley Interactions for Tree Ensembles. In: Proceedings of the AAAI Conference on Artificial Intelligence, 38(13), 14388-14396. https://doi.org/10.1609/aaai.v38i13.29352

Initializes the TreeExplainer.

Parameters:
  • model (dict | TreeModel | list[TreeModel] | Any) – A tree-based model to explain.

  • mode (In "interventional") – The mode of the explainer, either "pathdependent" or "interventional".

  • mode

  • "SV". (the explainer computes path-dependent interaction values using the TreeSHAPIQ algorithm or the Linear TreeSHAP algorithm if the index is)

  • mode

  • algorithm. (the explainer computes interventional interaction values using the Interventional TreeExplainer)

  • "pathdependent". (Defaults to)

  • max_order (int) – The maximum order of interactions to be computed. Set to 1 for no interactions (i.e, for Shapley values "SV" or Banzhaf values "BV"). Any value higher than 1 computes interaction values up to that order. Defaults to 1.

  • min_order (int) – The minimum interaction order to keep in the returned InteractionValues. Must satisfy 0 <= min_order <= max_order. When min_order == 0 the empty interaction () is included with the baseline value. When min_order >= 1 all interactions of order below min_order are filtered out of the result; the underlying algorithm still computes them internally when required by aggregated indices such as "k-SII". Defaults to 0.

  • index (Literal['SV', 'SII', 'k-SII']) – The type of interaction to be computed. It can be one of ["k-SII", "SII", "STII", "FSII", "BII", "SV"]. All indices apart from "BII" will reduce to the "SV" (Shapley value) for order 1. Defaults to "SV".

  • class_index (int | None) – The class index of the model to explain. Defaults to None, which will set the class index to 1 per default for classification models and is ignored for regression models.

  • reference_dataset (ndarray | None) – A dataset to be used for reference in the explanation when using mode=interventional. Defaults to None.

  • **kwargs (Any) – Additional keyword arguments are ignored.

explain_function(x, *args, **kwargs)[source]¶

Computes the interaction index for a single instance.

The method used for computing the explanation depends on the specified mode and the parameters of the explainer.

Parameters:
  • x (ndarray) – The instance to explain as a 1-dimensional array.

  • *args (Any) – Additional positional arguments are ignored.

  • **kwargs (Any) – Additional keyword arguments forwarded to the per-mode explain function.

Return type:

InteractionValues

Returns:

The computed interaction index for the instance.