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

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

Closed
3 of 4 tasks
craftsliu opened this issue May 17, 2024 · 4 comments
Labels
bug Indicates an unexpected problem or unintended behaviour

Comments

@craftsliu
Copy link

Issue Description

run this demo:https://github.com/shap/shap/blob/master/notebooks/tabular_examples/tree_based_models/Census%20income%20classification%20with%20LightGBM.ipynb
image


TypeError Traceback (most recent call last)
/tmp/ipykernel_52792/960682606.py in ()
----> 1 shap.force_plot(
2 explainer.expected_value, shap_values.values[1, :], X_display.iloc[0, :]
3 )

~/anaconda3/envs/myenv/lib/python3.8/site-packages/shap/plots/_force.py in force(base_value, shap_values, features, feature_names, out_names, link, plot_cmap, matplotlib, show, figsize, ordering_keys, ordering_keys_time_format, text_rotation, contribution_threshold)
128 "shap.plots.force(explainer.expected_value[0], shap_values[0])."
129 )
--> 130 raise TypeError(emsg)
131
132 if isinstance(shap_values, list):

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

sample order by similarity

f(x)

Minimal Reproducible Example

import lightgbm as lgb
from sklearn.model_selection import train_test_split

import shap

# print the JS visualization code to the notebook
shap.initjs()

X, y = shap.datasets.adult()
X_display, y_display = shap.datasets.adult(display=True)

# create a train/test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=7)
d_train = lgb.Dataset(X_train, label=y_train)
d_test = lgb.Dataset(X_test, label=y_test)

params = {
    "max_bin": 512,
    "learning_rate": 0.05,
    "boosting_type": "gbdt",
    "objective": "binary",
    "metric": "binary_logloss",
    "num_leaves": 10,
    "verbose": -1,
    "min_data": 100,
    "boost_from_average": True,
    "early_stopping_round": 50,
}

model = lgb.train(
    params,
    d_train,
    10000,
    valid_sets=[d_test],
)
explainer = shap.TreeExplainer(model)
shap_values = explainer(X)
shap.force_plot(
    explainer.expected_value, shap_values.values[1, :], X_display.iloc[0, :]
)

Traceback

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_52792/960682606.py in ()
----> 1 shap.force_plot(
      2     explainer.expected_value, shap_values.values[1, :], X_display.iloc[0, :]
      3 )

~/anaconda3/envs/myenv/lib/python3.8/site-packages/shap/plots/_force.py in force(base_value, shap_values, features, feature_names, out_names, link, plot_cmap, matplotlib, show, figsize, ordering_keys, ordering_keys_time_format, text_rotation, contribution_threshold)
    128                 "shap.plots.force(explainer.expected_value[0], shap_values[0])."
    129             )
--> 130             raise TypeError(emsg)
    131 
    132     if isinstance(shap_values, list):

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

sample order by similarity

f(x)
<Figure size 800x630 with 2 Axes>

Expected Behavior

No response

Bug report checklist

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest release of shap.
  • I have confirmed this bug exists on the master branch of shap.
  • I'd be interested in making a PR to fix this bug

Installed Versions

0.44.1

@craftsliu craftsliu added the bug Indicates an unexpected problem or unintended behaviour label May 17, 2024
@IlyaSwallow
Copy link

I have the same bug(

@nprime496
Copy link

+1

@thatlittleboy
Copy link
Collaborator

I can't reproduce this bug (using the MWE in the original post above) on the latest version 0.46.0.
It might have been fixed recently after we standardized the shape of expected value and shap values returned by the TreeExplainer for various Tree ensemble models.

image

If anyone can confirm that this issue persists >= version 0.46.0, feel free to reply here with a reproducible example and I'll be happy to reopen the issue.
Thanks.

@Nacriema
Copy link

Nacriema commented Sep 5, 2024

I had the same issue. Does this problem related to the version of SHAP? Currently I'm using SHAP 0.44.1. And followed the visualization steps like the document.

Edited: By following the example script at v.0.44.1, then everything works like a charm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behaviour
Projects
None yet
Development

No branches or pull requests

5 participants