I am trying to improve the interpretability of a SpaCy binary text classification model I trained by explaining predictions using SHAP. Here's what I've tried so far (following this tutorial):
nlp = spacy.load("my_model") # load my model
explainer = shap.Explainer(nlp_predict)
shap_values = explainer(["This is an example"])
but I get AttributeError: 'str' object has no attribute 'shape'. nlp_predict is a method I wrote which takes a list of texts and outputs predicted probabilities for each text in the format that they use in the tutorial. What am I missing here?
Here's my formatting function:
def nlp_predict(texts):
result = []
for text in texts:
prediction = nlp_fn(text) # This returns label probability but in the wrong format
sub_result = []
sub_result.append({'label': 'label1', 'score': prediction["label1"]})
sub_result.append({'label': 'label2', 'score': prediction["label2"]})
result.append(sub_result)
return(result)
And here's the format of predictions they use in the tutorial (for 2 data points):
[[{'label': 'label1', 'score': 2.850986311386805e-05},
{'label': 'label2', 'score': 0.9999715089797974}],
[{'label': 'label1', 'score': 0.00010622951958794147},
{'label': 'label2', 'score': 0.9998937845230103}]]
My function's output matches this, but I still get the AttributeError. Here's the entire error message I get:
.