Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a feature importance from SHAP Values

iw ould like to get a dataframe of important features. With the code below i have got the shap_values and i am not sure, what do the values mean. In my df are 142 features and 67 experiments, but got an array with ca. 2500 values.

explainer = shap.TreeExplainer(rf)
shap_values = explainer.shap_values(X_test)

shap.summary_plot(shap_values, X_test, plot_type="bar")

enter image description here

I have tried to store them in a df:

rf_resultX = pd.DataFrame(shap_values, columns = ['shap_values'])

but got: ValueError: Shape of passed values is (18, 142), indices imply (18, 1)

142 - the number of the features. 18 - i have no idea.

I believe it works as follows:

  • shap_values need to be averaged.
  • and paired with the feature names: pd.DataFrame(feature_names, columns = ['feature_names'])

Does anybody have an experience, how to interpret shap_values? At first i thought, that the number of values are the number of features x number of rows.

like image 482
Parsyk Avatar asked Jan 23 '26 12:01

Parsyk


2 Answers

Combining the other two answers like this worked for me.

feature_names = X_train.columns


rf_resultX = pd.DataFrame(shap_values, columns = feature_names)

vals = np.abs(rf_resultX.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)
shap_importance.head()
like image 90
sengul_karaderili Avatar answered Jan 25 '26 02:01

sengul_karaderili


From https://github.com/slundberg/shap/issues/632

vals = np.abs(shap_values.values).mean(0)
feature_names = train_x.columns()

feature_importance = pd.DataFrame(list(zip(feature_names, vals)),
                                 columns=['col_name','feature_importance_vals'])
feature_importance.sort_values(by=['feature_importance_vals'],
                              ascending=False, inplace=True)
feature_importance.head()
like image 35
banderlog013 Avatar answered Jan 25 '26 03:01

banderlog013



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!