4

I have machine learning results I plot using the shap package. Particularly I have plotted an interactive shap force plot and a static shap heat map.

Currently I save the shap force plot as a html file, but I'd like to add the shap heatmap to the html below the interactive force plot.

An example of how the interactive shap force plot looks is shown here: https://shap.readthedocs.io/en/latest/example_notebooks/tabular_examples/tree_based_models/Force%20Plot%20Colors.html (3rd figure down, the interactive one)

An example of the shap heat map is here: https://shap.readthedocs.io/en/latest/example_notebooks/api_examples/plots/heatmap.html

How I code for these plots currently is with:

import shap

model = RandomForestRegressor()
explainer = shap.TreeExplainer(model)
shap_values = explainer(X)
select = range(8)
features = X.iloc[select]
features_display = X.loc[features.index]

#Create force plot and save it as html:

output_of_force_plot = shap.force_plot(explainer.expected_value, shap_values[:500,:], 
                                        X.iloc[:500,:], features_display, show=False)

file ='force_plot.html'
shap.save_html(file, output_of_force_plot)


#Create static heat map:

shap_heatmap = shap.plots.heatmap(shap_values, max_display=8, show=False)

At the moment to add the shap_heatmap to the same html as the output_of_force_plot I've tried the answer from a similar question with plotly (Plotly saving multiple plots into a single html) but it doesn't work:

import plotly
with open('p_graph.html', 'a') as f:
    shap.save_html(file, output_of_force_plot, full_html=False)
    f.write(shap_heatmap.to_html(full_html=False))

AttributeError: 'NoneType' object has no attribute 'to_html'

Is there a way I can amend this plotly example to work for my shap html? Or another way I can combine my 2 figures into one html? I just want the interactive plot to stay interactive and then have the shap heat map shown below that interactive plot. Unfortunately I can't give example data to make the exact plots in this case.

DN1
  • 234
  • 1
  • 13
  • 38

0 Answers0