Open shaddyab opened 4 years ago
@shaddyab this looks like something that I should take a look at as I recently wrote that code
Can be fixed by adding the following before passing the model to TreeExplainer
, if your booster name is gbm
.
gbm.params['objective'] = 'binary' # or whatever is appropriate
@shaddyab I've sent a preliminary PR but I'm not able to reproduce this issue. Also I noticed someone sent a similar PR as well.
https://github.com/slundberg/shap/pull/1062
can you provide the code for how you built Final_Model?
@mu-wang I recall trying this trick and it didn't resolve the problem. In any case, I have a regression model not a classification model, would not setting the objective function to binary affect the Shap values?
I ran into this issue, also with a regression target. This fixed it for me:
model.params["objective"] = "regression"
explainer = shap.TreeExplainer(model)
Hi, I ran into this problem today, I find if I directly use result from lgb.train, there would be no problem, but if I reload a lightgbm model from file, likelgbmodel=lgb.Booster(model_file='a.lgb')
, then explainer.shap_values would raise this exception, I think maybe this exception is caused because lightgbm doesn't correctly reload 'binary' or 'regression' into model.params["objective"], so adding
lgbmodel.params["objective"] = "binary"
would fix this problem.
thank you for the details, I was able to reproduce the issue with non-sklearn API, so I think this PR does fix the issue:
I fixed this issue by running this:
explainer = shap.TreeExplainer(model)
if isinstance(model, lgb.LGBMClassifier):
explainer.model.original_model.params['objective'] = 'binary'
This issue has been inactive for two years, so it's been automatically marked as 'stale'.
We value your input! If this issue is still relevant, please leave a comment below. This will remove the 'stale' label and keep it open.
If there's no activity in the next 90 days the issue will be closed.
When executing the following code to find the features importance of a LGBMRegressor model I get a KeyError: 'objective' error. The model's objective was specified as 'mean_squared_error'.
Code:
Error:
Versions: shap version = 0.34.0 LightGBM version = 2.3.1
Model:
Update: I can plot the shap values if they are predicted directly. Final_Model.predict(X_Predict, pred_contrib=True)