Quickly build Explainable AI dashboards that show the inner workings of so-called "blackbox" machine learning models.
Quickly build Explainable AI dashboards that show the inner workings of so-called "blackbox" machine learning models.
by: Oege Dijk
This package makes it convenient to quickly deploy a dashboard web app that explains the workings of a (scikit-learn compatible) machine learning model. The dashboard provides interactive plots on model performance, feature importances, feature contributions to individual predictions, "what if" analysis, partial dependence plots, SHAP (interaction) values, visualization of individual decision trees, etc.
You can also interactively explore components of the dashboard in a notebook/colab environment (or just launch a dashboard straight from there). Or design a dashboard with your own custom layout and explanations (thanks to the modular design of the library). And you can combine multiple dashboards into a single ExplainerHub.
Dashboards can be exported to static html directly from a running dashboard, or programmatically as an artifact as part of an automated CI/CD deployment process.
Examples deployed at: Fly.io, Hugging Face Space, detailed documentation at explainerdashboard.readthedocs.io, example notebook on how to launch dashboard for different models here, and an example notebook on how to interact with the explainer object here.
Works with scikit-learn, xgboost, catboost, lightgbm, and skorch
(sklearn wrapper for tabular PyTorch models) and others.
You can install the package through pip:
pip install explainerdashboard
or conda-forge:
conda install -c conda-forge explainerdashboard
SageMaker Studio runs notebooks and terminals in separate apps, so a common workflow
is to export a dashboard config to disk and run it from the JupyterServer terminal.
When running inside Studio, explainerdashboard can auto-detect SageMaker and apply
the correct proxy prefixes, or you can set them explicitly.
Notebook example (export dashboard to disk):
db = ExplainerDashboard(
explainer,
mode="dash",
port=8051,
sagemaker=True,
)
db.to_yaml("dashboard.yaml", explainerfile="dashboard.joblib", dump_explainer=True)Terminal example (run from the JupyterServer app):
explainerdashboard run dashboard.yaml --sagemaker --port 8051 --no-browserAccess the dashboard via the Studio proxy URL:
<STUDIO_URL>/jupyter/default/proxy/8051/If your Studio proxy path differs, you can override the prefixes:
explainerdashboard run dashboard.yaml \
--routes-pathname-prefix="/" \
--requests-pathname-prefix="/jupyter/default/proxy/8051/"Auto-detection uses the presence of /opt/ml/metadata/resource-metadata.json.
(for live demonstration see Fly.io or Hugging Face Space)
In a lot of organizations, especially governmental, but with the GDPR also increasingly in private sector, it is becoming more and more important to be able to explain the inner workings of your machine learning algorithms. Customers have to some extent a right to an explanation why they received a certain prediction, and more and more internal and external regulators require it. With recent innovations in explainable AI (e.g. SHAP values) the old black box trope is no longer valid, but it can still take quite a bit of data wrangling and plot manipulation to get the explanations out of a model. This library aims to make this easy.
The goal is manyfold:
The library includes:
The library is designed to be modular so that it should be easy to design your own interactive dashboards with plotly dash, with most of the work of calculating and formatting data, and rendering plots and tables handled by explainerdashboard, so that you can focus on the layout
and project specific textual explanations. (i.e. design it so that it will be interpretable for business users in your organization, not just data scientists)
Alternatively, there is a built-in standard dashboard with pre-built tabs (that you can switch off individually)
Fitting a model, building the explainer object, building the dashboard, and then running it can be as simple as:
ExplainerDashboard(ClassifierExplainer(RandomForestClassifier().fit(X_train, y_train), X_test, y_test)).run()Below a multi-line example, adding a few extra parameters.
You can group onehot encoded categorical variables together using the cats
parameter. You can either pass a dict specifying a list of onehot cols per
categorical feature, or if you encode using e.g.
pd.get_dummies(df.Name, prefix=['Name']) (resulting in column names 'Name_Adam', 'Name_Bob')
you can simply pass the prefix 'Name':
…If you are passing an sklearn/imblearn Pipeline, you can also clean up transformed
feature names and let the explainer infer onehot groups automatically:
explainer = ClassifierExplainer(
pipeline_model, X_test, y_test,
strip_pipeline_prefix=True, # e.g. "num__Age" -> "Age"
feature_name_fn=None, # optional custom rename function
auto_detect_pipeline_cats=True, # infer cats from transformed pipeline output
)For a regression model you can also pass the units of the target variable (e.g. dollars):
X_train, y_train, X_test, y_test = titanic_fare()
model = RandomForestRegressor().fit(X_train, y_train)
explainer = RegressionExplainer(model, X_test, y_test,
cats=['Deck', 'Embarked', 'Sex'],
descriptions=feature_descriptions,
units = "$", # defaults to ""
)
ExplainerDashboard(explainer).run()For pipeline-based models with post-processing/scaling, grouped categorical
features passed through cats are now accepted as long as encoded columns are
binary-like (not strictly only 0/1).
y_test is actually optional, although some parts of the dashboard like performance
metrics will obviously not be available: ExplainerDashboard(ClassifierExplainer(model, X_test)).run().
You can export a dashboard to static html with db.save_html('dashboard.html').
ExplainerDashboard(explainer, index=0).save_html('dashboard.html')or
ExplainerDashboard(explainer, index='Cumings, Mrs. John Bradley (Florence Briggs Thayer)').save_html('dashboard.html')For a simplified single page dashboard try ExplainerDashboard(explainer, simple=True).
You can combine multiple dashboards and host them in a single place using ExplainerHub:
db1 = ExplainerDashboard(explainer1, title="Classifier Explainer",
description="Model predicting survival on H.M.S. Titanic")
db2 = ExplainerDashboard(explainer2, title="Regression Explainer",
description="Model predicting ticket price on H.M.S. Titanic")
hub = ExplainerHub([db1, db2])
hub.run()You can adjust titles and descriptions, manage users and logins, store and load from config, manage the hub through a CLI and more. See the ExplainerHub documentation.
Show ExplainerHub screenshotSome of the calculations for the dashboard such as calculating SHAP (interaction) values and permutation importances can be slow for large datasets and complicated models. There are a few tricks to make this less painful:
shap_interaction=False) and disabling
permutation importances (no_permutations=True). Especially SHAP interaction
values can be very slow to calculate, and often are not needed for analysis.
For permutation importances you can set the n_jobs parameter to speed up
the calculation in parallel.shap_kwargs=dict(approximate=True) to the explainer initialization.shap='gputree' when your model supports it.
This requires an NVIDIA GPU and a CUDA-enabled SHAP build (see the SHAP docs).explainer.dump("explainer.joblib") and load with e.g.
ClassifierExplainer.from_file("explainer.joblib"). All calculated properties
are stored along with the explainer.O(TLD^2), where T is the
number of trees, L is the maximum number of leaves in any tree and
D the maximal depth of any tree. So reducing the number of leaves or average
depth in the decision tree can really speed up SHAP calculations.explainer.set_shap_values() and explainer.set_shap_interaction_values() methods.plot_sample
parameter to render a (different each time) random sample of observations
for the various scatter plots in the dashboard. E.g.:
ExplainerDashboard(explainer, plot_sample=1000).run()When working inside Jupyter or Google Colab you can use
ExplainerDashboard(mode='inline'), ExplainerDashboard(mode='external') or
`ExplainerDashboard(mode='jupyterlab
No open issues yet, or sync has not completed.