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 TreeInterpreter

From Leeroopedia


Knowledge Sources
Domains Machine_Learning, Interpretability
Last Updated 2026-02-07 12:00 GMT

Overview

TreeInterpreter is a greybox explainer that wraps the treeinterpreter package to provide local feature contribution explanations for scikit-learn tree-based models within the InterpretML API.

Description

This class provides an InterpretML-compatible wrapper around the treeinterpreter package by andosa:

  • TreeInterpreter: Extends ExplainerMixin with explainer type "specific" (greybox). It decomposes a tree-based model's prediction for each instance into per-feature contributions plus a bias term, showing how each feature pushed the prediction from the base value.

Supported scikit-learn models (from the treeinterpreter package):

    • DecisionTreeRegressor / DecisionTreeClassifier
    • ExtraTreeRegressor / ExtraTreeClassifier
    • RandomForestRegressor / RandomForestClassifier
    • ExtraTreesRegressor / ExtraTreesClassifier

The class handles both classification and regression. For binary classification, it extracts contributions for the positive class (index 1). Background data is optional but can be provided to determine feature names and types.

The output is a FeatureValueExplanation with per-instance breakdowns showing feature names, contribution scores, feature values, and a bias (intercept) term.

Usage

Use TreeInterpreter when you need to understand per-feature contributions for individual predictions from scikit-learn tree models or random forests. It requires the treeinterpreter package (pip install treeinterpreter).

Code Reference

Source Location

Signature

class TreeInterpreter(ExplainerMixin):
    available_explanations = ["local"]
    explainer_type = "specific"

    def __init__(self, model, data=None, feature_names=None, feature_types=None):

    def explain_local(self, X, y=None, name=None, **kwargs):

Import

from interpret.greybox import TreeInterpreter

I/O Contract

Constructor Inputs

Name Type Required Description
model scikit-learn tree model Yes A scikit-learn tree or forest model (see supported list above)
data numpy array or compatible No Optional background data used to determine feature names and types
feature_names list of str No List of feature names
feature_types list of str No List of feature types

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 treeinterpreter.predict

Outputs

Name Type Description
explanation FeatureValueExplanation Local explanation with per-instance feature contribution breakdowns and bias term

Usage Examples

Random Forest Example

from interpret.greybox import TreeInterpreter
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

model = RandomForestClassifier(n_estimators=100).fit(X_train, y_train)

ti = TreeInterpreter(model, data=X_train)
local_exp = ti.explain_local(X_test[:5], y_test[:5], name="RF TreeInterpreter")
local_exp.visualize(key=0)  # Feature contributions for first test instance

Regression Example

from interpret.greybox import TreeInterpreter
from sklearn.ensemble import RandomForestRegressor
import numpy as np

X = np.random.randn(200, 4)
y = X[:, 0] * 2 + X[:, 1] + np.random.randn(200) * 0.1

model = RandomForestRegressor(n_estimators=50).fit(X, y)

ti = TreeInterpreter(model, feature_names=["f0", "f1", "f2", "f3"])
local_exp = ti.explain_local(X[:3], y[:3])
local_exp.visualize(key=0)

Related Pages

Page Connections

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