Positive vs. Negative Sentiment Classification

In this notebook, we demonstrate how to interpret a sentiment classification model using SHAP. The goal is to understand how individual words in a movie review influence the model’s prediction of positive or negative sentiment.

[1]:
import datasets
import numpy as np
import transformers

import shap
/Users/aribaa/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
  warnings.warn(
/Users/aribaa/Library/Python/3.9/lib/python/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm

Load the IMDB movie review dataset

[2]:
# Load IMDB dataset (movie reviews labeled as positive/negative)
dataset = datasets.load_dataset("imdb", split="test")

# shorten the strings to fit into the pipeline model
short_data = [v[:500] for v in dataset["text"][:20]]

Load and run a sentiment analysis pipeline

[3]:
# Load pretrained sentiment analysis model from HuggingFace
# Note: Model will be downloaded on first run
classifier = transformers.pipeline("sentiment-analysis", return_all_scores=True)
classifier(short_data[:2])
No model was supplied, defaulted to distilbert/distilbert-base-uncased-finetuned-sst-2-english and revision 714eb0f (https://huggingface.co/distilbert/distilbert-base-uncased-finetuned-sst-2-english).
Using a pipeline without specifying a model name and revision in production is not recommended.
Device set to use mps:0
/Users/aribaa/Library/Python/3.9/lib/python/site-packages/transformers/pipelines/text_classification.py:111: UserWarning: `return_all_scores` is now deprecated,  if want a similar functionality use `top_k=None` instead of `return_all_scores=True` or `top_k=1` instead of `return_all_scores=False`.
  warnings.warn(
[3]:
[[{'label': 'NEGATIVE', 'score': 0.07581914216279984},
  {'label': 'POSITIVE', 'score': 0.924180805683136}],
 [{'label': 'NEGATIVE', 'score': 0.01834261603653431},
  {'label': 'POSITIVE', 'score': 0.9816573858261108}]]

Explain the sentiment analysis pipeline

[4]:
# Create SHAP explainer to compute word-level importance
explainer = shap.Explainer(classifier)
[5]:
# explain the predictions of the pipeline on the first two samples
shap_values = explainer(short_data[:2])
PartitionExplainer explainer: 3it [00:11, 11.25s/it]
[6]:
# Visualize contribution of each word to the prediction for the "POSITIVE" class
shap.plots.text(shap_values[:, :, "POSITIVE"])


[0]
0.70.40.111.30.4371090.437109base value0.9241810.924181fPOSITIVE(inputs)0.268 good 0.059 sci 0.058 to good 0.055 but 0.045 love sci 0.044 it is 0.039 fi 0.039 -fi 0.038 to Star Trek 0.035 as Babylon 0.034 TV 0.033 5 is 0.03 sci 0.03 m sure 0.021 I 0.02 I 0.016 am willing to put up with a lot. 0.016 ' 0.015 (the original) 0.014 - 0.012 -fi and 0.007 I really 0.006 think 0.006 did 0.005 . 0.002 , -0.049 who -0.037 there -0.028 painfully one-dimensional -0.028 is -0.027 misunderstood. -0.022 cheap cardboard sets, -0.019 I tried -0.016 underfunded, -0.016 stilted dialogues, -0.016 under -0.016 - -0.015 and -0.014 there are -0.014 match the background -0.014 and -0.013 Sci-fi movies/TV are usually -0.013 appreciated -0.013 a 'sci-fi' setting. -0.011 to like this -0.01 be overcome with -0.009 t -0.009 doesn' -0.008 cannot -0.007 CG that -0.006 Babylon -0.006 , -0.005 5 -0.004 you -0.004 -0.004 , -0.003 , -0.002 Silly prosthetics -0.002 those of -0.001 characters -0.0 out -0.0 (
inputs
0.021 / 2
I
0.045 / 2
love sci
0.012 / 3
-fi and
0.016 / 9
am willing to put up with a lot.
-0.013 / 8
Sci-fi movies/TV are usually
-0.016 / 4
underfunded,
-0.016
under
-0.016
-
-0.013
appreciated
-0.015
and
-0.027 / 2
misunderstood.
-0.019 / 2
I tried
-0.011 / 3
to like this
-0.004
,
0.007 / 2
I really
0.006
did
-0.003
,
0.055
but
0.044 / 2
it is
0.058 / 2
to good
0.034
TV
0.03
sci
0.039 / 2
-fi
0.035 / 2
as Babylon
0.033 / 2
5 is
0.038 / 3
to Star Trek
0.015 / 4
(the original)
0.005
.
-0.002 / 5
Silly prosthetics
0.002
,
-0.022 / 4
cheap cardboard sets,
-0.016 / 4
stilted dialogues,
-0.007 / 3
CG that
-0.009 / 2
doesn'
-0.009
t
-0.014 / 3
match the background
-0.006
,
-0.014
and
-0.028 / 4
painfully one-dimensional
-0.001
characters
-0.008
cannot
-0.01 / 3
be overcome with
-0.013 / 8
a 'sci-fi' setting.
-0.0
(
0.02
I
0.016
'
0.03 / 2
m sure
-0.014 / 2
there are
-0.002 / 2
those of
-0.004
you
-0.0
out
-0.037
there
-0.049
who
0.006
think
-0.006
Babylon
-0.005
5
-0.028
is
0.268
good
0.059
sci
0.014
-
0.039
fi
-0.004


[1]
0.70.40.111.30.4611710.461171base value0.9816570.981657fPOSITIVE(inputs)0.164 Worth 0.115 interesting 0.085 entertaining 0.076 by 0.075 entertainment 0.048 is 0.047 is made 0.043 the 0.036 . 0.036 action movies 0.031 you 0.029 Damme kick style, 0.026 fights with the great Van 0.02 this 0.019 like 0.019 and 0.018 competently 0.017 0.016 the inclusion 0.015 features the 0.013 <br /><br /> 0.011 especially 0.009 This one 0.009 which is clever but 0.009 you' 0.007 , 0.007 handled 0.006 . 0.006 and 0.004 . 0.004 , 0.003 All of 0.003 The plot 0.003 ve seen 0.002 you away if 0.001 usual car chases 0.001 shooting battles with the -0.114 but -0.111 nothing -0.054 really -0.035 hardly -0.033 that -0.031 40 shell load shotgun -0.023 of a rabbit -0.018 profound -0.016 . -0.015 style bombs -0.015 even terrorist -0.014 , -0.012 there is -0.006 Many of -0.005 the c -0.004 , -0.002 blows -0.002 value of a rental -0.001 your share before -0.0 if
inputs
0.017
0.164
Worth
0.043
the
0.075
entertainment
-0.002 / 4
value of a rental
0.007
,
0.011
especially
-0.0
if
0.031
you
0.019
like
0.036 / 2
action movies
0.036
.
0.009 / 2
This one
0.015 / 2
features the
0.001 / 3
usual car chases
0.004
,
0.026 / 5
fights with the great Van
0.029 / 5
Damme kick style,
0.001 / 4
shooting battles with the
-0.031 / 4
40 shell load shotgun
-0.004
,
0.006
and
-0.015 / 2
even terrorist
-0.015 / 2
style bombs
0.004
.
0.003 / 2
All of
0.02
this
0.048
is
0.085
entertaining
0.019
and
0.018 / 2
competently
0.007
handled
-0.114
but
-0.012 / 2
there is
-0.111
nothing
-0.033
that
-0.054
really
-0.002
blows
0.002 / 3
you away if
0.009 / 2
you'
0.003 / 2
ve seen
-0.001 / 3
your share before
0.006
.
0.013 / 8
<br /><br />
0.003 / 2
The plot
0.047 / 2
is made
0.115
interesting
0.076
by
0.016 / 2
the inclusion
-0.023 / 3
of a rabbit
-0.014
,
0.009 / 4
which is clever but
-0.035
hardly
-0.018
profound
-0.016
.
-0.006 / 2
Many of
-0.005 / 3
the c

Wrap the pipeline manually

SHAP explanations operate best in additive feature spaces. Since probabilities are not additive, we convert them into logits (log-odds). This ensures that SHAP values correctly sum up to the model output.

Create a TransformersPipeline wrapper

[7]:
pmodel = shap.models.TransformersPipeline(classifier, rescale_to_logits=False)
[8]:
pmodel(short_data[:2])
[8]:
array([[0.07581914, 0.92418081],
       [0.01834262, 0.98165739]])
[9]:
pmodel = shap.models.TransformersPipeline(classifier, rescale_to_logits=True)
pmodel(short_data[:2])
[9]:
array([[-2.50055699,  2.50055625],
       [-3.98001525,  3.98001536]])
[10]:
explainer2 = shap.Explainer(pmodel)
shap_values2 = explainer2(short_data[:2])
shap.plots.text(shap_values2[:, :, 1])
PartitionExplainer explainer: 3it [00:17, 17.59s/it]


[0]
1-2-5-84710-0.252905-0.252905base value2.500562.50056fPOSITIVE(inputs)1.448 it is to good 1.422 good 1.206 as Babylon 5 is 0.913 TV sci 0.76 but 0.713 -fi 0.661 to Star Trek 0.567 I love sci 0.454 sci 0.428 -fi 0.421 (the original). 0.385 -fi and 0.246 m sure 0.229 I' 0.219 up with a lot. 0.169 am willing to put 0.163 I really did, 0.078 is 0.034 5 0.008 , 0.002 -0.661 characters cannot be overcome with -0.609 cheap cardboard sets, -0.544 and -0.518 painfully one -0.512 doesn't -0.494 match the background -0.491 -dimensional -0.467 stilted dialogues, -0.412 under- -0.383 and misunderstood. -0.347 appreciated -0.343 who -0.285 there -0.279 CG that -0.257 , -0.216 Silly prosthetics, -0.2 underfunded, -0.188 a 'sci-fi' setting. -0.135 Sci-fi movies/TV are usually -0.121 there are -0.09 think -0.076 those of -0.064 you out -0.041 I tried to like this -0.033 Babylon -0.01 (
inputs
0.567 / 4
I love sci
0.385 / 3
-fi and
0.169 / 4
am willing to put
0.219 / 5
up with a lot.
-0.135 / 8
Sci-fi movies/TV are usually
-0.2 / 4
underfunded,
-0.412 / 2
under-
-0.347
appreciated
-0.383 / 3
and misunderstood.
-0.041 / 5
I tried to like this
0.008
,
0.163 / 4
I really did,
0.76
but
1.448 / 4
it is to good
0.913 / 2
TV sci
0.713 / 2
-fi
1.206 / 4
as Babylon 5 is
0.661 / 3
to Star Trek
0.421 / 5
(the original).
-0.216 / 6
Silly prosthetics,
-0.609 / 4
cheap cardboard sets,
-0.467 / 4
stilted dialogues,
-0.279 / 3
CG that
-0.512 / 3
doesn't
-0.494 / 3
match the background
-0.257
,
-0.544
and
-0.518 / 2
painfully one
-0.491 / 2
-dimensional
-0.661 / 5
characters cannot be overcome with
-0.188 / 8
a 'sci-fi' setting.
-0.01
(
0.229 / 2
I'
0.246 / 2
m sure
-0.121 / 2
there are
-0.076 / 2
those of
-0.064 / 2
you out
-0.285
there
-0.343
who
-0.09
think
-0.033
Babylon
0.034
5
0.078
is
1.422
good
0.454
sci
0.428 / 2
-fi
0.002


[1]
1-2-5-84710-0.155629-0.155629base value3.980023.98002fPOSITIVE(inputs)1.277 interesting by 1.247 Worth 0.964 entertaining 0.615 entertainment 0.52 the inclusion 0.517 competently handled 0.456 the 0.455 is 0.385 and 0.342 The plot is made 0.331 this 0.328 Damme kick style, 0.326 fights with the great Van 0.314 This one features the usual car chases, 0.314 especially if 0.314 0.291 action movies 0.278 . 0.276 you like 0.186 All of 0.169 value of a rental 0.146 you've seen 0.115 your share before. 0.096 but 0.092 and 0.061 <br /><br /> 0.056 Many of the c 0.021 which is clever -1.739 but -1.313 nothing -1.057 hardly profound -0.556 . -0.546 that -0.327 really blows -0.318 there is -0.228 you away if -0.105 of a rabbit -0.078 40 shell load shotgun, -0.073 even terrorist style bombs. -0.01 shooting battles with the -0.003 , -0.001 ,
inputs
0.314
1.247
Worth
0.456
the
0.615
entertainment
0.169 / 4
value of a rental
-0.001
,
0.314 / 2
especially if
0.276 / 2
you like
0.291 / 2
action movies
0.278
.
0.314 / 8
This one features the usual car chases,
0.326 / 5
fights with the great Van
0.328 / 5
Damme kick style,
-0.01 / 4
shooting battles with the
-0.078 / 5
40 shell load shotgun,
0.092
and
-0.073 / 5
even terrorist style bombs.
0.186 / 2
All of
0.331
this
0.455
is
0.964
entertaining
0.385
and
0.517 / 3
competently handled
-1.739
but
-0.318 / 2
there is
-1.313
nothing
-0.546
that
-0.327 / 2
really blows
-0.228 / 3
you away if
0.146 / 4
you've seen
0.115 / 4
your share before.
0.061 / 8
<br /><br />
0.342 / 4
The plot is made
1.277 / 2
interesting by
0.52 / 2
the inclusion
-0.105 / 3
of a rabbit
-0.003
,
0.021 / 3
which is clever
0.096
but
-1.057 / 2
hardly profound
-0.556
.
0.056 / 5
Many of the c

Pass a tokenizer as the masker object

[11]:
explainer2 = shap.Explainer(pmodel, classifier.tokenizer)
shap_values2 = explainer2(short_data[:2])
shap.plots.text(shap_values2[:, :, 1])
PartitionExplainer explainer: 3it [00:13, 13.83s/it]


[0]
1-2-5-84710-0.252905-0.252905base value2.500562.50056fPOSITIVE(inputs)1.448 it is to good 1.422 good 1.206 as Babylon 5 is 0.913 TV sci 0.76 but 0.713 -fi 0.661 to Star Trek 0.567 I love sci 0.454 sci 0.428 -fi 0.421 (the original). 0.385 -fi and 0.246 m sure 0.229 I' 0.219 up with a lot. 0.169 am willing to put 0.163 I really did, 0.078 is 0.034 5 0.008 , 0.002 -0.661 characters cannot be overcome with -0.609 cheap cardboard sets, -0.544 and -0.518 painfully one -0.512 doesn't -0.494 match the background -0.491 -dimensional -0.467 stilted dialogues, -0.412 under- -0.383 and misunderstood. -0.347 appreciated -0.343 who -0.285 there -0.279 CG that -0.257 , -0.216 Silly prosthetics, -0.2 underfunded, -0.188 a 'sci-fi' setting. -0.135 Sci-fi movies/TV are usually -0.121 there are -0.09 think -0.076 those of -0.064 you out -0.041 I tried to like this -0.033 Babylon -0.01 (
inputs
0.567 / 4
I love sci
0.385 / 3
-fi and
0.169 / 4
am willing to put
0.219 / 5
up with a lot.
-0.135 / 8
Sci-fi movies/TV are usually
-0.2 / 4
underfunded,
-0.412 / 2
under-
-0.347
appreciated
-0.383 / 3
and misunderstood.
-0.041 / 5
I tried to like this
0.008
,
0.163 / 4
I really did,
0.76
but
1.448 / 4
it is to good
0.913 / 2
TV sci
0.713 / 2
-fi
1.206 / 4
as Babylon 5 is
0.661 / 3
to Star Trek
0.421 / 5
(the original).
-0.216 / 6
Silly prosthetics,
-0.609 / 4
cheap cardboard sets,
-0.467 / 4
stilted dialogues,
-0.279 / 3
CG that
-0.512 / 3
doesn't
-0.494 / 3
match the background
-0.257
,
-0.544
and
-0.518 / 2
painfully one
-0.491 / 2
-dimensional
-0.661 / 5
characters cannot be overcome with
-0.188 / 8
a 'sci-fi' setting.
-0.01
(
0.229 / 2
I'
0.246 / 2
m sure
-0.121 / 2
there are
-0.076 / 2
those of
-0.064 / 2
you out
-0.285
there
-0.343
who
-0.09
think
-0.033
Babylon
0.034
5
0.078
is
1.422
good
0.454
sci
0.428 / 2
-fi
0.002


[1]
1-2-5-84710-0.155629-0.155629base value3.980023.98002fPOSITIVE(inputs)1.277 interesting by 1.247 Worth 0.964 entertaining 0.615 entertainment 0.52 the inclusion 0.517 competently handled 0.456 the 0.455 is 0.385 and 0.342 The plot is made 0.331 this 0.328 Damme kick style, 0.326 fights with the great Van 0.314 This one features the usual car chases, 0.314 especially if 0.314 0.291 action movies 0.278 . 0.276 you like 0.186 All of 0.169 value of a rental 0.146 you've seen 0.115 your share before. 0.096 but 0.092 and 0.061 <br /><br /> 0.056 Many of the c 0.021 which is clever -1.739 but -1.313 nothing -1.057 hardly profound -0.556 . -0.546 that -0.327 really blows -0.318 there is -0.228 you away if -0.105 of a rabbit -0.078 40 shell load shotgun, -0.073 even terrorist style bombs. -0.01 shooting battles with the -0.003 , -0.001 ,
inputs
0.314
1.247
Worth
0.456
the
0.615
entertainment
0.169 / 4
value of a rental
-0.001
,
0.314 / 2
especially if
0.276 / 2
you like
0.291 / 2
action movies
0.278
.
0.314 / 8
This one features the usual car chases,
0.326 / 5
fights with the great Van
0.328 / 5
Damme kick style,
-0.01 / 4
shooting battles with the
-0.078 / 5
40 shell load shotgun,
0.092
and
-0.073 / 5
even terrorist style bombs.
0.186 / 2
All of
0.331
this
0.455
is
0.964
entertaining
0.385
and
0.517 / 3
competently handled
-1.739
but
-0.318 / 2
there is
-1.313
nothing
-0.546
that
-0.327 / 2
really blows
-0.228 / 3
you away if
0.146 / 4
you've seen
0.115 / 4
your share before.
0.061 / 8
<br /><br />
0.342 / 4
The plot is made
1.277 / 2
interesting by
0.52 / 2
the inclusion
-0.105 / 3
of a rabbit
-0.003
,
0.021 / 3
which is clever
0.096
but
-1.057 / 2
hardly profound
-0.556
.
0.056 / 5
Many of the c

Build a Text masker explicitly

[12]:
masker = shap.maskers.Text(classifier.tokenizer)
explainer2 = shap.Explainer(pmodel, masker)
shap_values2 = explainer2(short_data[:2])
shap.plots.text(shap_values2[:, :, 1])
PartitionExplainer explainer: 3it [00:18, 18.80s/it]


[0]
1-2-5-84710-0.252905-0.252905base value2.500562.50056fPOSITIVE(inputs)1.448 it is to good 1.422 good 1.206 as Babylon 5 is 0.913 TV sci 0.76 but 0.713 -fi 0.661 to Star Trek 0.567 I love sci 0.454 sci 0.428 -fi 0.421 (the original). 0.385 -fi and 0.246 m sure 0.229 I' 0.219 up with a lot. 0.169 am willing to put 0.163 I really did, 0.078 is 0.034 5 0.008 , 0.002 -0.661 characters cannot be overcome with -0.609 cheap cardboard sets, -0.544 and -0.518 painfully one -0.512 doesn't -0.494 match the background -0.491 -dimensional -0.467 stilted dialogues, -0.412 under- -0.383 and misunderstood. -0.347 appreciated -0.343 who -0.285 there -0.279 CG that -0.257 , -0.216 Silly prosthetics, -0.2 underfunded, -0.188 a 'sci-fi' setting. -0.135 Sci-fi movies/TV are usually -0.121 there are -0.09 think -0.076 those of -0.064 you out -0.041 I tried to like this -0.033 Babylon -0.01 (
inputs
0.567 / 4
I love sci
0.385 / 3
-fi and
0.169 / 4
am willing to put
0.219 / 5
up with a lot.
-0.135 / 8
Sci-fi movies/TV are usually
-0.2 / 4
underfunded,
-0.412 / 2
under-
-0.347
appreciated
-0.383 / 3
and misunderstood.
-0.041 / 5
I tried to like this
0.008
,
0.163 / 4
I really did,
0.76
but
1.448 / 4
it is to good
0.913 / 2
TV sci
0.713 / 2
-fi
1.206 / 4
as Babylon 5 is
0.661 / 3
to Star Trek
0.421 / 5
(the original).
-0.216 / 6
Silly prosthetics,
-0.609 / 4
cheap cardboard sets,
-0.467 / 4
stilted dialogues,
-0.279 / 3
CG that
-0.512 / 3
doesn't
-0.494 / 3
match the background
-0.257
,
-0.544
and
-0.518 / 2
painfully one
-0.491 / 2
-dimensional
-0.661 / 5
characters cannot be overcome with
-0.188 / 8
a 'sci-fi' setting.
-0.01
(
0.229 / 2
I'
0.246 / 2
m sure
-0.121 / 2
there are
-0.076 / 2
those of
-0.064 / 2
you out
-0.285
there
-0.343
who
-0.09
think
-0.033
Babylon
0.034
5
0.078
is
1.422
good
0.454
sci
0.428 / 2
-fi
0.002


[1]
1-2-5-84710-0.155629-0.155629base value3.980023.98002fPOSITIVE(inputs)1.277 interesting by 1.247 Worth 0.964 entertaining 0.615 entertainment 0.52 the inclusion 0.517 competently handled 0.456 the 0.455 is 0.385 and 0.342 The plot is made 0.331 this 0.328 Damme kick style, 0.326 fights with the great Van 0.314 This one features the usual car chases, 0.314 especially if 0.314 0.291 action movies 0.278 . 0.276 you like 0.186 All of 0.169 value of a rental 0.146 you've seen 0.115 your share before. 0.096 but 0.092 and 0.061 <br /><br /> 0.056 Many of the c 0.021 which is clever -1.739 but -1.313 nothing -1.057 hardly profound -0.556 . -0.546 that -0.327 really blows -0.318 there is -0.228 you away if -0.105 of a rabbit -0.078 40 shell load shotgun, -0.073 even terrorist style bombs. -0.01 shooting battles with the -0.003 , -0.001 ,
inputs
0.314
1.247
Worth
0.456
the
0.615
entertainment
0.169 / 4
value of a rental
-0.001
,
0.314 / 2
especially if
0.276 / 2
you like
0.291 / 2
action movies
0.278
.
0.314 / 8
This one features the usual car chases,
0.326 / 5
fights with the great Van
0.328 / 5
Damme kick style,
-0.01 / 4
shooting battles with the
-0.078 / 5
40 shell load shotgun,
0.092
and
-0.073 / 5
even terrorist style bombs.
0.186 / 2
All of
0.331
this
0.455
is
0.964
entertaining
0.385
and
0.517 / 3
competently handled
-1.739
but
-0.318 / 2
there is
-1.313
nothing
-0.546
that
-0.327 / 2
really blows
-0.228 / 3
you away if
0.146 / 4
you've seen
0.115 / 4
your share before.
0.061 / 8
<br /><br />
0.342 / 4
The plot is made
1.277 / 2
interesting by
0.52 / 2
the inclusion
-0.105 / 3
of a rabbit
-0.003
,
0.021 / 3
which is clever
0.096
but
-1.057 / 2
hardly profound
-0.556
.
0.056 / 5
Many of the c

Explore how the Text masker works

[13]:
masker.shape("I like this movie.")
[13]:
(1, 7)
[14]:
model_args = masker(np.array([True, True, True, True, True, True, True]), "I like this movie.")
model_args
[14]:
(array(['I like this movie.'], dtype='<U18'),)
[15]:
pmodel(*model_args)
[15]:
array([[-5.16124821,  5.16124821]])
[16]:
model_args = masker(np.array([True, True, False, False, True, True, True]), "I like this movie.")
model_args
[16]:
(array(['I [MASK] [MASK]movie.'], dtype='<U21'),)
[17]:
pmodel(*model_args)
[17]:
array([[-0.60019428,  0.60019402]])
[18]:
masker2 = shap.maskers.Text(classifier.tokenizer, mask_token="...", collapse_mask_token=True)
[19]:
model_args2 = masker2(np.array([True, True, False, False, True, True, True]), "I like this movie.")
model_args2
[19]:
(array(['I ...movie.'], dtype='<U11'),)
[20]:
pmodel(*model_args2)
[20]:
array([[-4.21697383,  4.21697376]])

Plot summary statistics and bar charts

[21]:
# explain the predictions of the pipeline on the first two samples
shap_values = explainer(short_data[:20])
PartitionExplainer explainer: 21it [02:32,  8.02s/it]
[22]:
shap.plots.bar(shap_values[:, :, "POSITIVE"])
../../../_images/example_notebooks_text_examples_sentiment_analysis_Positive_vs._Negative_Sentiment_Classification_31_0.png
[23]:
shap.plots.bar(shap_values[:, :, "POSITIVE"].mean(0))
../../../_images/example_notebooks_text_examples_sentiment_analysis_Positive_vs._Negative_Sentiment_Classification_32_0.png
[24]:
shap.plots.bar(shap_values[:, :, "POSITIVE"].mean(0), order=shap.Explanation.argsort)
../../../_images/example_notebooks_text_examples_sentiment_analysis_Positive_vs._Negative_Sentiment_Classification_33_0.png
[ ]: