text categorization moshe koppel lecture 2: naïve bayes slides based on manning, raghavan and...

29
Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Post on 22-Dec-2015

222 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Text CategorizationMoshe Koppel

Lecture 2: Naïve Bayes

Slides based on Manning, Raghavan and Schutze

Page 2: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Naïve Bayes: Why Bother?• Tightly tied to text categorization• Interesting theoretical properties.• A simple example of an important class of

learners based on generative models that approximate how data is produced

• For certain special cases, NB is the best thing you can do.

Page 3: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Bayes’ Rule

)()|()()|(),( CPCXPXPXCPXCP

)(

)()|()|(

XP

CPCXPXCP

Page 4: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Maximum a posteriori Hypothesis

)|(argmax DhPhHh

MAP

)(

)()|(argmax

DP

hPhDP

Hh

)()|(argmax hPhDPHh

As P(D) isconstant

Page 5: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Maximum likelihood Hypothesis

If all hypotheses are a priori equally likely, we only

need to consider the P(D|h) term:

)|(argmax hDPhHh

ML

Page 6: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Naive Bayes Classifiers

Task: Classify a new instance D based on a tuple of attribute

values into one of the classes cj CnxxxD ,,, 21

),,,|(argmax 21 njCc

MAP xxxcPcj

),,,(

)()|,,,(argmax

21

21

n

jjn

Cc xxxP

cPcxxxP

j

)()|,,,(argmax 21 jjnCc

cPcxxxPj

Page 7: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Naïve Bayes Classifier: Naïve Bayes Assumption

• P(cj)– Can be estimated from the frequency of classes in the

training examples.

• P(x1,x2,…,xn|cj) – O(|X|n•|C|) parameters– Could only be estimated if a very, very large number of

training examples was available.Naïve Bayes Conditional Independence Assumption:• Assume that the probability of observing the conjunction

of attributes is equal to the product of the individual probabilities P(xi|cj).

Page 8: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Smoothing to Avoid Overfitting

kcCN

cCxXNcxP

j

jiiji

)(

1),()|(ˆ

• Somewhat more subtle version

# of values of Xi

mcCN

mpcCxXNcxP

j

kijkiijki

)(

),()|(ˆ ,,

,

overall fraction in data where

Xi=xi,k

extent of“smoothing”

Page 9: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Naive Bayes for Text Categorization

• Attributes are text positions, values are words.

• Still too many possibilities• Assume that classification is independent of the

positions of the words– Use same parameters for each position– Result is bag of words model (over tokens not

types)

)|text""()|our""()(argmax

)|()(argmax

1j

j

jnjjCc

ijij

CcNB

cxPcxPcP

cxPcPc

Page 10: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

• Textj single document containing all docsj

• for each word xk in Vocabulary

– nk number of occurrences of xk in Textj

Naïve Bayes: Learning

• From training corpus, extract Vocabulary• Calculate required P(cj) and P(xk | cj) terms

– For each cj in C do• docsj subset of documents for which the target class is cj

||)|(

Vocabularyn

ncxP k

jk

|documents # total|

||)( j

j

docscP

Page 11: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Naïve Bayes: Classifying

• positions all word positions in current document which contain tokens found in Vocabulary

• Return cNB, where

positionsi

jijCc

NB cxPcPc )|()(argmaxj

Page 12: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Underflow Prevention

• Multiplying lots of probabilities, which are between 0 and 1 by definition, can result in floating-point underflow.

• Since log(xy) = log(x) + log(y), it is better to perform all computations by summing logs of probabilities rather than multiplying probabilities.

• Class with highest final un-normalized log probability score is still the most probable.

positionsi

jijCc

NB cxPcPc )|(log)(logargmaxj

Page 13: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Naïve Bayes as Stochastic Language Models

• Model probability of generating strings (each word in turn) in the language (commonly all strings over ∑). E.g., unigram model

0.2 the

0.1 a

0.01 man

0.01 woman

0.03 said

0.02 likes

the man likes the woman

0.2 0.01 0.02 0.2 0.01

multiply

Model M

P(s | M) = 0.00000008

Page 14: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Naïve Bayes as Stochastic Language Models

• Model probability of generating any string

0.2 the

0.01 class

0.0001 sayst

0.0001 pleaseth

0.0001 yon

0.0005 maiden

0.01 woman

Model M1 Model M2

maidenclass pleaseth yonthe

0.00050.01 0.0001 0.00010.2

0.010.0001 0.02 0.10.2

P(s|M2) > P(s|M1)

0.2 the

0.0001 class

0.03 sayst

0.02 pleaseth

0.1 yon

0.01 maiden

0.0001 woman

Page 15: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Unigram and higher-order models

• Unigram Language Models

• Bigram (generally, n-gram) Language Models

= P ( ) P ( | ) P ( | )P ( | )

P ( ) P ( ) P ( ) P ( )

P ( )

P ( ) P ( | ) P ( | ) P ( | )

Easy.Effective!

Page 16: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Smoothing and Backoff

• Suppose we’re using a trigram model.

We need to estimate P(w3 | w1,w2) • It will often be the case that the trigram w1,w2,w3 is

rare or non-existent in the training corpus. (Similar to problem we saw above with unigrams.)

• First resort: backoff.

Estimate P(w3 | w1,w2) using P(w3 | w2)• Alternatively, use some very large backup corpus.• Various combinations have been tried.

Page 17: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Multinomial Naïve Bayes = class conditional language model

• Think of wi as the ith word in the document

• Effectively, the probability of each class is done as a class-specific unigram language model

Cat

w1 w2 w3 w4 w5 w6

Page 18: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

But Wait! Another Approach

• Now think of wi as the ith word in the dictionary (not the document)

• Each value is either 1 (in the doc) or 0 (not)This is very different than the multinomial method. McCallum and

Nigam (1998) observed that the two were often confused.

Cat

w1 w2 w3 w4 w5 w6

Page 19: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Binomial Naïve Bayes

• One feature Xw for each word in dictionary

• Xw = true in document d if w appears in d

• Naive Bayes assumption: Given the document’s topic, appearance of one word

in the document tells us nothing about chances that another word appears

Page 20: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Parameter Estimation

fraction of documents of topic cj

in which word w appears

• Binomial model:

• Multinomial model:

– Can create a mega-document for topic j by concatenating all documents in this topic

– Use frequency of w in mega-document

)|(ˆjw ctXP

fraction of times in which word w appears

across all documents of topic cj

)|(ˆji cwXP

Page 21: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Experiment: Multinomial vs Binomial

• M&N (1998) did some experiments to see which is better

• Determine if a university web page is {student, faculty, other_stuff}

• Train on ~5,000 hand-labeled web pages– Cornell, Washington, U.Texas, Wisconsin

• Crawl and classify a new site (CMU)

Page 22: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Multinomial vs. Binomial

Page 23: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Conclusions

• Multinomial is better

• For Binomial, it’s really important to do feature filtering

• Other experiments bear out these conclusions

Page 24: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Feature Filtering

• If irrelevant words mess up the results, let’s try to use only words that might help

• In training set, choose k words which best discriminate the categories.

• Best way to choose: for each category build a list of j most discriminating terms

Page 25: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Infogain

• Use terms with maximal Mutual Information with the classes:

– For each word w and each category c

(This is equivalent to the usual two-class Infogain formula.)

}1,0{ }1,0{ )()(

),(log),(),(

w ce e cw

cwcw epep

eepeepcwI

Page 26: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Chi-Square Feature Selection

Term presentTerm absent

Document belongs to category

AB

Document does not belong to category

CD

X2 = N(AD-BC)2 / ( (A+B) (A+C) (B+D) (C+D) )

For complete independence of term and category: AD=BC

Page 27: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Feature Selection

• Many other measures of differentiation have been tried.

• Empirical tests suggest Infogain works best.• Simply eliminating rare terms is easy and

usual doesn’t do much harm.• Be sure not to use test data when you do

feature selection. (This is tricky when you’re using k-fold cross-validation.)

Page 28: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Naïve Bayes: Conclusions

• Classification results of naïve Bayes (the class with maximum posterior probability) are usually fairly accurate, though not nearly as good as, say, SVM.

• However, due to the inadequacy of the conditional independence assumption, the actual posterior-probability numerical estimates are not.– Output probabilities are generally very close to 0 or 1.

Page 29: Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze

Some Good Things about NB

• Theoretically optimal if the independence assumptions hold

• Fast

• Sort of robust to irrelevant features (but not really)

• Very good in domains with many equally important features

• Probably only method useful for very short test documents (Why?)