si485i nlp
play

SI485i : NLP Set 5 Using Nave Bayes Motivation We want to predict - PowerPoint PPT Presentation

SI485i : NLP Set 5 Using Nave Bayes Motivation We want to predict something . We have some text related to this something. something = target label Y text = text features X Given X, what is the most probable Y? Motivation:


  1. SI485i : NLP Set 5 Using Naïve Bayes

  2. Motivation • We want to predict something . • We have some text related to this something. • something = target label Y • text = text features X Given X, what is the most probable Y?

  3. Motivation: Author Detection Alas the day! take heed of him; he stabbed me in mine own house, and that most beastly: in good X = faith, he cares not what mischief he does. If his weapon be out: he will foin like any devil; he will spare neither man, woman, nor child. { Charles Dickens, William Shakespeare, Herman Y = Melville, Jane Austin, Homer, Leo Tolstoy }     arg max ( ) ( | ) Y P Y y P X Y y k k y k

  4. More Motivation P(Y=spam | X=email) P(Y=worthy | X=review sentence)

  5. The Naïve Bayes Classifier ( ) ( | ) P Y P X Y  i j i • Recall Bayes rule: ( | ) P Y X i j ( ) P X j • Which is short for:    ( ) ( | ) P Y y P X x Y y    i j i ( | ) P Y y X x  i j ( ) P X x j • We can re-write this as:    ( ) ( | ) P Y y P X x Y y    i j i ( | ) P Y y X x     i j ( | ) ( ) P X x Y y P Y y j k k k Remaining slides adapted from Tom Mitchell. 5

  6. Deriving Naïve Bayes • Idea: use the training data to directly estimate: and ( | ) ( Y ) P X Y P • We can use these values to estimate using Bayes rule. ( | ) P Y X new • Recall that representing the full joint probability is not practical.   ( | ) ( , , , | ) P X Y P X X X Y 1 2 n 6

  7. Deriving Naïve Bayes • However, if we make the assumption that the attributes are independent, estimation is easy!   1  ( , , | ) ( | ) P X X Y P X Y n i i • In other words, we assume all attributes are conditionally independent given Y. • Often this assumption is violated in practice, but more on that later… 7

  8. Deriving Naïve Bayes  • Let and label Y be discrete. 1  , , X X X n ( | ) ( i ) • Then, we can estimate P X i Y P Y and i directly from the training data by counting! Sky Temp Humid Wind Water Forecast Play? sunny warm normal strong warm same yes sunny warm high strong warm same yes rainy cold high strong warm change no sunny warm high strong cool change yes P(Sky = sunny | Play = yes) = ? P(Humid = high | Play = yes) = ? 8

  9. The Naïve Bayes Classifier • Now we have:    ( ) ( | ) P Y y P X Y y   j i j 1  i ( | , , ) P Y y X X     j n ( ) ( | ) P Y y P X Y y k i k k i • To classify a new point X new :      arg max ( ) ( | ) Y P Y y P X Y y new k i k y k i 9

  10. The Naïve Bayes Algorithm • For each value y k • Estimate P(Y = y k ) from the data. • For each value x ij of each attribute X i • Estimate P(X i =x ij | Y = y k ) • Classify a new point via:      arg max ( ) ( | ) Y P Y y P X Y y new k i k y k i • In practice, the independence assumption doesn’t often hold true, but Naïve Bayes performs very well despite it. 10

  11. An alternate view of NB as LMs Y1 = dickens Y2 = twain P(Y1) * P(X | Y1) P(Y2) * P(X | Y2) P(X | Y1) = P Y1 (X) P(X | Y2) = P Y2 (X) Bigrams: Bigrams: P Y1 (X) = 𝑄 𝑍1 (𝑦 𝑗 |𝑦 𝑗 − 1) P Y2 (X) = 𝑄 𝑍2 (𝑦 𝑗 |𝑦 𝑗 − 1)

  12. Naïve Bayes Applications • Text classification • Which e-mails are spam? • Which e-mails are meeting notices? • Which author wrote a document? • Which webpages are about current events? • Which blog contains angry writing? • What sentence in a document talks about company X? • etc. 12

  13. Text and Features   1  ( , , | ) ( | ) P X X Y P X Y n i i • What is X i ? • Could be unigrams, hopefully bigrams too. • It can be anything that is computed from the text X. • Yes, I really mean anything. Creativity and intuition into language is where the real gains come from in NLP. • Non n-gram examples: • X 10 = “the number of sentences that begin with conjunctions” • X 356 = “existence of a semi - colon in the paragraph”

  14. Features • In machine learning, “features” are the attributes to which you assign weights (probabilities in Naïve Bayes) that help in the final classification. • Up until now, your features have been n-grams. You now want to consider other types of features. • You count features just like n-grams. How many did you see? • X = set of features • P(Y|X) = probability of a Y given a set of features

  15. How do you count features? • Feature idea: “a semicolon exists in this sentence” • Count them: • Count(“FEAT - SEMICOLON”, 1) • Make up a unique name for the feature, then count! • Compute probability: • P(“FEAT - SEMICOLON” | author=“dickens”) = Count(“FEAT - SEMICOLON”) / (# dickens sentences)

  16. Authorship Lab 1. Figure out how to use your Language Models from Lab 2. They can be your initial features. • Can you train() a model on one author’s text? 2. P(dickens | text) = P(dickens) * P BigramModel (text) 3. New code for new features. Call your language models, get a probability, and then multiply new feature probabilities.

Recommend


More recommend