cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to display SHAP plots?

ArielHerrera
New Contributor II

I am looking to display SHAP plots, here is the code:

import xgboost import shap

shap.initjs() # load JS visualization code to notebookX,y = shap.datasets.boston() # train XGBoost model

model = xgboost.train({"learning_rate": 0.01}, xgboost.DMatrix(X, label=y), 100)

explainer = shap.TreeExplainer(model) # explain the model's predictions using SHAP values

shap_values = explainer.shap_values(X)

shap_explain = shap.force_plot(explainer.expected_value, shap_values[0,:], X.iloc[0,:]) # visualize the first prediction's explanation

displayHTML(shap_explain.data) # display plot

However I am receiving the following error:

0693f000007OoIfAAK

Any help greatly appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions

RimmaShafikova
New Contributor III

I was finally able to plot them in DataBricks without js

shap_display = shap.force_plot(explainer.expected_value[1], shap_value[1], feat_x.iloc[0, :], matplotlib=True)

display(shap_display)

View solution in original post

5 REPLIES 5

RimmaShafikova
New Contributor III

I was finally able to plot them in DataBricks without js

shap_display = shap.force_plot(explainer.expected_value[1], shap_value[1], feat_x.iloc[0, :], matplotlib=True)

display(shap_display)

It is quite good but only works for a single explanation. If you want to apply to multiple samples, force_plot has not been supported yet as in Jan 2020.

ArielHerrera
New Contributor II

It also needs to be run on an earlier version of matplotlib

matplotlib: 1.5.3

sean_owen
Honored Contributor II

For what it's worth, I got force plots to work in Databricks by copying the bundle.js Javascript from the SHAP package into a hidden cell in the notebook. See https://databricks.com/blog/2019/06/17/detecting-bias-with-shap.html and the notebook that accompanies it.

lrnzcig
New Contributor II

As @Vinh dqvinh87​  noted, the accepted solution only works for

force_plot

. For other plots, the following trick works for me:

import matplotlib.pyplot as plt
p = shap.summary_plot(shap_values, test_df, show=False) 
display(p)

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group