Implementation:Interpretml Interpret ShapTree
| Knowledge Sources | |
|---|---|
| Domains | Machine_Learning, Interpretability |
| Last Updated | 2026-02-07 12:00 GMT |
Overview
ShapTree is a greybox explainer that wraps the SHAP TreeExplainer algorithm to provide tree-specific SHAP (SHapley Additive exPlanations) local explanations within the InterpretML API.
Description
This class provides an InterpretML-compatible wrapper around shap.TreeExplainer:
- ShapTree: Extends
ExplainerMixinwith explainer type "specific" (greybox). It initializes the SHAP TreeExplainer with a tree-based model and optional background data, then provides local explanations via the sharedshap_explain_localutility. The background data is converted tonp.float64format since SHAP does not support string categoricals.
Key implementation details:
- The
check_additivityparameter is set toFalseby default due to precision issues on macOS - Background data can be
Nonefor some tree SHAP options - Data is preprocessed through InterpretML's
preclean_Xandunify_datapipeline before being passed to SHAP
- The
Usage
Use ShapTree when you need SHAP-based local feature importance explanations for tree-based models (such as XGBoost, LightGBM, or scikit-learn tree ensembles) and want the results presented through the InterpretML visualization framework.
Code Reference
Source Location
- Repository: Interpretml_Interpret
- File:
python/interpret-core/interpret/greybox/_shaptree.py
Signature
class ShapTree(ExplainerMixin):
available_explanations = ["local"]
explainer_type = "specific"
def __init__(self, model, data, feature_names=None, feature_types=None, **kwargs):
def explain_local(self, X, y=None, name=None, **kwargs):
Import
from interpret.greybox import ShapTree
I/O Contract
Constructor Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | tree model | Yes | A tree-based model compatible with SHAP TreeExplainer (e.g. XGBoost, LightGBM, sklearn trees) |
| data | numpy array or compatible | Yes (can be None) | Background data used to initialize SHAP; None for some tree SHAP options |
| feature_names | list of str | No | List of feature names |
| feature_types | list of str | No | List of feature types |
| **kwargs | varies | No | Additional keyword arguments passed to shap.TreeExplainer
|
explain_local Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| X | numpy array or compatible | Yes | Instances to explain |
| y | numpy array | No | True labels for performance metrics |
| name | str | No | User-defined explanation name |
| **kwargs | varies | No | Additional keyword arguments passed to SHAP (e.g. check_additivity)
|
Outputs
| Name | Type | Description |
|---|---|---|
| explanation | FeatureValueExplanation | Local explanation with per-instance SHAP values as horizontal bar charts |
Usage Examples
Basic Example
from interpret.greybox import ShapTree
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
model = RandomForestClassifier(n_estimators=100).fit(X, y)
shap_tree = ShapTree(model, X, feature_names=["sepal_l", "sepal_w", "petal_l", "petal_w"])
local_exp = shap_tree.explain_local(X[:5], y[:5], name="SHAP Tree Local")
local_exp.visualize(key=0)