2

I am going through https://towardsdatascience.com/explain-your-model-with-the-shap-values-bc36aac4de3d trying to get the force_plot to print.

I'm running Python 3.8.5 on Ubuntu 20.04

I run this code:

shap.initjs()

# Write in a function

random_picks = np.arange(1,330,50) # Every 50 rows
S = X_test.iloc[random_picks]
def shap_plot(j):
    explainerModel = shap.TreeExplainer(xg_clf)
    shap_values_Model = explainerModel.shap_values(S)
    p = shap.force_plot(explainerModel.expected_value, shap_values_Model[j], S.iloc[[j]])
    return(p)
z = shap_plot(3)

and I get <shap.plots._force.AdditiveForceVisualizer object at 0x7f1568cac070>

to return.

I'm not a python expert, so I've tried looking at this data:

display(z)

whieh isn't defined.

and print(z) which just returns the name of the object, and doesn't help me to see what was plotted.

I've also tried using matplotlib which is already loaded,

def shap_plot(j):
    explainerModel = shap.TreeExplainer(xg_clf)
    shap_values_Model = explainerModel.shap_values(S)
    p = shap.force_plot(explainerModel.expected_value, shap_values_Model[j], S.iloc[[j]])
    plt.savefig('tmp.svg')
    plt.close()
    return(p)
shap_plot(3)

but this just gives an empty image.

If there is an error, I don't see it.

How can I get this shap.force_plot to show the image?

2 Answers 2

5

The solution is in the manual:

help(shap.force_plot)

which shows

matplotlib : bool
        Whether to use the default Javascript output, or the (less developed) matplotlib output. Using matplotlib can be helpful in scenarios where rendering Javascript/HTML is inconvenient.

Indeed, running a notebook is very inconvenient for my purposes.

so in order to save an image:

def shap_plot(j):
    explainerModel = shap.TreeExplainer(xg_clf)
    shap_values_Model = explainerModel.shap_values(S)
    p = shap.force_plot(explainerModel.expected_value, shap_values_Model[j], S.iloc[[j]], matplotlib = True, show = False)
    plt.savefig('tmp.svg')
    plt.close()
    return(p)
Sign up to request clarification or add additional context in comments.

5 Comments

On their website it has a tutorial for multiple samples (github.com/slundberg/shap -> shap.plots.force(shap_values)). But when you try the matplotlib = True, it says that it's not possible with multiple samples. Any ideas why?
@NoobProgrammer I have no idea. Shap is finicky
It's because it makes interactive plot, and it has to be htm format :)
And due to the fact that by design you want to show the 'force' of variables of one observation of the model. Adding more observation would make the plot less intuitive. Anyway, one should also consider what is a good j-value. Personally, I prefer the best and worst prediction since there one can show why the model succeds/fails.
The documentation of SHAP is really bad.
-1

Given this fact in SHAP dacumentation and this thread, This works for me in Google Colab notebook:

# Attempt to display the SHAP force plot by saving to HTML and displaying
import shap
from IPython.display import display, HTML

# Create SHAP explainer (for three base models)
shap.initjs()
explainer = shap.TreeExplainer(forecaster.regressor)

#....

# Generate the force plot object
force_plot_object = shap.force_plot(

    matplotlib=False # Ensure it's not using matplotlib backend
)

# Save the plot to an HTML file
shap.save_html('force_plot.html', force_plot_object)

# Display the HTML file content
with open('force_plot.html', 'r') as f:
    html_content = f.read()

display(HTML(html_content))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.