Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Interpretml Interpret ShapTree

From Leeroopedia


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 ExplainerMixin with explainer type "specific" (greybox). It initializes the SHAP TreeExplainer with a tree-based model and optional background data, then provides local explanations via the shared shap_explain_local utility. The background data is converted to np.float64 format since SHAP does not support string categoricals.

Key implementation details:

    • The check_additivity parameter is set to False by default due to precision issues on macOS
    • Background data can be None for some tree SHAP options
    • Data is preprocessed through InterpretML's preclean_X and unify_data pipeline before being passed to SHAP

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

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)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment