I am working on a binary classification using random forest and trying out SHAP to explain the model predictions.
However, I would like to convert the SHAP local explanation plots with values into a pandas dataframe for each instance.
Is there any one here who can help me with exporting SHAP local explanations to pandas dataframe for each instance?
I know that SHAPASH has .to_pandas()
method but couldn't find anything like that in SHAP
I tried something like below based on the SO post here but it doesn't help
feature_names = shap_values.feature_names
shap_df = pd.DataFrame(shap_values.values, columns=feature_names)
vals = np.abs(shap_df.values).mean(0)
shap_importance = pd.DataFrame(list(zip(feature_names, vals)), columns=['col_name', 'feature_importance_vals'])
shap_importance.sort_values(by=['feature_importance_vals'], ascending=False, inplace=True)
I expect my output something like below. Here, negative sign indicates feature contribution for class 0 and positive values indicates feature contribution for class 1
subject_id Feature importance value (contribution)
1 F1 31
1 F2 27
1 F3 20
1 F5 - 10
1 F9 - 29