Shap: No output for force_plot

Created on 30 Sep 2018  路  4Comments  路  Source: slundberg/shap

import shap

def critical_factors(data_for_prediction):
    shap.initjs()
    background = X_train.iloc[0:100,:].values.astype('float')
    explainer = shap.DeepExplainer(model, background)
    shap_values = explainer.shap_values(data_for_prediction)
    shap.force_plot(explainer.expected_value[0], shap_values[0], data_for_prediction)

critical_factors( X_test.iloc[0:1,:].values.astype('float') )

Just shows a hexagon with JS symbol in it. No plot is visible in the Jupyter Notebook. Do I need to initialize something else?

Most helpful comment

The Jupyter notebook only displays the HTML object returned by force_plot because it is the final return value of the last statement in the block. So if you add return to the last statement in the function it should work.

All 4 comments

Looks like defining a function was the problem. The below works fine. It would be useful to mention this in the README or documentation.

shap.initjs()
data_for_prediction = X_test.iloc[0:1,:].values.astype('float')
background = X_train.iloc[0:100,:].values.astype('float')
explainer = shap.DeepExplainer(model, background)
shap_values = explainer.shap_values(data_for_prediction)
shap.force_plot(explainer.expected_value[0], shap_values[0], data_for_prediction)

However, the plot looses names of the features due to values() of Pandas. How to achieve the same using a DataFrame so that feature names are also visible in the plot?

screen shot 2018-09-30 at 3 38 32 pm

The Jupyter notebook only displays the HTML object returned by force_plot because it is the final return value of the last statement in the block. So if you add return to the last statement in the function it should work.

@slundberg Could you please answer my second comment. Thank you.

Ah, missed that one. You can just use the feature_names keyword argument to force_plot. It accepts a list of feature names (of the same length as shap_values.shape[1]).

Was this page helpful?
0 / 5 - 0 ratings