8

I'm using Catboost and would like to visualize shap_values:

from catboost import CatBoostClassifier
model = CatBoostClassifier(iterations=300)
model.fit(X, y,cat_features=cat_features)

pool1 = Pool(data=X, label=y, cat_features=cat_features)
shap_values = model.get_feature_importance(data=pool1, fstr_type='ShapValues', verbose=10000)

shap_values.shape
Output: (32769, 10)
X.shape
Output: (32769, 9)

Then I do the following and an exception is raised:

shap.initjs()
shap.force_plot(shap_values[0,:-1], X.iloc[0,:])

Exception: In v0.20 force_plot now requires the base value as the first parameter! Try shap.force_plot(explainer.expected_value, shap_values) or for multi-output models try shap.force_plot(explainer.expected_value[0], shap_values[0]).

The following works, but I would like to make force_plot() work:

shap.initjs()
shap.summary_plot(shap_values[:,:-1], X)

I read the Documentation but can't make sense of explainer. I tried:

explainer = shap.TreeExplainer(model,data=pool1)
#Also tried:
explainer = shap.TreeExplainer(model,data=X)

but I get: TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Can anyone point me in the right direction? THX

3 Answers 3

1

I had the same error as below-

Exception: In v0.20 force_plot now requires the base value as the first parameter! Try shap.force_plot(explainer.expected_value, shap_values) or for multi-output models try shap.force_plot(explainer.expected_value[0], shap_values[0]).

This helped me resolve the issue-

import shap
explainer = shap.TreeExplainer(model,data=X)
shap.initjs()
shap.force_plot(explainer.expected_value[0],X.iloc[0,:])

Also for the below issue -

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Check your data, if it contains any NaN's or missing values.
Hope this helps!

0

try this:

shap.force_plot(explainer.expected_value, shap_values.values[0, :], X.iloc[0, :])
1
  • Doesn't work, I get AttributeError: 'list' object has no attribute 'values'.
    – PJ_
    Commented Jul 22, 2022 at 15:04
0

Building on @Sparsha's answer, since I was still getting errors, what worked for me was:

explainer = shap.TreeExplainer(model, data = X)
shap_values = explainer.shap_values(X_train)
shap.initjs()
shap.force_plot(explainer.expected_value, shap_values[0], feature_names = explainer.data_feature_names)
          

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.