Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze."— Presentation transcript:

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

2 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.

3 Bayes’ Rule

4 Maximum a posteriori Hypothesis As P(D) is constant

5 Maximum likelihood Hypothesis If all hypotheses are a priori equally likely, we only need to consider the P(D|h) term:

6 Naive Bayes Classifiers Task: Classify a new instance D based on a tuple of attribute values into one of the classes c j  C

7 Naïve Bayes Classifier: Naïve Bayes Assumption P(c j ) –Can be estimated from the frequency of classes in the training examples. P(x 1,x 2,…,x n |c j ) –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(x i |c j ).

8 Smoothing to Avoid Overfitting Somewhat more subtle version # of values of X i overall fraction in data where X i =x i,k extent of “smoothing”

9 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)

10 Text j  single document containing all docs j for each word x k in Vocabulary –n k  number of occurrences of x k in Text j – Naïve Bayes: Learning From training corpus, extract Vocabulary Calculate required P(c j ) and P(x k | c j ) terms –For each c j in C do docs j  subset of documents for which the target class is c j

11 Naïve Bayes: Classifying positions  all word positions in current document which contain tokens found in Vocabulary Return c NB, where

12 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.

13 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.2the 0.1a 0.01man 0.01woman 0.03said 0.02likes … themanlikesthewoman 0.20.010.020.20.01 multiply Model M P(s | M) = 0.00000008

14 Naïve Bayes as Stochastic Language Models Model probability of generating any string 0.2the 0.01class 0.0001sayst 0.0001pleaseth 0.0001yon 0.0005maiden 0.01woman Model M1Model M2 maidenclasspleasethyonthe 0.00050.010.0001 0.2 0.010.00010.020.10.2 P(s|M2) > P(s|M1) 0.2the 0.0001class 0.03sayst 0.02pleaseth 0.1yon 0.01maiden 0.0001woman

15 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 ( | ) Easy. Effective!

16 Smoothing and Backoff Suppose we’re using a trigram model. We need to estimate P(w 3 | w 1,w 2 ) It will often be the case that the trigram w 1,w 2,w 3 is rare or non-existent in the training corpus. (Similar to problem we saw above with unigrams.) First resort: backoff. Estimate P(w 3 | w 1,w 2 ) using P(w 3 | w 2 ) Alternatively, use some very large backup corpus. Various combinations have been tried.

17 Multinomial Naïve Bayes = class conditional language model Think of w i as the i th word in the document Effectively, the probability of each class is done as a class-specific unigram language model Cat w1w1 w2w2 w3w3 w4w4 w5w5 w6w6

18 But Wait! Another Approach Now think of w i as the i th 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 w1w1 w2w2 w3w3 w4w4 w5w5 w6w6

19 Binomial Naïve Bayes One feature X w for each word in dictionary X w = 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

20 Parameter Estimation fraction of documents of topic c j 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 fraction of times in which word w appears across all documents of topic c j

21 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)

22 Multinomial vs. Binomial

23 Conclusions Multinomial is better For Binomial, it’s really important to do feature filtering Other experiments bear out these conclusions

24 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

25 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.)

26 Chi-Square Feature Selection Term presentTerm absent Document belongs to category AB Document does not belong to category CD X 2 = N(AD-BC) 2 / ( (A+B) (A+C) (B+D) (C+D) ) For complete independence of term and category: AD=BC

27 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.)

28 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.

29 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?)


Download ppt "Text Categorization Moshe Koppel Lecture 2: Naïve Bayes Slides based on Manning, Raghavan and Schutze."

Similar presentations


Ads by Google