Explain Blackbox Regressors#

In this notebook we will use the interpret package to explain blackbox regressors using SHAP, Lime, MorrisSensitivity, and PartialDependence.

This notebook can be found in our examples folder on GitHub.

# install interpret if not already installed
    import interpret
except ModuleNotFoundError:
    !pip install --quiet interpret pandas scikit-learn lime
import numpy as np
import pandas as pd
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from interpret import show

from interpret import set_visualize_provider
from interpret.provider import InlineProvider

X, y = load_diabetes(return_X_y=True, as_frame=True)

seed = 42
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=seed)

Train a blackbox regression system

from sklearn.ensemble import RandomForestRegressor
from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline

#Blackbox system can include preprocessing, not just a regressor!
pca = PCA()
rf = RandomForestRegressor(random_state=seed)

blackbox_model = Pipeline([('pca', pca), ('rf', rf)])
blackbox_model.fit(X_train, y_train)
Pipeline(steps=[('pca', PCA()), ('rf', RandomForestRegressor(random_state=42))])
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

Show blackbox model performance

from interpret.perf import RegressionPerf

blackbox_perf = RegressionPerf(blackbox_model).explain_perf(X_test, y_test, name='Blackbox')