INFO 4300 / CS4300 Information Retrieval slides adapted from - - PowerPoint PPT Presentation

info 4300 cs4300 information retrieval slides adapted
SMART_READER_LITE
LIVE PREVIEW

INFO 4300 / CS4300 Information Retrieval slides adapted from - - PowerPoint PPT Presentation

INFO 4300 / CS4300 Information Retrieval slides adapted from Hinrich Sch utzes, linked from http://informationretrieval.org/ IR 24/25: Text Classification and Naive Bayes Paul Ginsparg Cornell University, Ithaca, NY 30 Nov 2010 1 / 46


slide-1
SLIDE 1

INFO 4300 / CS4300 Information Retrieval slides adapted from Hinrich Sch¨ utze’s, linked from http://informationretrieval.org/

IR 24/25: Text Classification and Naive Bayes

Paul Ginsparg

Cornell University, Ithaca, NY

30 Nov 2010

1 / 46

slide-2
SLIDE 2

Administrativa

Assignment 4 due Fri 3 Dec (extended to Sun 5 Dec). Mon 13 Dec, Early Final examination, 2:00-4:30 p.m., Upson B17 (by prior notification of intent via CMS) Fri 17 Dec, Final examination, 2:00-4:30 p.m., in Hollister Hall B14

2 / 46

slide-3
SLIDE 3

Overview

1

Recap

2

Text classification

3

Naive Bayes

4

Discussion

3 / 46

slide-4
SLIDE 4

Outline

1

Recap

2

Text classification

3

Naive Bayes

4

Discussion

4 / 46

slide-5
SLIDE 5

Major issue in clustering – labeling

After a clustering algorithm finds a set of clusters: how can they be useful to the end user? We need a pithy label for each cluster. For example, in search result clustering for “jaguar”, The labels of the three clusters could be “animal”, “car”, and “operating system”. How can we automatically find good labels for clusters?

discriminative vs. non-discriminative labelling titles?

5 / 46

slide-6
SLIDE 6

Feature selection

In text classification, we usually represent documents in a high-dimensional space, with each dimension corresponding to a term. In this lecture: axis = dimension = word = term = feature Many dimensions correspond to rare words. Rare words can mislead the classifier. Rare misleading features are called noise features. Eliminating noise features from the representation increases efficiency and effectiveness of text classification. Eliminating features is called feature selection.

6 / 46

slide-7
SLIDE 7

Different feature selection methods

A feature selection method is mainly defined by the feature utility measures it employs. Feature utility measures: Frequency – select the most frequent terms Mutual information – select the terms with the highest mutual information (mutual information is also called information gain in this context) χ2 (Chi-square)

7 / 46

slide-8
SLIDE 8

How to compute MI values

Based on maximum likelihood estimates, the formula we actually use is: I(U; C) = N11 N log2 NN11 N1.N.1 + N10 N log2 NN10 N1.N.0 (1) +N01 N log2 NN01 N0.N.1 + N00 N log2 NN00 N0.N.0

N11: # of documents that contain t (et = 1) and are in c (ec = 1) N10: # of documents that contain t (et = 1) and not in c (ec = 0) N01: # of documents that don’t contain t (et = 0) and in c (ec = 1) N00: # of documents that don’t contain t (et = 0) and not in c (ec = 0) N = N00 + N01 + N10 + N11 p(t, c) ≈ N11/N, p(t, c) ≈ N01/N, p(t, c) ≈ N10/N, p(t, c) ≈ N00/N

  • N1. = N10 + N11: # documents that contain t, p(t) ≈ N1./N

N.1 = N01 + N11: # documents in c, p(c) ≈ N.1/N

  • N0. = N00 + N01: # documents that don’t contain t, p(t) ≈ N0./N

N.0 = N00 + N10: # documents not in c, p(c) ≈ N.0/N

8 / 46

slide-9
SLIDE 9

MI example for poultry/export in Reuters

ec = epoultry = 1 ec = epoultry = 0 et = eexport = 1 N11 = 49 N10 = 141 et = eexport = 0 N01 = 27,652 N00 = 774,106 Plug these values into formula: I(U; C) = 49 801,948 log2 801,948 · 49 (49+27,652)(49+141) + 141 801,948 log2 801,948 · 141 (141+774,106)(49+141) + 27,652 801,948 log2 801,948 · 27,652 (49+27,652)(27,652+774,106) +774,106 801,948 log2 801,948 · 774,106 (141+774,106)(27,652+774,106) ≈ 0.000105

9 / 46

slide-10
SLIDE 10

MI feature selection on Reuters

Terms with highest mutual information for three classes: coffee coffee 0.0111 bags 0.0042 growers 0.0025 kg 0.0019 colombia 0.0018 brazil 0.0016 export 0.0014 exporters 0.0013 exports 0.0013 crop 0.0012 sports soccer 0.0681 cup 0.0515 match 0.0441 matches 0.0408 played 0.0388 league 0.0386 beat 0.0301 game 0.0299 games 0.0284 team 0.0264 poultry poultry 0.0013 meat 0.0008 chicken 0.0006 agriculture 0.0005 avian 0.0004 broiler 0.0003 veterinary 0.0003 birds 0.0003 inspection 0.0003 pathogenic 0.0003 I(export,poultry) ≈ .000105 not among the ten highest for class poultry, but still potentially significant.

10 / 46

slide-11
SLIDE 11

χ2 Feature selection

χ2 tests independence of two events, p(A, B) = p(A)p(B) (or p(A|B) = p(A), p(B|A) = p(B)) . Test occurrence of the term, occurrence of the class, rank w.r.t.: X 2(D, t, c) =

  • et∈{0,1}
  • ec∈{0,1}

(Netec − Eetec)2 Eetec where N = observed frequency in D, E = expected frequency (e.g., E11 is the expected frequency of t and c occurring together in a document, assuming term and class are independent) High value of X 2 indicates independence hypothesis is incorrect, i.e., observed and expected are too dissimilar. If occurrence of term and class are dependent events, then

  • ccurrence of term makes class more (or less) likely, hence helpful

as feature.

11 / 46

slide-12
SLIDE 12

χ2 Feature selection, example

Are class poultry and term export interdependent by χ2 test? ec = epoultry = 1 ec = epoultry = 0 et = eexport = 1 N11 = 49 N10 = 141 et = eexport = 0 N01 = 27,652 N00 = 774,106 N = N11 + N10 + N01 + N00 = 801948 Identify: p(t) = N11+N10

N

, p(c) = N11+N01

N

, p(t) = N01+N00

N

, p(c) = N10+N00

N

Then estimate expected frequencies: ec = epoultry = 1 ec = epoultry = 0 et = eexport = 1 E11 = Np(t)p(c) E10 = Np(t)p(c) et = eexport = 0 E01 = Np(t)p(c) E00 = Np(t)p(c) e.g., E11 = N · p(t) · p(c) = N · N11 + N10 N · N11 + N01 N = N · 49 + 141 N · 49 + 27652 N ≈ 6.6

12 / 46

slide-13
SLIDE 13

Expected Frequencies

From ec = epoultry = 1 ec = epoultry = 0 et = eexport = 1 E11 = Np(t)p(c) E10 = Np(t)p(c) et = eexport = 0 E01 = Np(t)p(c) E00 = Np(t)p(c) the full table of expected frequencies is ec = epoultry = 1 ec = epoultry = 0 et = eexport = 1 E11 ≈ 6.6 E10 ≈ 183.4 et = eexport = 0 E01 ≈ 27694.4 E00 ≈ 774063.6 Compared to the original data: ec = epoultry = 1 ec = epoultry = 0 et = eexport = 1 N11 = 49 N10 = 141 et = eexport = 0 N01 = 27,652 N00 = 774,106 the question is now whether a quantity like the surplus N11 = 49

  • ver the expected E11 ≈ 6.6 is statistically significant.

13 / 46

slide-14
SLIDE 14

For these values of N and E, the result for X 2 is X 2(D, t, c) =

  • et∈{0,1}
  • ec∈{0,1}

(Netec − Eetec)2 Eetec ≈ 284 We are testing the assumption that the values of the Netec are generated by two independent probabilities, fitting the three ratios with two parameters p(t) and p(c), leaving one degree of freedom. There is a tabulated distribution, called the χ2 distribution (in this case with one degree of freedom) which assesses the statistical likelihood

  • f any value of X 2, as defined above (and is

analogous to likelihood of standard deviations from the mean of a gaussian distribution): p χ2 critical .1 2.71 .05 3.84 .01 6.63 .005 7.88 .001 10.83 The above X 2 ≈ 284 > 10.83, i.e., giving a less than .1% chance that so large a value of X 2 would occur if export/poultry were really independent (equivalently a 99.9% chance they’re dependent).

14 / 46

slide-15
SLIDE 15

Outline

1

Recap

2

Text classification

3

Naive Bayes

4

Discussion

15 / 46

slide-16
SLIDE 16

Relevance feedback

In relevance feedback, the user marks documents as relevant/nonrelevant. Relevant/nonrelevant can be viewed as classes or categories. For each document, the user decides which of these two classes is correct. The IR system then uses these class assignments to build a better query (“model”) of the information need . . . . . . and returns better documents. Relevance feedback is a form of text classification. The notion of text classification (TC) is very general and has many applications within and beyond information retrieval.

16 / 46

slide-17
SLIDE 17

Another TC task: spam filtering

From: ‘‘’’ <takworlld@hotmail.com> Subject: real estate is the only way... gem

  • alvgkay

Anyone can buy real estate with no money down Stop paying rent TODAY ! There is no need to spend hundreds or even thousands for similar courses I am 22 years old and I have already purchased 6 properties using the methods outlined in this truly INCREDIBLE ebook. Change your life NOW ! ================================================= Click Below to order: http://www.wholesaledaily.com/sales/nmd.htm ================================================= How would you write a program that would automatically detect and delete this type of message?

17 / 46

slide-18
SLIDE 18

Formal definition of TC — summary

Training Given: A document space X

Documents are represented in some high-dimensional space.

A fixed set of classes C = {c1, c2, . . . , cJ}

human-defined for needs of application (e.g., rel vs. non-rel).

A training set D of labeled documents d, c ∈ X × C Using a learning method or learning algorithm, we then wish to learn a classifier γ that maps documents to classes: γ : X → C Application/Testing Given: a description d ∈ X of a document Determine: γ(d) ∈ C, i.e., the class most appropriate for d

18 / 46

slide-19
SLIDE 19

Topic classification

classes: training set: test set:

regions industries subject areas γ(d′) =China

first private Chinese airline

UK China poultry coffee elections sports

London congestion Big Ben Parliament the Queen Windsor Beijing Olympics Great Wall tourism communist Mao chicken feed ducks pate turkey bird flu beans roasting robusta arabica harvest Kenya votes recount run-off seat campaign TV ads baseball diamond soccer forward captain team

d′

19 / 46

slide-20
SLIDE 20

Examples of how search engines use classification

Standing queries (e.g., Google Alerts) Language identification (classes: English vs. French etc.) The automatic detection of spam pages (spam vs. nonspam) The automatic detection of sexually explicit content (sexually explicit vs. not) Sentiment detection: is a movie or product review positive or negative (positive vs. negative) Topic-specific or vertical search – restrict search to a “vertical” like “related to health” (relevant to vertical vs. not)

20 / 46

slide-21
SLIDE 21

Classification methods: 1. Manual

Manual classification was used by Yahoo in the beginning of the web. Also: ODP, PubMed Very accurate if job is done by experts Consistent when the problem size and team is small Scaling manual classification is difficult and expensive. → We need automatic methods for classification.

21 / 46

slide-22
SLIDE 22

Classification methods: 2. Rule-based

Our Google Alerts example was rule-based classification. There are “IDE” type development environments for writing very complex rules efficiently. (e.g., Verity integrated development environment) Often: Boolean combinations (as in Google Alerts) Accuracy is very high if a rule has been carefully refined over time by a subject expert. Building and maintaining rule-based classification systems is expensive.

22 / 46

slide-23
SLIDE 23

Classification methods: 3. Statistical/Probabilistic

As per our definition of the classification problem – text classification as a learning problem Supervised learning of a the classification function γ and its application to classifying new documents We have looked at a couple of methods for doing this: Rocchio, kNN. Now Naive Bayes No free lunch: requires hand-classified training data But this manual classification can be done by non-experts.

23 / 46

slide-24
SLIDE 24

Classification methods — summary

  • 1. Manual (accurate if done by experts, consistent for problem size

and team is small difficult and expensive to scale)

  • 2. Rule-based (accuracy very high if a rule has been carefully

refined over time by a subject expert, building and maintaining expensive)

  • 3. Statistical/Probabilistic

As per our definition of the classification problem – text classification as a learning problem Supervised learning of a the classification function γ and its application to classifying new documents We have looked at a couple of methods for doing this: Rocchio, kNN. Now Naive Bayes No free lunch: requires hand-classified training data But this manual classification can be done by non-experts.

24 / 46

slide-25
SLIDE 25

Outline

1

Recap

2

Text classification

3

Naive Bayes

4

Discussion

25 / 46

slide-26
SLIDE 26

The Naive Bayes classifier

The Naive Bayes classifier is a probabilistic classifier. We compute the probability of a document d being in a class c as follows: P(c|d) ∝ P(c)

  • 1≤k≤nd

P(tk|c) nd is the length of the document. (number of tokens) P(tk|c) is the conditional probability of term tk occurring in a document of class c P(tk|c) as a measure of how much evidence tk contributes that c is the correct class. P(c) is the prior probability of c. If a document’s terms do not provide clear evidence for one class vs. another, we choose the c with higher P(c).

26 / 46

slide-27
SLIDE 27

Maximum a posteriori class

Our goal is to find the “best” class. The best class in Naive Bayes classification is the most likely

  • r maximum a posteriori (MAP) class cmap:

cmap = arg max

c∈C

ˆ P(c|d) = arg max

c∈C

ˆ P(c)

  • 1≤k≤nd

ˆ P(tk|c) We write ˆ P for P since these values are estimates from the training set.

27 / 46

slide-28
SLIDE 28

Taking the log

Multiplying lots of small probabilities can result in floating point underflow. Since log(xy) = log(x) + log(y), we can sum log probabilities instead of multiplying probabilities. Since log is a monotonic function, the class with the highest score does not change. So what we usually compute in practice is: cmap = arg max

c∈C

  • log ˆ

P(c) +

  • 1≤k≤nd

log ˆ P(tk|c)

  • 28 / 46
slide-29
SLIDE 29

Naive Bayes classifier

Classification rule: cmap = arg max

c∈C

  • log ˆ

P(c) +

  • 1≤k≤nd

log ˆ P(tk|c)

  • Simple interpretation:

Each conditional parameter log ˆ P(tk|c) is a weight that indicates how good an indicator tk is for c. The prior log ˆ P(c) is a weight that indicates the relative frequency of c. The sum of log prior and term weights is then a measure of how much evidence there is for the document being in the class. We select the class with the most evidence.

29 / 46

slide-30
SLIDE 30

Parameter estimation

How to estimate parameters ˆ P(c) and ˆ P(tk|c) from training data? Prior: ˆ P(c) = Nc N Nc: number of docs in class c; N: total number of docs Conditional probabilities: ˆ P(t|c) = Tct

  • t′∈V Tct′

Tct is the number of tokens of t in training documents from class c (includes multiple occurrences) We’ve made a Naive Bayes independence assumption here: ˆ P(tk1|c) = ˆ P(tk2|c) (i.e., position independence of terms)

30 / 46

slide-31
SLIDE 31

The problem with maximum likelihood estimates: Zeros

C=China X1=Beijing X2=and X3=Taipei X4=join X5=WTO

P(China|d) ∝ P(China) · P(Beijing|China) · P(and|China) · P(Taipei|China) · P(join|China) · P(WTO|China) If WTO never occurs in class China: ˆ P(WTO|China) = TChina,WTO

  • t′∈V TChina,t′

= 0

31 / 46

slide-32
SLIDE 32

The problem with maximum likelihood estimates: Zeros (cont’d)

If there were no occurrences of WTO in documents in class China, we’d get a zero estimate: ˆ P(WTO|China) = TChina,WTO

  • t′∈V TChina,t′

= 0 → We will get P(China|d) = 0 for any document that contains WTO! Zero probabilities cannot be conditioned away.

32 / 46

slide-33
SLIDE 33

To avoid zeros: Add-one smoothing

Add one to each count to avoid zeros: ˆ P(t|c) = Tct + 1

  • t′∈V (Tct′ + 1) =

Tct + 1 (

t′∈V Tct′) + B

B is the number of different words (in this case the size of the vocabulary: |V | = M)

33 / 46

slide-34
SLIDE 34

Naive Bayes: Summary

Estimate parameters from the training corpus using add-one smoothing For a new document, for each class, compute sum of (i) log of prior, and (ii) logs of conditional probabilities of the terms Assign the document to the class with the largest score

34 / 46

slide-35
SLIDE 35

Naive Bayes: Training

TrainMultinomialNB(C, D) 1 V ← ExtractVocabulary(D) 2 N ← CountDocs(D) 3 for each c ∈ C 4 do Nc ← CountDocsInClass(D, c) 5 prior[c] ← Nc/N 6 textc ← ConcatenateTextOfAllDocsInClass(D, c) 7 for each t ∈ V 8 do Tct ← CountTokensOfTerm(textc, t) 9 for each t ∈ V 10 do condprob[t][c] ←

Tct+1 P

t′(Tct′+1)

11 return V , prior, condprob

35 / 46

slide-36
SLIDE 36

Naive Bayes: Testing

ApplyMultinomialNB(C, V , prior, condprob, d) 1 W ← ExtractTokensFromDoc(V , d) 2 for each c ∈ C 3 do score[c] ← log prior[c] 4 for each t ∈ W 5 do score[c]+ = log condprob[t][c] 6 return arg maxc∈C score[c]

36 / 46

slide-37
SLIDE 37

Exercise

docID words in document in c = China? training set 1 Chinese Beijing Chinese yes 2 Chinese Chinese Shanghai yes 3 Chinese Macao yes 4 Tokyo Japan Chinese no test set 5 Chinese Chinese Chinese Tokyo Japan ? Estimate parameters of Naive Bayes classifier Classify test document

37 / 46

slide-38
SLIDE 38

Example: Parameter estimates

Priors: ˆ P(c) = 3/4 and ˆ P(c) = 1/4 Conditional probabilities: ˆ P(Chinese|c) = (5 + 1)/(8 + 6) = 6/14 = 3/7 ˆ P(Tokyo|c) = ˆ P(Japan|c) = (0 + 1)/(8 + 6) = 1/14 ˆ P(Chinese|c) = ˆ P(Tokyo|c) = ˆ P(Japan|c) = (1 + 1)/(3 + 6) = 2/9 The denominators are (8 + 6) and (3 + 6) because the lengths of textc and textc are 8 and 3, respectively, and because the constant B is 6 since the vocabulary consists of six terms.

Exercise: verify that ˆ P(Chinese|c) + ˆ P(Beijing|c) + ˆ P(Shanghai|c) + ˆ P(Macao|c) + ˆ P(Tokyo|c) + ˆ P(Japan|c) = 1 and ˆ P(Chinese|c) + ˆ P(Beijing|c) + ˆ P(Shanghai|c) + ˆ P(Macao|c) + ˆ P(Tokyo|c) + ˆ P(Japan|c) = 1

38 / 46

slide-39
SLIDE 39

Example: Classification

d5 = (Chinese Chinese Chinese Tokyo Japan) ˆ P(c|d5) ∝ 3/4 · (3/7)3 · 1/14 · 1/14 ≈ 0.0003 ˆ P(c|d5) ∝ 1/4 · (2/9)3 · 2/9 · 2/9 ≈ 0.0001 Thus, the classifier assigns the test document to c = China: the three occurrences of the positive indicator Chinese in d5

  • utweigh the occurrences of the two negative indicators Japan and

Tokyo. Exercise: evaluate ˆ P(c|d) and ˆ P(c|d) for d6 = (Chinese Chinese Tokyo Japan) and d7 = (Chinese Tokyo Japan)

39 / 46

slide-40
SLIDE 40

Time complexity of Naive Bayes

mode time complexity training Θ(|D|Lave + |C||V |) testing Θ(La + |C|Ma) = Θ(|C|Ma) Lave: the average length of a doc, La: length of the test doc, Ma: number of distinct terms in the test doc Θ(|D|Lave) is the time it takes to compute all counts. Θ(|C||V |) is the time it takes to compute the parameters from the counts. Generally: |C||V | < |D|Lave Why? Test time is also linear (in the length of the test document). Thus: Naive Bayes is linear in the size of the training set (training) and the test document (testing). This is optimal.

40 / 46

slide-41
SLIDE 41

Outline

1

Recap

2

Text classification

3

Naive Bayes

4

Discussion

41 / 46

slide-42
SLIDE 42

Discussion 6

More Statistical Methods: Peter Norvig, “How to Write a Spelling Corrector” http://norvig.com/spell-correct.html See also http://yehha.net/20794/facebook.com/peter-norvig.html, ”Engineering@Facebook: Tech Talk with Peter Norvig” roughly 00:11:00 – 00:19:15 of a one hour video, but whole first half (or more) if you have time... or as well http://videolectures.net/cikm08 norvig slatuad/, “Statistical Learning as the Ultimate Agile Development Tool” Additional related reference: http://doi.ieeecomputersociety.org/10.1109/MIS.2009.36

  • A. Halevy, P. Norvig, F. Pereira,

The Unreasonable Effectiveness of Data, Intelligent Systems Mar/Apr 2009 (copy at readings/unrealdata.pdf)

42 / 46

slide-43
SLIDE 43

A little theory

Find the correction c that maximizes the probability of c given the

  • riginal word w:

argmaxc P(c|w) By Bayes’ Theorem, equivalent to argmaxc P(w|c)P(c)/P(w). P(w) the same for every possible c, so ignore, and consider: argmaxc P(w|c)P(c) . Three parts : P(c), the probability that a proposed correction c stands on its own. The language model: “how likely is c to appear in an English text?” (P(“the”) high, P(“zxzxzxzyyy”) near zero) P(w|c), the probability that w would be typed when author meant c. The error model: “how likely is author to type w by mistake instead of c?” argmaxc, the control mechanism: choose c that gives the best combined probability score.

43 / 46

slide-44
SLIDE 44

Example

w=“thew” two candidate corrections c=“the” and c=“thaw”. which has higher P(c|w)? “thaw” has only small change “a” to “e” “the” is a very common word, and perhaps the typist’s finger slipped off the “e” onto the “w”. To estimate P(c|w), have to consider both the probability of c and the probability of the change from c to w

44 / 46

slide-45
SLIDE 45

Complete Spelling Corrector

import re, collections def words(text): return re.findall(’[a-z]+’, text.lower()) def train(features): model = collections.defaultdict(lambda: 1) for f in features: model[f] += 1 return model NWORDS = train(words(file(’big.txt’).read())) alphabet = ’abcdefghijklmnopqrstuvwxyz’ = ⇒

45 / 46

slide-46
SLIDE 46

def edits1(word): s = [(word[:i], word[i:]) for i in range(len(word) + 1)] deletes = [a + b[1:] for a, b in s if b] transposes = [a + b[1] + b[0] + b[2:] for a, b in s if len(b)>1] replaces = [a + c + b[1:] for a, b in s for c in alphabet if b] inserts = [a + c + b for a, b in s for c in alphabet] return set(deletes + transposes + replaces + inserts)

def known edits2(word): return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in NWORDS)

def known(words): return set(w for w in words if w in NWORDS) def correct(word): candidates = known([word]) or known(edits1(word))

  • r known edits2(word) or [word]

return max(candidates, key=NWORDS.get)

46 / 46