LO 7.2.3.D

Learning Objective: Determine the class that a test sentence belongs to using the Naïve Bayes classifier.

Review:

Assume we have five text documents and two classes: ‘+’/positive comment and ‘-’/negative comment. The class categorization of the five documents are described in the figure below.

Source: Assigned reading

The goal is to classify the text “predictable with no fun” either as ‘+’/positive comment or ‘-’/negative comment. Please, first note that the word “with” is not present in the training data, so we can drop it completely. Thus, the sentence we are going to classify is reduced to “predictable no fun”.

As we are using a naïve Bayes classifier , the returned class is the one that yield the max posterior probability

Thus, our next steps are to calculate the posterior probabilities and the class associated with the larger probability will be the returned class.

The prior probability has been calculated in LO 7.2.3.C

<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi><mfenced><mrow><mi>c</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mo>&#xA0;</mo><mo>+</mo></mrow></mfenced><mo>=</mo><mfrac><mn>2</mn><mn>5</mn></mfrac></math> 

The conditional probabilities are computed using Laplace Smoothing with a vocabulary of 20 words

<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi><mfenced><mrow><mtext>predictable</mtext><mo>&#xA0;</mo><menclose notation="left"><mi>c</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mo>&#xA0;+</mo></menclose></mrow></mfenced><mo>=</mo><mfrac><mrow><mn>0</mn><mo>+</mo><mn>1</mn></mrow><mrow><mn>9</mn><mo>+</mo><mn>20</mn></mrow></mfrac><mo>=</mo><mfrac><mn>1</mn><mn>29</mn></mfrac><mspace linebreak="newline"/><mi>P</mi><mfenced><mrow><mtext>no</mtext><mo>&#xA0;</mo><menclose notation="left"><mi>c</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mo>&#xA0;+</mo></menclose></mrow></mfenced><mo>=</mo><mfrac><mrow><mn>0</mn><mo>+</mo><mn>1</mn></mrow><mrow><mn>9</mn><mo>+</mo><mn>20</mn></mrow></mfrac><mo>=</mo><mfrac><mn>1</mn><mn>29</mn></mfrac><mspace linebreak="newline"/><mi>P</mi><mfenced><mrow><mtext>fun</mtext><mo>&#xA0;</mo><menclose notation="left"><mi>c</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mo>&#xA0;+</mo></menclose></mrow></mfenced><mo>=</mo><mfrac><mrow><mn>1</mn><mo>+</mo><mn>1</mn></mrow><mrow><mn>9</mn><mo>+</mo><mn>20</mn></mrow></mfrac><mo>=</mo><mfrac><mn>2</mn><mn>29</mn></mfrac></math>

         Thus, the posterior probability is given by

<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi><mi>o</mi><mi>s</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>i</mi><mi>o</mi><mi>r</mi><mo>&#xA0;</mo><mi>P</mi><mi>r</mi><mi>o</mi><mi>b</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi><mfenced><mrow><mi>c</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mo>&#xA0;</mo><mo>+</mo></mrow></mfenced><mo>&#xA0;</mo><mo>=</mo><mi>P</mi><mfenced><mrow><mi>c</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mo>&#xA0;</mo><mo>+</mo></mrow></mfenced><mo>&#xB7;</mo><munder><mrow><mo>&#x220F;</mo><mi>P</mi><mfenced><mrow><msub><mi>w</mi><mi>i</mi></msub><menclose notation="left"><mi>c</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mo>&#xA0;</mo><mo>+</mo></menclose></mrow></mfenced></mrow><mrow/></munder><mspace linebreak="newline"/><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>=</mo><mfrac><mn>2</mn><mn>5</mn></mfrac><mo>&#xD7;</mo><mfrac><mrow><mn>1</mn><mo>&#xD7;</mo><mn>1</mn><mo>&#xD7;</mo><mn>2</mn></mrow><msup><mn>29</mn><mn>3</mn></msup></mfrac><mo>=</mo><mn>3</mn><mo>.</mo><mn>2</mn><mo>&#xD7;</mo><msup><mn>10</mn><mrow><mo>-</mo><mn>5</mn></mrow></msup></math>

The prior probability has been calculated in LO 7.2.3.C

<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi><mfenced><mrow><mi>c</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mo>&#xA0;</mo><mo>-</mo></mrow></mfenced><mo>=</mo><mfrac><mn>3</mn><mn>5</mn></mfrac></math> 

The conditional probabilities are computed using Laplace Smoothing with a vocabulary of 20 words

<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi><mfenced><mrow><mtext>predictable</mtext><mo>&#xA0;</mo><menclose notation="left"><mi>c</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mo>&#xA0;</mo><mo>-</mo></menclose></mrow></mfenced><mo>=</mo><mfrac><mrow><mn>1</mn><mo>+</mo><mn>1</mn></mrow><mrow><mn>14</mn><mo>+</mo><mn>20</mn></mrow></mfrac><mo>=</mo><mfrac><mn>2</mn><mn>34</mn></mfrac><mspace linebreak="newline"/><mi>P</mi><mfenced><mrow><mtext>no</mtext><mo>&#xA0;</mo><menclose notation="left"><mi>c</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mo>&#xA0;</mo><mo>-</mo></menclose></mrow></mfenced><mo>=</mo><mfrac><mrow><mn>1</mn><mo>+</mo><mn>1</mn></mrow><mrow><mn>14</mn><mo>+</mo><mn>20</mn></mrow></mfrac><mo>=</mo><mfrac><mn>2</mn><mn>34</mn></mfrac><mspace linebreak="newline"/><mi>P</mi><mfenced><mrow><mtext>fun</mtext><mo>&#xA0;</mo><menclose notation="left"><mi>c</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mo>&#xA0;</mo><mo>-</mo></menclose></mrow></mfenced><mo>=</mo><mfrac><mrow><mn>0</mn><mo>+</mo><mn>1</mn></mrow><mrow><mn>14</mn><mo>+</mo><mn>20</mn></mrow></mfrac><mo>=</mo><mfrac><mn>1</mn><mn>34</mn></mfrac></math>

         

Thus, the posterior probability is given by

<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi><mi>o</mi><mi>s</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>i</mi><mi>o</mi><mi>r</mi><mo>&#xA0;</mo><mi>P</mi><mi>r</mi><mi>o</mi><mi>b</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi><mfenced><mrow><mi>c</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mo>&#xA0;</mo><mo>-</mo></mrow></mfenced><mo>&#xA0;</mo><mo>=</mo><mi>P</mi><mfenced><mrow><mi>c</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mo>&#xA0;</mo><mo>-</mo></mrow></mfenced><mo>&#xB7;</mo><munder><mrow><mo>&#x220F;</mo><mi>P</mi><mfenced><mrow><msub><mi>w</mi><mi>i</mi></msub><menclose notation="left"><mi>c</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mo>&#xA0;</mo><mo>-</mo></menclose></mrow></mfenced></mrow><mrow/></munder><mspace linebreak="newline"/><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>&#xA0;</mo><mo>=</mo><mfrac><mn>3</mn><mn>5</mn></mfrac><mo>&#xD7;</mo><mfrac><mrow><mn>2</mn><mo>&#xD7;</mo><mn>2</mn><mo>&#xD7;</mo><mn>1</mn></mrow><msup><mn>14</mn><mn>3</mn></msup></mfrac><mo>=</mo><mn>6</mn><mo>.</mo><mn>1</mn><mo>&#xD7;</mo><msup><mn>10</mn><mrow><mo>-</mo><mn>5</mn></mrow></msup></math>

Given that the posterior probability of class “negative'' is the larger one, the Naïve Bayes classifier predicts the sentence “predictable with no fun” is indeed a negative comment!