Shap: predict() got an unexpected keyword argument 'validate_features'

Created on 14 Mar 2019  路  14Comments  路  Source: slundberg/shap

Using
Xgboost version: 0.7.post3

SHAP version: 0.28.5

TypeError Traceback (most recent call last)
in
15 explainer = shap.TreeExplainer(model)
16
---> 17 shap_values = explainer.shap_values(X)
18
19 # visualize the first prediction's explanation (use matplotlib=True to avoid Javascript)

~/anaconda3/envs/propensity/lib/python3.7/site-packages/shap/explainers/tree.py in shap_values(self, X, y, tree_limit, approximate)
177 phi = self.model.original_model.predict(
178 X, ntree_limit=tree_limit, pred_contribs=True,
--> 179 approx_contribs=approximate, validate_features=False
180 )
181

TypeError: predict() got an unexpected keyword argument 'validate_features'

Most helpful comment

Yes thanks @SaadAhmed96 that solved the issue.

All 14 comments

I'm also seeing this error, running shap in Colab with xgboost version 0.7 and shap 0.28.5. Here's my model

model = xgb.train({'objective': 'binary:logistic'}, dmatrix)

I'm able to run the code to create the explainer without errors:

explainer = shap.TreeExplainer(model)

But when I run: shap_values = explainer.shap_values(np.array(x_test[:10])) I get the same error that @gaush123 ran into. Let me know if I can provide more details.

I guess you need to create a DMatrix for the test set as well or you can also try X_test.as_matrix() to get the shap values. Let me know if this works.

Thanks @SaadAhmed96 it worked.
changing
shap_values = explainer.shap_values(X)
to
shap_values = explainer.shap_values(X.as_matrix())
worked

I'm also running into the same error. I am using shap in Colab with xgboost version 0.7 and shap 0.28.5.
I used the suggestion of changing X to X.as_matrix() but that does not work either. Is there anything else that I can do?

shap: 0.28.5
xgboost: 0.7.post4
Same issue. The example given in the read of shap for xgboost gives same error. X.as_matrix() does not help.

Can you post the full example here so that I can look at it? Maybe something else is causing issues.

print(shap.__version__)
print(xgb.__version__)
shap.initjs()

X,y = shap.datasets.boston()
model = xgb.train({"learning_rate": 0.01}, xgb.DMatrix(X, label=y), 100)

explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X.as_matrix())

shap.force_plot(explainer.expected_value, shap_values[0,:], X.iloc[0,:])

This is the same from the example README. using a Dmatrix or normal X without as_matrix() gives the same error.

## Output

0.28.5
0.7.post4

TypeError Traceback (most recent call last)

in ()
10 # (same syntax works for LightGBM, CatBoost, and scikit-learn models)
11 explainer = shap.TreeExplainer(model)
---> 12 shap_values = explainer.shap_values(X.as_matrix())
13
14 # visualize the first prediction's explanation (use matplotlib=True to avoid Javascript)

/usr/local/lib/python3.6/dist-packages/shap/explainers/tree.py in shap_values(self, X, y, tree_limit, approximate)
177 phi = self.model.original_model.predict(
178 X, ntree_limit=tree_limit, pred_contribs=True,
--> 179 approx_contribs=approximate, validate_features=False
180 )
181

TypeError: predict() got an unexpected keyword argument 'validate_features'

Thanks for posting this example. It works fine on my system. May be its because of the xgboost version you are having issues. Try updating it.

image

Yes thanks @SaadAhmed96 that solved the issue.

I'm experiencing the same error on the example above. Shap 0.28.5 and Xgboost 0.82:

X,y = shap.datasets.boston()
model = xgb.train({"learning_rate": 0.01}, xgb.DMatrix(X, label=y), 100)

explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X.as_matrix())

shap.force_plot(explainer.expected_value, shap_values[0,:], X.iloc[0,:])

TypeError: predict() got an unexpected keyword argument 'validate_features'

@joshw66 that's surprising. Could you share the full output (along with xgboost.__version__ just to be sure)

@slundberg sorry eventually realised it was an issue with installing most recent version of xgboost, worked fine once I'd resoved that. Thanks for getting back anyway

Is there any update on this issue? I'm having the same issue with xgboost '0.7.post3' and shap '0.29.3'. @slundberg

it seems xgboost 0.7 version have no 'validate_features' but '_validate_features'

Was this page helpful?
0 / 5 - 0 ratings

Related issues

samupino picture samupino  路  3Comments

shoaibkhanz picture shoaibkhanz  路  4Comments

1vecera picture 1vecera  路  3Comments

brookewenig picture brookewenig  路  3Comments

Nithanaroy picture Nithanaroy  路  4Comments