Professional Documents
Culture Documents
Naivebayes 2021
Naivebayes 2021
Is this spam?
Dan Jurafsky
4
Dan Jurafsky
Text Classification
• Assigning subject categories, topics, or genres
• Spam detection
• Authorship identification
• Age/gender identification
• Language Identification
• Sentiment analysis
• …
Dan Jurafsky
Classification Methods:
Supervised Machine Learning
• Input:
• a document d
• a fixed set of classes C = {c1, c2,…, cJ}
• A training set of m hand-labeled documents (d1,c1),....,(dm,cm)
• Output:
• a learned classifier γ:d à c
9
Dan Jurafsky
Classification Methods:
Supervised Machine Learning
• Any kind of classifier
• Naïve Bayes
• Logistic regression
• Support-vector machines
• k-Nearest Neighbors
• …
Text The Naive Bayes Classifier
Classification
and Naive
Bayes
Naive Bayes Intuition
γ( )=c
sweet 1
whimsical 1
recommend 1
happy 1
... ...
Bayes’ Rule Applied to Documents and Classes
P(d | c)P(c)
P(c | d) =
P(d)
Naive Bayes Classifier (I)
MAP is “maximum a
cMAP = argmax P(c | d) posteriori” = most
c∈C likely class
P(d | c)P(c)
= argmax Bayes Rule
c∈C P(d)
= argmax P(d | c)P(c) Dropping the
denominator
c∈C
Naive Bayes Classifier (II)
"Likelihood" "Prior"
available.
Multinomial Naive Bayes Independence
Assumptions
P(x1, x2 ,…, xn | c)
2 3
X
This: cNB = argmax 4log P (cj ) + log P (xi |cj )5
cj 2C
i2positions
Notes:
1) Taking log doesn't change the ranking of classes!
The class with highest probability also has highest log probability!
2) It's a linear model:
Just a max of a sum of weights: a linear function of the inputs
So naive bayes is a linear classifier
Text The Naive Bayes Classifier
Classification
and Naive
Bayes
Text
Classification Naive Bayes: Learning
and Naïve
Bayes
Sec.13.3
𝑁"!
𝑃! 𝑐! =
𝑁#$#%&
count(wi , c j )
P̂(wi | c j ) =
∑ count(w, c j )
w∈V
Parameter estimation
P̂(wi | c j ) =
count(wi , c j ) fraction of times word wi appears
∑ count(w, c j ) among all words in documents of topic cj
w∈V
count("fantastic", positive)
P̂("fantastic" positive) = = 0
∑ count(w, positive)
w∈V
count(wi , c) +1
P̂(wi | c) =
∑ (count(w, c))+1)
w∈V
count(wi , c) +1
=
# &
%% ∑ count(w, c)(( + V
$ w∈V '
Multinomial Naïve Bayes: Learning
39
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
Binary multinominal naive Bayes
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
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
Binary multinominal naive Bayes
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
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
Binary multinominal naive Bayes
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
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
Binary multinominal naive Bayes
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
Counts can still be 2! Binarization is within-doc! worst 0 1 0 1
Text Sentiment and Binary
Classification Naive Bayes
and Naive
Bayes
Text More on Sentiment
Classification Classification
and Naive
Bayes
Sentiment Classification: Dealing with Negation
I really like this movie
I really don't like this movie
Riloff and Wiebe (2003). Learning extraction patterns for subjective expressions. EMNLP-2003.
49
The General Inquirer
Philip J. Stone, Dexter C Dunphy, Marshall S. Smith, Daniel M. Ogilvie. 1966. The General
Inquirer: A Computer Approach to Content Analysis. MIT Press
Naïve Bayes:
Relationship to
Language Modeling
Dan Jurafsky
c=China
57
Dan Jurafsky
Naïve Bayes:
Relationship to
Language Modeling
Text Classification
and Naïve Bayes
A combined measure: F
• A combined measure that assesses the P/R tradeoff is F measure
(weighted harmonic mean):
2
1 ( β + 1) PR
F= =
1 1 β 2
P + R
α + (1 − α )
P R
• The harmonic mean is a very conservative average; see IIR §
8.3
• People usually use balanced F1 measure
• i.e., with b = 1 (that is, a = ½): F = 2PR/(P+R)
Text Classification
and Naïve Bayes
Text Classification:
Evaluation
Dan Jurafsky Sec.14.5
More Than Two Classes:
Sets of binary classifiers
• Dealing with any-of or multivalue classification
• A document can belong to 0, 1, or >1 classes.
Confusion matrix c
• For each pair of classes <c1,c2> how many documents from c1
were incorrectly assigned to c2?
• c3,2: 90 wheat documents incorrectly assigned to poultry
Docs in test set Assigned Assigned Assigned Assigned Assigned Assigned
UK poultry wheat coffee interest trade
True UK 95 1 13 0 1 0
True poultry 0 1 0 0 0 0
True wheat 10 90 0 1 0 0
True coffee 0 0 0 34 3 7
True interest - 1 2 13 26 5
72 True trade 0 0 2 14 5 10
Dan Jurafsky Sec. 15.2.4
Precision: cii
Fraction of docs assigned class i that are ∑ c ji
actually about class i: j
∑ cii
i
Accuracy: (1 - error rate)
73 Fraction of docs classified correctly:
∑∑ cij
j i
Dan Jurafsky Sec. 15.2.4
74
Dan Jurafsky Sec. 15.2.4
Text Classification:
Evaluation