Shap: get SHAP value for each feature

Created on 24 Dec 2019  路  2Comments  路  Source: slundberg/shap

Is it possible to the SHAP value for each feature?

Most helpful comment

From the docstrings of the shap_values method, we can see that:

For models with a single output this returns a matrix of SHAP values 
(# samples x # features)

So unless you have a different situation/case, you should be getting # samples arrays with SHAP values for each feature when calling this method.
Do let me know if this does not work for you.

All 2 comments

From the docstrings of the shap_values method, we can see that:

For models with a single output this returns a matrix of SHAP values 
(# samples x # features)

So unless you have a different situation/case, you should be getting # samples arrays with SHAP values for each feature when calling this method.
Do let me know if this does not work for you.

Unless I'm mistaken, you can simply average over the SHAP values for each sample for each feature. e.g. here's a snippet:
mean_shap_feature_values = pd.DataFrame(shap_values, columns=your_xs_dataframe.columns).abs().mean(axis=0).sort_values(ascending=False)
These should align with your simple SHAP importance plot (summary_plot with plot_type='bar').

It would be a nice convenience function for SHAP to expose these "importances" with an API like SKL's rf.feature_importances_ or similar.

Was this page helpful?
0 / 5 - 0 ratings