SHAP for sentiment analysis

An example of SHAP on sentiment analysis. The SHAP explainer for NLP tasks only supports TextClassificationPipeline in the transformer library. If using this explainer, please cite the original work: https://github.com/slundberg/shap.

[1]:
import transformers
from omnixai.data.text import Text
from omnixai.explainers.nlp import ShapText
[2]:
# A transformer model for sentiment analysis
model = transformers.pipeline(
    'sentiment-analysis',
    model='distilbert-base-uncased-finetuned-sst-2-english',
    return_all_scores=True
)
[3]:
x = Text([
    "What a great movie! if you have no taste.",
    "it was a fantastic performance!",
    "best film ever",
    "such a great show!",
    "it was a horrible movie",
    "i've never watched something as bad"
])
explainer = ShapText(model=model)
explanations = explainer.explain(x)
explanations.ipython_plot()
Partition explainer: 7it [00:17,  3.51s/it]
Instance 0: Class POSITIVE
What a great movie ! if you have no taste .

Instance 1: Class POSITIVE
it was a fantastic performance !

Instance 2: Class POSITIVE
best film ever

Instance 3: Class POSITIVE
such a great show !

Instance 4: Class NEGATIVE
it was a horrible movie

Instance 5: Class POSITIVE
i ' ve never watched something as bad