Shap: AttributeError: 'Index' object has no attribute 'to_list' in function decision_plot

Created on 12 Dec 2019  路  7Comments  路  Source: slundberg/shap

When trying to call the function "shap.decision_plot", I get the aforementioned error. It seems that it is an issue related to the use of "to_list()".

explainer = shap.TreeExplainer(rf_model)
shap.decision_plot(explainer.expected_value[1], explainer.shap_values(X6.iloc[64])[1], X6.iloc[64])

AttributeError Traceback (most recent call last)
in
----> 1 shap.decision_plot(explainer.expected_value[1], explainer.shap_values(X6.iloc[64])[1], X6.iloc[64])

~\AppData\Local\Continuum\anaconda3\lib\site-packages\shap\plots\decision.py in decision_plot(base_value, shap_values, features, feature_names, feature_order, feature_display_range, highlight, link, plot_color, axis_color, y_demarc_color, alpha, color_bar, auto_size_plot, title, xlim, show, return_objects, ignore_warnings, matplotlib)
323 elif str(type(features)) == "":
324 if feature_names is None:
--> 325 feature_names = features.index.to_list()
326 features = features.values
327 elif isinstance(features, list):

AttributeError: 'Index' object has no attribute 'to_list'

Any help would be really appreciated.
Thank you very much.

Regards,
David

Most helpful comment

You may have an older version of Pandas installed. I suggest upgrading. At some point, the standard method name changed from tolist() to to_list(). If upgrading doesn't work, please run the following script and post the output again.

import pandas as pd
print(pd.__version__)
a = pd.Series(range(4), ['a', 'b', 'c', 'd'])
print(a.index.tolist())
print(a.index.to_list())

All 7 comments

@floidgilbert thoughts?

@davidgar: Thanks for including the trace. Please execute the following and send me the output.

f = X6.iloc[64]
print(type(f.index))
print(len(f.index.to_list()))

Thank you very much @slundberg and @floidgilbert for your help and prompt response.
Please, find attached the screenshot of the output obtained after the execution.
shapIssueJPG
.

You may have an older version of Pandas installed. I suggest upgrading. At some point, the standard method name changed from tolist() to to_list(). If upgrading doesn't work, please run the following script and post the output again.

import pandas as pd
print(pd.__version__)
a = pd.Series(range(4), ['a', 'b', 'c', 'd'])
print(a.index.tolist())
print(a.index.to_list())

Thank you again @floidgilbert . As you suggested, I upgraded Pandas and (after some dependencies issues), the original "tolist"-"to_list()" issue seems to be fixed.
I have not been able to completely check as I'm getting a new error message when calling "TreeExplainer" under a Random Forest Classifier model.
I'm not sure if I should open a new issue. Anyway, I'm sending a screenshot of this new error.
Thank you in advance.

seed = 0

rf_params2 = { 'n_jobs': -1, 'n_estimators': 500, 'max_features': 0.3, 'max_depth': 100, 'min_samples_leaf': 2, 'max_features': 'sqrt', 'random_state': seed, 'verbose': 0, 'class_weight': 'balanced' }
rf_clf2 = ensemble.RandomForestClassifier(**rf_params2)
TreeExplainerIssue

Glad it worked. As for the new issue, see #928. You will need to install SHAP from the GitHub repository unless you want to wait for the next release. For example: pip install git+https://github.com/slundberg/shap.git

Thank you very much @floidgilbert . Everything is working perfectly fine now.
Thanks for your help and support.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yolle103 picture yolle103  路  3Comments

resdntalien picture resdntalien  路  3Comments

GitAnalyst picture GitAnalyst  路  3Comments

gabrielcs picture gabrielcs  路  3Comments

samupino picture samupino  路  3Comments