I’m trying to do some DRL explainability stuff, but I’m having some problems with SHAP.
I have a shap_explanation object called shap_values, from which I should be able to select a column using shap_values[:, column_name], but I’m getting that error (index 0 is out of bounds for axis 0 with size 0) instead.
I’ve also tried to select columns with shap_values[:, integer], which works fine with integer = 0 but doesn’t with any other integers, since I get an error as if shap_values only had one column.
I’m 100% sure shap_values has multiple columns, as print(np.shape((shap_values)) gives me (501,11).
I’m also 100% sure shap_values has the right feature names, since print(shap_values.feature_names)) gives me what I expect.
shap_values is type <class ‘shap._explanation.Explanation’>
Here’s the part of the code where the error happens. Error happens at the last line shown.
feature_names = ["T(t)", "C(t)", "W(t)", "T(t-3)", "F(t-3)", "T(t-2)", "F(t-2)", "T(t-1)", "F(t-1)", "T(t)", "F(t)"]
obss = np.squeeze(obss)
DF_obss = pd.DataFrame(data=obss, columns=feature_names)
explainer = shap.Explainer(model=pred, masker=DF_obss)
shap_values = explainer(DF_obss)
print(DF_obss.head(10))
print(type(shap_values))
print(shap_values.feature_names)
print(shap_values)
print(np.shape(shap_values))
for name in feature_names:
shap.plots.scatter(shap_values[:,name], show=False)
obss is an array of size (501,11) with all the observations given to my agent during testing.
Here’s the full error:
Traceback (most recent call last):
File “(…)/explainer.py”, line 107, in
explain(1, model, env)
File “(…)/explainer.py”, line 102, in explain
shap.plots.scatter(shap_values[:,name], show=False)
~~~~~~~~~~~^^^^^^^^
File “/opt/anaconda3/lib/python3.12/site-packages/shap/_explanation.py”, line 320, in getitem
t = np.argwhere(np.array(self.output_names) == t)[0][0]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: index 0 is out of bounds for axis 0 with size 0
I know that it should work because the code is copied from something else I did just a few days ago, and it worked just fine then. I genuinely have no idea what I did wrong.
NathanG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.