SHAP (shap.force_plot) on Streamlit

I am trying to use SHAP library on streamlit to draw force_plot, summary_plot, summary_plot_bar and dependance_plot.
But only force_plot is not be displayed as follows:

*force_plot image should be located on top but that place is blank.

  • python coding is as follows:
###########
import numpy as np
import pandas as pd
import streamlit as st
import matplotlib.pyplot as plt
import japanize_matplotlib

#Use Random Forest
from sklearn.ensemble import RandomForestRegressor

#SHAP(SHapley Additive exPlanations)
import shap
shap.initjs() # for visualization
X, y = shap.datasets.boston()

clf = RandomForestRegressor(n_estimators=500, n_jobs=-1)
clf.fit(X, y)

explainer = shap.TreeExplainer(clf, X)
shap_values = explainer.shap_values(X)
st.set_option('deprecation.showPyplotGlobalUse', False)

#force_plot
i = 0
shap.force_plot(explainer.expected_value, shap_values[i,:], X.iloc[i,:])
st.pyplot()

#summary_plot_bar
shap.summary_plot(shap_values, X, plot_type="bar")
st.pyplot()

#summary_plot
shap.summary_plot(shap_values, X)
st.pyplot()

#dependance_plot
shap.dependence_plot("LSTAT", shap_values, X)
st.pyplot()

###########################

The image of force_plot is displayed on Jupyter notebook using the same python coding.

I would appreciate it if you could give me some advice.

Thank you.

Hi @59er ,

I haven’t use SHAP since a while, but I remember a time ago that @andfanilo created a workaround for SHAP and streamlit.

Maybe this might help you? Maybe there are also other solutions in the meanwhile?

Best,
Alex

2 Likes

Thank you very much for your advice.

I solved SHAP(force_plot) problem using streamlit.components.v1.

Thank you!

Hey guys, just a simple line:
st.pyplot(shap.plots.force(shaps_values[0],matplotlib=True))
this works very well in my case.
streamlit version:1.1.0

1 Like