Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shap.force_plot() raises Exeption: In v0.20 force_plot now requires the base value as the first parameter

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

like image 825
Francesco Zambolin Avatar asked Jun 27 '19 12:06

Francesco Zambolin


1 Answers

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!

like image 94
Sparsha Devapalli Avatar answered Sep 21 '22 06:09

Sparsha Devapalli