Naive Bayes and Sentiment Classification
Naive Bayes and Sentiment Classification
Naive Bayes and Sentiment Classification
All
rights reserved. Draft of December 30, 2020.
CHAPTER
Finally, one of the oldest tasks in text classification is assigning a library sub-
ject category or topic label to a text. Deciding whether a research paper concerns
epidemiology or instead, perhaps, embryology, is an important component of infor-
mation retrieval. Various sets of subject categories exist, such as the MeSH (Medical
Subject Headings) thesaurus. In fact, as we will see, subject category classification
is the task for which the naive Bayes algorithm was invented in 1961.
Classification is essential for tasks below the level of the document as well.
We’ve already seen period disambiguation (deciding if a period is the end of a sen-
tence or part of a word), and word tokenization (deciding if a character should be
a word boundary). Even language modeling can be viewed as classification: each
word can be thought of as a class, and so predicting the next word is classifying the
context-so-far into a class for each next word. A part-of-speech tagger (Chapter 8)
classifies each occurrence of a word in a sentence as, e.g., a noun or a verb.
The goal of classification is to take a single observation, extract some useful
features, and thereby classify the observation into one of a set of discrete classes.
One method for classifying text is to use handwritten rules. There are many areas of
language processing where handwritten rule-based classifiers constitute a state-of-
the-art system, or at least part of it.
Rules can be fragile, however, as situations or data change over time, and for
some tasks humans aren’t necessarily good at coming up with the rules. Most cases
supervised
of classification in language processing are instead done via supervised machine
machine learning, and this will be the subject of the remainder of this chapter. In supervised
learning
learning, we have a data set of input observations, each associated with some correct
output (a ‘supervision signal’). The goal of the algorithm is to learn how to map
from a new observation to a correct output.
Formally, the task of supervised classification is to take an input x and a fixed
set of output classes Y = y1 , y2 , ..., yM and return a predicted class y ∈ Y . For text
classification, we’ll sometimes talk about c (for “class”) instead of y as our output
variable, and d (for “document”) instead of x as our input variable. In the supervised
situation we have a training set of N documents that have each been hand-labeled
with a class: (d1 , c1 ), ...., (dN , cN ). Our goal is to learn a classifier that is capable of
mapping from a new document d to its correct class c ∈ C. A probabilistic classifier
additionally will tell us the probability of the observation being in the class. This
full distribution over the classes can be useful information for downstream decisions;
avoiding making discrete decisions early on can be useful when combining systems.
Many kinds of machine learning algorithms are used to build classifiers. This
chapter introduces naive Bayes; the following one introduces logistic regression.
These exemplify two ways of doing classification. Generative classifiers like naive
Bayes build a model of how a class could generate some input data. Given an ob-
servation, they return the class most likely to have generated the observation. Dis-
criminative classifiers like logistic regression instead learn what features from the
input are most useful to discriminate between the different possible classes. While
discriminative systems are often more accurate and hence more commonly used,
generative classifiers still have a role.
it 6
I 5
I love this movie! It's sweet, the 4
but with satirical humor. The fairy always love it to 3
it whimsical it to and 3
dialogue is great and the I
and seen are seen 2
adventure scenes are fun... friend anyone
It manages to be whimsical happy dialogue yet 1
and romantic while laughing adventure recommend would 1
satirical whimsical 1
at the conventions of the who sweet of movie it
fairy tale genre. I would it I but to romantic I times 1
recommend it to just about several yet sweet 1
anyone. I've seen it several again it the humor satirical 1
the seen would
times, and I'm always happy adventure 1
to scenes I the manages
to see it again whenever I the genre 1
fun I times and fairy 1
have a friend who hasn't and
about while humor 1
seen it yet! whenever have
conventions have 1
with great 1
… …
Figure 4.1 Intuition of the multinomial naive Bayes classifier applied to a movie review. The position of the
words is ignored (the bag of words assumption) and we make use of the frequency of each word.
Bayesian This idea of Bayesian inference has been known since the work of Bayes (1763),
inference
and was first applied to text classification by Mosteller and Wallace (1964). The
intuition of Bayesian classification is to use Bayes’ rule to transform Eq. 4.1 into
other probabilities that have some useful properties. Bayes’ rule is presented in
Eq. 4.2; it gives us a way to break down any conditional probability P(x|y) into
three other probabilities:
P(y|x)P(x)
P(x|y) = (4.2)
P(y)
We can then substitute Eq. 4.2 into Eq. 4.1 to get Eq. 4.3:
P(d|c)P(c)
ĉ = argmax P(c|d) = argmax (4.3)
c∈C c∈C P(d)
4 C HAPTER 4 • NAIVE BAYES AND S ENTIMENT C LASSIFICATION
We can conveniently simplify Eq. 4.3 by dropping the denominator P(d). This
is possible because we will be computing P(d|c)P(c)
P(d) for each possible class. But P(d)
doesn’t change for each class; we are always asking about the most likely class for
the same document d, which must have the same probability P(d). Thus, we can
choose the class that maximizes this simpler formula:
likelihood prior
z }| { z}|{
ĉ = argmax P( f1 , f2 , ...., fn |c) P(c) (4.6)
c∈C
Unfortunately, Eq. 4.6 is still too hard to compute directly: without some sim-
plifying assumptions, estimating the probability of every possible combination of
features (for example, every possible set of words and positions) would require huge
numbers of parameters and impossibly large training sets. Naive Bayes classifiers
therefore make two simplifying assumptions.
The first is the bag of words assumption discussed intuitively above: we assume
position doesn’t matter, and that the word “love” has the same effect on classification
whether it occurs as the 1st, 20th, or last word in the document. Thus we assume
that the features f1 , f2 , ..., fn only encode word identity and not position.
naive Bayes
assumption The second is commonly called the naive Bayes assumption: this is the condi-
tional independence assumption that the probabilities P( fi |c) are independent given
the class c and hence can be ‘naively’ multiplied as follows:
P( f1 , f2 , ...., fn |c) = P( f1 |c) · P( f2 |c) · ... · P( fn |c) (4.7)
The final equation for the class chosen by a naive Bayes classifier is thus:
Y
cNB = argmax P(c) P( f |c) (4.8)
c∈C f ∈F
To apply the naive Bayes classifier to text, we need to consider word positions, by
simply walking an index through every word position in the document:
positions ← all word positions in test document
Y
cNB = argmax P(c) P(wi |c) (4.9)
c∈C i∈positions
4.2 • T RAINING THE NAIVE BAYES C LASSIFIER 5
Naive Bayes calculations, like calculations for language modeling, are done in log
space, to avoid underflow and increase speed. Thus Eq. 4.9 is generally instead
expressed as
X
cNB = argmax log P(c) + log P(wi |c) (4.10)
c∈C i∈positions
By considering features in log space, Eq. 4.10 computes the predicted class as a lin-
ear function of input features. Classifiers that use a linear combination of the inputs
to make a classification decision —like naive Bayes and also logistic regression—
linear are called linear classifiers.
classifiers
Nc
P̂(c) = (4.11)
Ndoc
To learn the probability P( fi |c), we’ll assume a feature is just the existence of a word
in the document’s bag of words, and so we’ll want P(wi |c), which we compute as
the fraction of times the word wi appears among all words in all documents of topic
c. We first concatenate all documents with category c into one big “category c” text.
Then we use the frequency of wi in this concatenated document to give a maximum
likelihood estimate of the probability:
count(wi , c)
P̂(wi |c) = P (4.12)
w∈V count(w, c)
Here the vocabulary V consists of the union of all the word types in all classes, not
just the words in one class c.
There is a problem, however, with maximum likelihood training. Imagine we
are trying to estimate the likelihood of the word “fantastic” given class positive, but
suppose there are no training documents that both contain the word “fantastic” and
are classified as positive. Perhaps the word “fantastic” happens to occur (sarcasti-
cally?) in the class negative. In such a case the probability for this feature will be
zero:
count(“fantastic”, positive)
P̂(“fantastic”|positive) = P =0 (4.13)
w∈V count(w, positive)
But since naive Bayes naively multiplies all the feature likelihoods together, zero
probabilities in the likelihood term for any class will cause the probability of the
class to be zero, no matter the other evidence!
The simplest solution is the add-one (Laplace) smoothing introduced in Chap-
ter 3. While Laplace smoothing is usually replaced by more sophisticated smoothing
6 C HAPTER 4 • NAIVE BAYES AND S ENTIMENT C LASSIFICATION
count(wi , c) + 1 count(wi , c) + 1
P̂(wi |c) = P = P (4.14)
w∈V (count(w, c) + 1) w∈V count(w, c) + |V |
Note once again that it is crucial that the vocabulary V consists of the union of all the
word types in all classes, not just the words in one class c (try to convince yourself
why this must be true; see the exercise at the end of the chapter).
What do we do about words that occur in our test data but are not in our vocab-
ulary at all because they did not occur in any training document in any class? The
unknown word solution for such unknown words is to ignore them—remove them from the test
document and not include any probability for them at all.
Finally, some systems choose to completely ignore another class of words: stop
stop words words, very frequent words like the and a. This can be done by sorting the vocabu-
lary by frequency in the training set, and defining the top 10–100 vocabulary entries
as stop words, or alternatively by using one of the many predefined stop word list
available online. Then every instance of these stop words are simply removed from
both training and test documents as if they had never occurred. In most text classi-
fication applications, however, using a stop word list doesn’t improve performance,
and so it is more common to make use of the entire vocabulary and not use a stop
word list.
Fig. 4.2 shows the final algorithm.
function T RAIN NAIVE BAYES(D, C) returns log P(c) and log P(w|c)
Figure 4.2 The naive Bayes algorithm, using add-1 smoothing. To use add-α smoothing
instead, change the +1 to +α for loglikelihood counts in training.
4.3 • W ORKED EXAMPLE 7
3 2
P(−) = P(+) =
5 5
The word with doesn’t occur in the training set, so we drop it completely (as
mentioned above, we don’t use unknown word models for naive Bayes). The like-
lihoods from the training set for the remaining three words “predictable”, “no”, and
“fun”, are as follows, from Eq. 4.14 (computing the probabilities for the remainder
of the words in the training set is left as an exercise for the reader):
1+1 0+1
P(“predictable”|−) = P(“predictable”|+) =
14 + 20 9 + 20
1+1 0+1
P(“no”|−) = P(“no”|+) =
14 + 20 9 + 20
0+1 1+1
P(“fun”|−) = P(“fun”|+) =
14 + 20 9 + 20
For the test sentence S = “predictable with no fun”, after removing the word ‘with’,
the chosen class, via Eq. 4.9, is therefore computed as follows:
3 2×2×1
P(−)P(S|−) = × = 6.1 × 10−5
5 343
2 1×1×2
P(+)P(S|+) = × = 3.2 × 10−5
5 293
The model thus predicts the class negative for the test sentence.
binary NB multinomial naive Bayes or binary NB. The variant uses the same Eq. 4.10 except
that for each document we remove all duplicate words before concatenating them
into the single big document. Fig. 4.3 shows an example in which a set of four
documents (shortened and text-normalized for this example) are remapped to binary,
with the modified counts shown in the table on the right. The example is worked
without add-1 smoothing to make the differences clearer. Note that the results counts
need not be 1; the word great has a count of 2 even for Binary NB, because it appears
in multiple documents.
NB Binary
Counts Counts
Four original documents: + − + −
− it was pathetic the worst part was the and 2 0 1 0
boxing scenes boxing 0 1 0 1
film 1 0 1 0
− no plot twists or great scenes great 3 1 2 1
+ and satire and great plot twists it 0 1 0 1
+ great scenes great film no 0 1 0 1
or 0 1 0 1
After per-document binarization: part 0 1 0 1
− it was pathetic the worst part boxing pathetic 0 1 0 1
plot 1 1 1 1
scenes satire 1 0 1 0
− no plot twists or great scenes scenes 1 2 1 2
+ and satire great plot twists the 0 2 0 1
+ great scenes film twists 1 1 1 1
was 0 2 0 1
worst 0 1 0 1
Figure 4.3 An example of binarization for the binary naive Bayes algorithm.
A second important addition commonly made when doing text classification for
sentiment is to deal with negation. Consider the difference between I really like this
movie (positive) and I didn’t like this movie (negative). The negation expressed by
didn’t completely alters the inferences we draw from the predicate like. Similarly,
negation can modify a negative word to produce a positive review (don’t dismiss this
film, doesn’t let us get bored).
A very simple baseline that is commonly used in sentiment analysis to deal with
negation is the following: during text normalization, prepend the prefix NOT to
every word after a token of logical negation (n’t, not, no, never) until the next punc-
tuation mark. Thus the phrase
didn’t like this movie , but I
becomes
didn’t NOT_like NOT_this NOT_movie , but I
Newly formed ‘words’ like NOT like, NOT recommend will thus occur more of-
ten in negative document and act as cues for negative sentiment, while words like
NOT bored, NOT dismiss will acquire positive associations. We will return in Chap-
ter 16 to the use of parsing to deal more accurately with the scope relationship be-
tween these negation words and the predicates they modify, but this simple baseline
works quite well in practice.
Finally, in some situations we might have insufficient labeled training data to
train accurate naive Bayes classifiers using all words in the training set to estimate
positive and negative sentiment. In such cases we can instead derive the positive
4.5 • NAIVE BAYES FOR OTHER TEXT CLASSIFICATION TASKS 9
sentiment and negative word features from sentiment lexicons, lists of words that are pre-
lexicons
annotated with positive or negative sentiment. Four popular lexicons are the General
General
Inquirer Inquirer (Stone et al., 1966), LIWC (Pennebaker et al., 2007), the opinion lexicon
LIWC of Hu and Liu (2004) and the MPQA Subjectivity Lexicon (Wilson et al., 2005).
For example the MPQA subjectivity lexicon has 6885 words, 2718 positive and
4912 negative, each marked for whether it is strongly or weakly biased. Some sam-
ples of positive and negative words from the MPQA lexicon include:
+ : admirable, beautiful, confident, dazzling, ecstatic, favor, glee, great
− : awful, bad, bias, catastrophe, cheat, deny, envious, foul, harsh, hate
A common way to use lexicons in a naive Bayes classifier is to add a feature
that is counted whenever a word from that lexicon occurs. Thus we might add a
feature called ‘this word occurs in the positive lexicon’, and treat all instances of
words in the lexicon as counts for that one feature, instead of counting each word
separately. Similarly, we might add as a second feature ‘this word occurs in the
negative lexicon’ of words in the negative lexicon. If we have lots of training data,
and if the test data matches the training data, using just two features won’t work as
well as using all the words. But when training data is sparse or not representative of
the test set, using dense lexicon features instead of sparse individual-word features
may generalize better.
We’ll return to this use of lexicons in Chapter 20, showing how these lexicons
can be learned automatically, and how they can be applied to many other tasks be-
yond sentiment classification.
of text is written in—the most effective naive Bayes features are not words at all,
but character n-grams, 2-grams (‘zw’) 3-grams (‘nya’, ‘ Vo’), or 4-grams (‘ie z’,
‘thei’), or, even simpler byte n-grams, where instead of using the multibyte Unicode
character representations called codepoints, we just pretend everything is a string of
raw bytes. Because spaces count as a byte, byte n-grams can model statistics about
the beginning or ending of words. A widely used naive Bayes system, langid.py
(Lui and Baldwin, 2012) begins with all possible n-grams of lengths 1-4, using fea-
ture selection to winnow down to the most informative 7000 final features.
Language ID systems are trained on multilingual text, such as Wikipedia (Wiki-
pedia text in 68 different languages were used in (Lui and Baldwin, 2011)), or
newswire. To make sure that this multilingual text correctly reflects different re-
gions, dialects, and socioeconomic classes, systems also add Twitter text in many
languages geotagged to many regions (important for getting world English dialects
from countries with large Anglophone populations like Nigeria or India), Bible and
Quran translations, slang websites like Urban Dictionary, corpora of African Amer-
ican Vernacular English (Blodgett et al., 2016), and so on (Jurgens et al., 2017).
Thus consider a naive Bayes model with the classes positive (+) and negative (-)
and the following model parameters:
w P(w|+) P(w|-)
I 0.1 0.2
love 0.1 0.001
this 0.01 0.01
fun 0.05 0.005
film 0.1 0.1
... ... ...
Each of the two columns above instantiates a language model that can assign a
probability to the sentence “I love this fun film”:
P(“I love this fun film”|+) = 0.1 × 0.1 × 0.01 × 0.05 × 0.1 = 0.0000005
P(“I love this fun film”|−) = 0.2 × 0.001 × 0.01 × 0.005 × 0.1 = .0000000010
4.7 • E VALUATION : P RECISION , R ECALL , F- MEASURE 11
Figure 4.4 A confusion matrix for visualizing how well a binary classification system per-
forms against gold standard labels.
To make this more explicit, imagine that we looked at a million tweets, and
let’s say that only 100 of them are discussing their love (or hatred) for our pie,
12 C HAPTER 4 • NAIVE BAYES AND S ENTIMENT C LASSIFICATION
while the other 999,900 are tweets about something completely unrelated. Imagine a
simple classifier that stupidly classified every tweet as “not about pie”. This classifier
would have 999,900 true negatives and only 100 false negatives for an accuracy of
999,900/1,000,000 or 99.99%! What an amazing accuracy level! Surely we should
be happy with this classifier? But of course this fabulous ‘no pie’ classifier would
be completely useless, since it wouldn’t find a single one of the customer comments
we are looking for. In other words, accuracy is not a good metric when the goal is
to discover something that is rare, or at least not completely balanced in frequency,
which is a very common situation in the world.
That’s why instead of accuracy we generally turn to two other metrics shown in
precision Fig. 4.4: precision and recall. Precision measures the percentage of the items that
the system detected (i.e., the system labeled as positive) that are in fact positive (i.e.,
are positive according to the human gold labels). Precision is defined as
true positives
Precision =
true positives + false positives
recall Recall measures the percentage of items actually present in the input that were
correctly identified by the system. Recall is defined as
true positives
Recall =
true positives + false negatives
Precision and recall will help solve the problem with the useless “nothing is
pie” classifier. This classifier, despite having a fabulous accuracy of 99.99%, has
a terrible recall of 0 (since there are no true positives, and 100 false negatives, the
recall is 0/100). You should convince yourself that the precision at finding relevant
tweets is equally problematic. Thus precision and recall, unlike accuracy, emphasize
true positives: finding the things that we are supposed to be looking for.
There are many ways to define a single metric that incorporates aspects of both
F-measure precision and recall. The simplest of these combinations is the F-measure (van
Rijsbergen, 1975) , defined as:
(β 2 + 1)PR
Fβ =
β 2P + R
The β parameter differentially weights the importance of recall and precision,
based perhaps on the needs of an application. Values of β > 1 favor recall, while
values of β < 1 favor precision. When β = 1, precision and recall are equally bal-
F1 anced; this is the most frequently used metric, and is called Fβ =1 or just F1 :
2PR
F1 = (4.16)
P+R
F-measure comes from a weighted harmonic mean of precision and recall. The
harmonic mean of a set of numbers is the reciprocal of the arithmetic mean of recip-
rocals:
n
HarmonicMean(a1 , a2 , a3 , a4 , ..., an ) = 1 1 1 1
(4.17)
a1 + a2 + a3 + ... + an
gold labels
urgent normal spam
8
urgent 8 10 1 precisionu=
8+10+1
system 60
output normal 5 60 50 precisionn=
5+60+50
200
spam 3 30 200 precisions=
3+30+200
recallu = recalln = recalls =
8 60 200
8+5+3 10+60+30 1+50+200
Figure 4.5 Confusion matrix for a three-class categorization task, showing for each pair of
classes (c1 , c2 ), how many documents from c1 were (in)correctly assigned to c2
But we’ll need to slightly modify our definitions of precision and recall. Con-
sider the sample confusion matrix for a hypothetical 3-way one-of email catego-
rization decision (urgent, normal, spam) shown in Fig. 4.5. The matrix shows, for
example, that the system mistakenly labeled one spam document as urgent, and we
have shown how to compute a distinct precision and recall value for each class. In
order to derive a single metric that tells us how well the system is doing, we can com-
macroaveraging bine these values in two ways. In macroaveraging, we compute the performance
microaveraging for each class, and then average over classes. In microaveraging, we collect the de-
cisions for all classes into a single confusion matrix, and then compute precision and
recall from that table. Fig. 4.6 shows the confusion matrix for each class separately,
and shows the computation of microaveraged and macroaveraged precision.
As the figure shows, a microaverage is dominated by the more frequent class (in
this case spam), since the counts are pooled. The macroaverage better reflects the
statistics of the smaller classes, and so is more appropriate when performance on all
the classes is equally important.
macroaverage = .42+.52+.86
= .60
precision 3
Figure 4.6 Separate confusion matrices for the 3 classes from the previous figure, showing the pooled confu-
sion matrix and the microaveraged and macroaveraged precision.
and in general decide what the best model is. Once we come up with what we think
is the best model, we run it on the (hitherto unseen) test set to report its performance.
While the use of a devset avoids overfitting the test set, having a fixed train-
ing set, devset, and test set creates another problem: in order to save lots of data
for training, the test set (or devset) might not be large enough to be representative.
Wouldn’t it be better if we could somehow use all our data for training and still use
cross-validation all our data for test? We can do this by cross-validation: we randomly choose a
training and test set division of our data, train our classifier, and then compute the
error rate on the test set. Then we repeat with a different randomly selected training
set and test set. We do this sampling process 10 times and average these 10 runs to
10-fold get an average error rate. This is called 10-fold cross-validation.
cross-validation
The only problem with cross-validation is that because all the data is used for
testing, we need the whole corpus to be blind; we can’t examine any of the data
to suggest possible features and in general see what’s going on, because we’d be
peeking at the test set, and such cheating would cause us to overestimate the perfor-
mance of our system. However, looking at the corpus to understand what’s going
on is important in designing NLP systems! What to do? For this reason, it is com-
mon to create a fixed training set and test set, then do 10-fold cross-validation inside
the training set, but compute error rate the normal way in the test set, as shown in
Fig. 4.7.
We would like to know if δ (x) > 0, meaning that our logistic regression classifier
effect size has a higher F1 than our naive Bayes classifier on X. δ (x) is called the effect size;
a bigger δ means that A seems to be way better than B; a small δ means A seems to
be only a little better.
Why don’t we just check if δ (x) is positive? Suppose we do, and we find that
the F1 score of A is higher than Bs by .04. Can we be certain that A is better? We
cannot! That’s because A might just be accidentally better than B on this particular x.
We need something more: we want to know if A’s superiority over B is likely to hold
again if we checked another test set x0 , or under some other set of circumstances.
In the paradigm of statistical hypothesis testing, we test this by formalizing two
hypotheses.
H0 : δ (x) ≤ 0
H1 : δ (x) > 0 (4.20)
null hypothesis The hypothesis H0 , called the null hypothesis, supposes that δ (x) is actually nega-
tive or zero, meaning that A is not better than B. We would like to know if we can
confidently rule out this hypothesis, and instead support H1 , that A is better.
We do this by creating a random variable X ranging over all test sets. Now we
ask how likely is it, if the null hypothesis H0 was correct, that among these test sets
we would encounter the value of δ (x) that we found. We formalize this likelihood
p-value as the p-value: the probability, assuming the null hypothesis H0 is true, of seeing
the δ (x) that we saw or one even greater
So in our example, this p-value is the probability that we would see δ (x) assuming
A is not better than B. If δ (x) is huge (let’s say A has a very respectable F1 of .9
and B has a terrible F1 of only .2 on x), we might be surprised, since that would be
extremely unlikely to occur if H0 were in fact true, and so the p-value would be low
(unlikely to have such a large δ if A is in fact not better than B). But if δ (x) is very
small, it might be less surprising to us even if H0 were true and A is not really better
than B, and so the p-value would be higher.
A very small p-value means that the difference we observed is very unlikely
under the null hypothesis, and we can reject the null hypothesis. What counts as very
16 C HAPTER 4 • NAIVE BAYES AND S ENTIMENT C LASSIFICATION
small? It is common to use values like .05 or .01 as the thresholds. A value of .01
means that if the p-value (the probability of observing the δ we saw assuming H0 is
true) is less than .01, we reject the null hypothesis and assume that A is indeed better
statistically
significant than B. We say that a result (e.g., “A is better than B”) is statistically significant if
the δ we saw has a probability that is below the threshold and we therefore reject
this null hypothesis.
How do we compute this probability we need for the p-value? In NLP we gen-
erally don’t use simple parametric tests like t-tests or ANOVAs that you might be
familiar with. Parametric tests make assumptions about the distributions of the test
statistic (such as normality) that don’t generally hold in our cases. So in NLP we
usually use non-parametric tests based on sampling: we artificially create many ver-
sions of the experimental setup. For example, if we had lots of different test sets x0
we could just measure all the δ (x0 ) for all the x0 . That gives us a distribution. Now
we set a threshold (like .01) and if we see in this distribution that 99% or more of
those deltas are smaller than the delta we observed, i.e. that p-value(x)—the proba-
bility of seeing a δ (x) as big as the one we saw, is less than .01, then we can reject
the null hypothesis and agree that δ (x) was a sufficiently surprising difference and
A is really a better algorithm than B.
There are two common non-parametric tests used in NLP: approximate ran-
approximate domization (Noreen, 1989). and the bootstrap test. We will describe bootstrap
randomization
below, showing the paired version of the test, which again is most common in NLP.
paired Paired tests are those in which we compare two sets of observations that are aligned:
each observation in one set can be paired with an observation in another. This hap-
pens naturally when we are comparing the performance of two systems on the same
test set; we can pair the performance of system A on an individual observation xi
with the performance of system B on the same xi .
1 2 3 4 5 6 7 8 9 10 A% B% δ ()
x AB AB AB AB AB AB AB AB AB AB
.70 .50 .20
x(1) AB AB AB AB AB AB AB AB AB AB .60 .60 .00
x(2) AB AB AB AB AB AB AB AB AB AB .60 .70 -.10
...
x(b)
Figure 4.8 The paired bootstrap test: Examples of b pseudo test sets x(i) being created
from an initial true test set x. Each pseudo test set is created by sampling n = 10 times with
replacement; thus an individual sample is a single cell, a document with its gold label and
the correct or incorrect performance of classifiers A and B. Of course real test sets don’t have
only 10 examples, and b needs to be large as well.
Now that we have the b test sets, providing a sampling distribution, we can do
statistics on how often A has an accidental advantage. There are various ways to
compute this advantage; here we follow the version laid out in Berg-Kirkpatrick
et al. (2012). Assuming H0 (A isn’t better than B), we would expect that δ (X), esti-
mated over many test sets, would be zero; a much higher value would be surprising,
since H0 specifically assumes A isn’t better than B. To measure exactly how surpris-
ing is our observed δ (x) we would in other circumstances compute the p-value by
counting over many test sets how often δ (x(i) ) exceeds the expected zero value by
δ (x) or more:
b
1 δ (x(i) ) − δ (x) ≥ 0
X
p-value(x) =
i=1
However, although it’s generally true that the expected value of δ (X) over many test
sets, (again assuming A isn’t better than B) is 0, this isn’t true for the bootstrapped
test sets we created. That’s because we didn’t draw these samples from a distribution
with 0 mean; we happened to create them from the original test set x, which happens
to be biased (by .20) in favor of A. So to measure how surprising is our observed
δ (x), we actually compute the p-value by counting over many test sets how often
δ (x(i) ) exceeds the expected value of δ (x) by δ (x) or more:
b
1 δ (x(i) ) − δ (x) ≥ δ (x)
X
p-value(x) =
i=1
b
1 δ (x(i) ) ≥ 2δ (x)
X
= (4.22)
i=1
So if for example we have 10,000 test sets x(i) and a threshold of .01, and in only
47 of the test sets do we find that δ (x(i) ) ≥ 2δ (x), the resulting p-value of .0047 is
smaller than .01, indicating δ (x) is indeed sufficiently surprising, and we can reject
the null hypothesis and conclude A is better than B.
The full algorithm for the bootstrap is shown in Fig. 4.9. It is given a test set x, a
number of samples b, and counts the percentage of the b bootstrap test sets in which
δ (x∗(i) ) > 2δ (x). This percentage then acts as a one-sided empirical p-value
18 C HAPTER 4 • NAIVE BAYES AND S ENTIMENT C LASSIFICATION
Figure 4.9 A version of the paired bootstrap algorithm after Berg-Kirkpatrick et al. (2012).
tant, when introducing any NLP model, to study these these kinds of factors and
model card make them clear. One way to do this is by releasing a model card (Mitchell et al.,
2019) for each version of a model, that documents a machine learning model with
information like:
• training algorithms and parameters
• training data sources, motivation, and preprocessing
• evaluation data sources, motivation, and preprocessing
• intended use and users
• model performance across different demographic or other groups and envi-
ronmental situations
4.11 Summary
This chapter introduced the naive Bayes model for classification and applied it to
the text categorization task of sentiment analysis.
• Many language processing tasks can be viewed as tasks of classification.
• Text categorization, in which an entire text is assigned a class from a finite set,
includes such tasks as sentiment analysis, spam detection, language identi-
fication, and authorship attribution.
• Sentiment analysis classifies a text as reflecting the positive or negative orien-
tation (sentiment) that a writer expresses toward some object.
• Naive Bayes is a generative model that makes the bag of words assumption
(position doesn’t matter) and the conditional independence assumption (words
are conditionally independent of each other given the class)
• Naive Bayes with binarized features seems to work better for many text clas-
sification tasks.
• Classifiers are evaluated based on precision and recall.
• Classifiers are trained using distinct training, dev, and test sets, including the
use of cross-validation in the training set.
• Statistical significance tests should be used to determine whether we can be
confident that one version of a classifier is better than another.
• Designers of classifiers should carefully consider harms that may be caused
by the model, including its training data and other components, and report
model characteristics in a model card.
Exercises
4.1 Assume the following likelihoods for each word being part of a positive or
negative movie review, and equal prior probabilities for each class.
E XERCISES 21
pos neg
I 0.09 0.16
always 0.07 0.06
like 0.29 0.06
foreign 0.04 0.15
films 0.08 0.11
What class will Naive bayes assign to the sentence “I always like foreign
films.”?
4.2 Given the following short movie reviews, each labeled with a genre, either
comedy or action:
1. fun, couple, love, love comedy
2. fast, furious, shoot action
3. couple, fly, fast, fun, fun comedy
4. furious, shoot, shoot, fun action
5. fly, fast, shoot, love action
and a new document D:
fast, couple, shoot, fly
compute the most likely class for D. Assume a naive Bayes classifier and use
add-1 smoothing for the likelihoods.
4.3 Train two models, multinomial naive Bayes and binarized naive Bayes, both
with add-1 smoothing, on the following document counts for key sentiment
words, with positive or negative class assigned as noted.
doc “good” “poor” “great” (class)
d1. 3 0 3 pos
d2. 0 1 2 pos
d3. 1 3 0 neg
d4. 1 5 2 neg
d5. 0 2 0 neg
Use both naive Bayes models to assign a class (pos or neg) to this sentence:
A good, good plot and great characters, but poor acting.
Recall from page 6 that with naive Bayes text classification, we simply ignore
(throw out) any word that never occurred in the training document. (We don’t
throw out words that appear in some classes but not others; that’s what add-
one smoothing is for.) Do the two models agree or disagree?
22 Chapter 4 • Naive Bayes and Sentiment Classification
Aggarwal, C. C. and Zhai, C. (2012). A survey of text clas- Hu, M. and Liu, B. (2004). Mining and summarizing cus-
sification algorithms. Aggarwal, C. C. and Zhai, C. (Eds.), tomer reviews. KDD.
Mining text data, 163–222. Springer. Hutchinson, B., Prabhakaran, V., Denton, E., Webster, K.,
Bayes, T. (1763). An Essay Toward Solving a Problem in the Zhong, Y., and Denuyl, S. (2020). Social biases in NLP
Doctrine of Chances, Vol. 53. Reprinted in Facsimiles of models as barriers for persons with disabilities. ACL.
Two Papers by Bayes, Hafner Publishing, 1963. Jaech, A., Mulcaire, G., Hathi, S., Ostendorf, M., and Smith,
Berg-Kirkpatrick, T., Burkett, D., and Klein, D. (2012). An N. A. (2016). Hierarchical character-word models for lan-
empirical investigation of statistical significance in NLP. guage identification. ACL Workshop on NLP for Social Me-
EMNLP. dia.
Bisani, M. and Ney, H. (2004). Bootstrap estimates for con- Jauhiainen, T., Lui, M., Zampieri, M., Baldwin, T., and
fidence intervals in ASR performance evaluation. ICASSP. Lindén, K. (2018). Automatic language identification in
texts: A survey. arXiv preprint arXiv:1804.08186.
Bishop, C. M. (2006). Pattern recognition and machine
learning. Springer. Jurgens, D., Tsvetkov, Y., and Jurafsky, D. (2017). Incorpo-
rating dialectal variability for socially equitable language
Blodgett, S. L., Barocas, S., Daumé III, H., and Wallach, H. identification. ACL.
(2020). Language (technology) is power: A critical survey
of “bias” in NLP. ACL. Kiritchenko, S. and Mohammad, S. M. (2018). Examining
gender and race bias in two hundred sentiment analysis sys-
Blodgett, S. L., Green, L., and O’Connor, B. (2016). Demo- tems. *SEM.
graphic dialectal variation in social media: A case study of
African-American English. EMNLP. Liu, B. and Zhang, L. (2012). A survey of opinion min-
ing and sentiment analysis. Aggarwal, C. C. and Zhai, C.
Borges, J. L. (1964). The analytical language of John (Eds.), Mining text data, 415–464. Springer.
Wilkins. University of Texas Press. Trans. Ruth L. C.
Lui, M. and Baldwin, T. (2011). Cross-domain feature se-
Simms.
lection for language identification. IJCNLP.
Caliskan, A., Bryson, J. J., and Narayanan, A. (2017). Se-
Lui, M. and Baldwin, T. (2012). langid.py: An off-the-
mantics derived automatically from language corpora con-
shelf language identification tool. ACL.
tain human-like biases. Science 356(6334), 183–186.
Manning, C. D., Raghavan, P., and Schütze, H. (2008). In-
Chinchor, N., Hirschman, L., and Lewis, D. L. (1993). Eval- troduction to Information Retrieval. Cambridge.
uating Message Understanding systems: An analysis of the
third Message Understanding Conference. Computational Maron, M. E. (1961). Automatic indexing: an experimental
Linguistics 19(3), 409–449. inquiry. Journal of the ACM 8(3), 404–417.
McCallum, A. and Nigam, K. (1998). A comparison of event
Crawford, K. (2017). The trouble with bias. Keynote at
models for naive bayes text classification. AAAI/ICML-98
NeurIPS.
Workshop on Learning for Text Categorization.
Davidson, T., Bhattacharya, D., and Weber, I. (2019). Racial
Metsis, V., Androutsopoulos, I., and Paliouras, G. (2006).
bias in hate speech and abusive language detection datasets.
Spam filtering with naive bayes-which naive bayes?. CEAS.
Third Workshop on Abusive Language Online.
Minsky, M. (1961). Steps toward artificial intelligence. Pro-
Dixon, L., Li, J., Sorensen, J., Thain, N., and Vasserman, L.
ceedings of the IRE 49(1), 8–30.
(2018). Measuring and mitigating unintended bias in text
classification. 2018 AAAI/ACM Conference on AI, Ethics, Mitchell, M., Wu, S., Zaldivar, A., Barnes, P., Vasserman,
and Society. L., Hutchinson, B., Spitzer, E., Raji, I. D., and Gebru, T.
(2019). Model cards for model reporting. ACM FAccT.
Dror, R., Baumer, G., Bogomolov, M., and Reichart, R.
(2017). Replicability analysis for natural language process- Mosteller, F. and Wallace, D. L. (1963). Inference in an au-
ing: Testing significance with multiple datasets. TACL 5, thorship problem: A comparative study of discrimination
471––486. methods applied to the authorship of the disputed federal-
ist papers. Journal of the American Statistical Association
Dror, R., Peled-Cohen, L., Shlomov, S., and Reichart, R. 58(302), 275–309.
(2020). Statistical Significance Testing for Natural Lan-
Mosteller, F. and Wallace, D. L. (1964). Inference and Dis-
guage Processing, Vol. 45 of Synthesis Lectures on Human
puted Authorship: The Federalist. Springer-Verlag. 1984
Language Technologies. Morgan & Claypool.
2nd edition: Applied Bayesian and Classical Inference.
Efron, B. and Tibshirani, R. J. (1993). An introduction to the
Murphy, K. P. (2012). Machine learning: A probabilistic
bootstrap. CRC press.
perspective. MIT press.
Gillick, L. and Cox, S. J. (1989). Some statistical issues in Noreen, E. W. (1989). Computer Intensive Methods for Test-
the comparison of speech recognition algorithms. ICASSP. ing Hypothesis. Wiley.
Guyon, I. and Elisseeff, A. (2003). An introduction to vari- Pang, B. and Lee, L. (2008). Opinion mining and sentiment
able and feature selection. JMLR 3, 1157–1182. analysis. Foundations and trends in information retrieval
Hastie, T., Tibshirani, R. J., and Friedman, J. H. (2001). The 2(1-2), 1–135.
Elements of Statistical Learning. Springer. Pang, B., Lee, L., and Vaithyanathan, S. (2002). Thumbs
Heckerman, D., Horvitz, E., Sahami, M., and Dumais, S. T. up? Sentiment classification using machine learning tech-
(1998). A bayesian approach to filtering junk e-mail. AAAI- niques. EMNLP.
98 Workshop on Learning for Text Categorization. Park, J. H., Shin, J., and Fung, P. (2018). Reducing gender
bias in abusive language detection. EMNLP.
Exercises 23