Skip to content

Instantly share code, notes, and snippets.

@yptheangel
Created December 22, 2021 07:18
Show Gist options
  • Save yptheangel/f9e61a8b54feeb8c2b82a4fd1f0c7dd0 to your computer and use it in GitHub Desktop.
Save yptheangel/f9e61a8b54feeb8c2b82a4fd1f0c7dd0 to your computer and use it in GitHub Desktop.
shap feature importance
import numpy as np
import pandas as pd
import shap
explainer = shap.TreeExplainer("your_tree_model")
shap_values = explainer.shap_values(X_train)
scores= np.abs(shap_values).mean(0)
feature_importance = pd.DataFrame(list(zip(X_train.columns, sum(scores))), columns=['feature','feature_importance_score'])
feature_importance.sort_values(by=['feature_importance_score'], ascending=False,inplace=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment