Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the exact SHAP importance values? #33

Closed
wishforgood opened this issue Jan 18, 2024 · 3 comments
Closed

How to get the exact SHAP importance values? #33

wishforgood opened this issue Jan 18, 2024 · 3 comments

Comments

@wishforgood
Copy link

wishforgood commented Jan 18, 2024

For example I have a BoostRFE object, and have called the model.fit() function of it, then how can I get the exact list of SHAP importance corresponding to each features selected by the model's estimator (just like what shap.plot.bar shows)? The values of the output from shap.Explainer seems not that straightforward to me, I have not found any info about the shap or meaning of those values.

Or can I get the SHAP value directly through the BoostRFE object?

@wishforgood
Copy link
Author

Oh maybe I should ask it in the git of shap

@wishforgood
Copy link
Author

When I use the model.estimator_ to calculate SHAP values, I found that some features corresponds to NaN values, maybe because I used the min_features_to_select option? How can I get the SHAP values used to select the features?

@wishforgood wishforgood reopened this Jan 18, 2024
@cerlymarco
Copy link
Owner

Hi,
in shap-hypetune, shap values are computed using shap.TreeExplainer with feature_perturbation="tree_path_dependent". shap importances are aggregated featurewise using this aggregation: np.abs(shap_values).mean(0). No method is available in shap-hypetune to extract/plot shap importances (since you can directly use the shap library).
To get the shap importances of the selected features, compute the shape values on your data (train or evaluation set using only the selected features) and aggregate them with the calculation np.abs(shap_values).mean(0)

explainer = TreeExplainer(
model, feature_perturbation="tree_path_dependent")
coefs = explainer.shap_values(X)
if isinstance(coefs, list):
coefs = list(map(lambda x: np.abs(x).mean(0), coefs))
coefs = np.sum(coefs, axis=0)
else:
coefs = np.abs(coefs).mean(0)

if u support the project, don't forget to leave a star ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants