Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Naive Bayes

Intuition

Pen and Paper Exercises

Exercise 1

Problem

Suppose we have the following training data to predict whether a person buys a computer or not:

RIDageincomestudentcredit_ratingClass: buys_computer
1<=30highnofairno
2<=30highnoexcellentno
331...40highnofairyes
4>40mediumnofairyes
5>40lowyesfairyes
6>40lowyesexcellentno
731...40lowyesexcellentyes
8<=30mediumnofairno
9<=30lowyesfairyes
10>40mediumyesfairyes
11<=30mediumyesexcellentyes
1231...40mediumnoexcellentyes
1331...40highyesfairyes
14>40mediumnoexcellentno

Using the Naive Bayes algorithm to

a. Calculate the prior probabilities of the classes.

b. Predict whether a person buys a computer or not given age<=30, income=medium, student=yes, credit-rating=fair.

Answer

a. Let BB be the event that a person buys a computer, and Bˉ\bar{B} be the event that a person does not buy a computer. Then the prior probabilities are:

P(B)=count(B)count(B)+count(Bˉ)=914P(B) = \frac{count(B)}{count(B) + count(\bar{B})} = \frac{9}{14}
P(Bˉ)=count(Bˉ)count(B)+count(Bˉ)=514P(\bar{B}) = \frac{count(\bar{B})}{count(B) + count(\bar{B})} = \frac{5}{14}

b. The posterior probabilities of the classes are given by:

Let A1=age≤30A_1 = \text{age} \leq 30, A2=income=mediumA_2 = \text{income} = \text{medium}, A3=student=yesA_3 = \text{student} = \text{yes}, and A4=credit_rating=fairA_4 = \text{credit\_rating} = \text{fair}, then A={A1,A2,A3,A4}A = \{A_1, A_2, A_3, A_4\}.

Then, for each event Ai∈AA_i \in A, we have:

  • P(A1∣B)=count(A1∩B)count(B)=29 P(A_1 | B) = \frac{count(A_1 \cap B)}{count(B)} = \frac{2}{9} and P(A1∣Bˉ)=count(A1∩Bˉ)count(Bˉ)=35 P(A_1 | \bar{B}) = \frac{count(A_1 \cap \bar{B})}{count(\bar{B})} = \frac{3}{5}

  • P(A2∣B)=count(A2∩B)count(B)=49 P(A_2 | B) = \frac{count(A_2 \cap B)}{count(B)} = \frac{4}{9} and P(A2∣Bˉ)=count(A2∩Bˉ)count(Bˉ)=25P(A_2 | \bar{B}) = \frac{count(A_2 \cap \bar{B})}{count(\bar{B})} = \frac{2}{5}

  • P(A3∣B)=count(A3∩B)count(B)=69 P(A_3 | B) = \frac{count(A_3 \cap B)}{count(B)} = \frac{6}{9} and P(A3∣Bˉ)=count(A3∩Bˉ)count(Bˉ)=15P(A_3 | \bar{B}) = \frac{count(A_3 \cap \bar{B})}{count(\bar{B})} = \frac{1}{5}

  • P(A4∣B)=count(A4∩B)count(B)=69 P(A_4 | B) = \frac{count(A_4 \cap B)}{count(B)} = \frac{6}{9} and P(A4∣Bˉ)=count(A4∩Bˉ)count(Bˉ)=25P(A_4 | \bar{B}) = \frac{count(A_4 \cap \bar{B})}{count(\bar{B})} = \frac{2}{5}

The likelihood of the evidence AA for each class is given by:

  • P(A∣B)=P(A1∣B)×P(A2∣B)×P(A3∣B)×P(A4∣B)=29×49×69×69=32729 P(A | B) = P(A_1 | B) \times P(A_2 | B) \times P(A_3 | B) \times P(A_4 | B) = \frac{2}{9} \times \frac{4}{9} \times \frac{6}{9} \times \frac{6}{9} = \frac{32}{729}

  • P(A∣Bˉ)=P(A1∣Bˉ)×P(A2∣Bˉ)×P(A3∣Bˉ)×P(A4∣Bˉ)=35×25×15×25=12625 P(A | \bar{B}) = P(A_1 | \bar{B}) \times P(A_2 | \bar{B}) \times P(A_3 | \bar{B}) \times P(A_4 | \bar{B}) = \frac{3}{5} \times \frac{2}{5} \times \frac{1}{5} \times \frac{2}{5} = \frac{12}{625}

The posterior probabilities of the classes are given by:

P(B∣A)=P(A∣B)×P(B)P(A)=32729×914P(A)≈0.028P(A)P(B | A) = \frac{P(A | B) \times P(B)}{P(A)} = \frac{\frac{32}{729} \times \frac{9}{14}}{P(A)} \approx \frac{0.028}{P(A)}

and

P(Bˉ∣A)=P(A∣Bˉ)×P(Bˉ)P(A)=12625×514P(A)≈0.007P(A)P(\bar{B} | A) = \frac{P(A | \bar{B}) \times P(\bar{B})}{P(A)} = \frac{\frac{12}{625} \times \frac{5}{14}}{P(A)} \approx \frac{0.007}{P(A)}

where P(A)P(A) is the probability of the evidence AA.

It is easy to see that P(A)P(A) is the same for both classes, so we can compare the numerators of the posterior probabilities to determine which class is more likely. Since P(B∣A)>P(Bˉ∣A)P(B | A) > P(\bar{B} | A), we predict that the person buys a computer.

Exercise 2

Problem

We are training a document classifier with Naive Bayes. Each document is represented by a sequence of characters (each character is a word). We have the following training data:

 Training data  d  c  d  c aaAbaAabAbbB\begin{aligned} & \text{ Training data }\\ &\begin{array}{cc|cc} \hline \text{ d } & \text{ c } & \text{ d } & \text{ c } \\ \hline aa & A & ba & A \\ ab & A & bb & B \\ \hline \end{array} \end{aligned}

a. Calculate P(A)P(A), P(B)P(B), P(a∣A)P(a|A), P(b∣A)P(b|A), P(a∣B)P(a|B), and P(b∣B)P(b|B) using Laplace smoothing for the conditional probabilities.

b. For each of the following new data points, predict the class of the data point:

  • aabaaaba

  • aa

  • bbbabbba

  • bccbbabccbba

  • bbbbbbbb

Answer

a. The probabilities are:

  • P(A)=count(A)count(A)+count(B)=34P(A) = \dfrac{count(A)}{count(A) + count(B)} = \dfrac{3}{4} and P(B)=count(B)count(A)+count(B)=14P(B) = \dfrac{count(B)}{count(A) + count(B)} = \dfrac{1}{4}

There are two words in the vocabulary, aa and bb. Using Laplace smoothing, we have:

  • P(a∣A)=count(a∣A)+1∑w∈Vcount(w∣A)+∣V∣=4+16+2=58P(a|A) = \dfrac{count(a|A) + 1}{\sum_{w \in V} count(w|A) + |V|} = \dfrac{4 + 1}{6 + 2} = \dfrac{5}{8}

  • P(b∣A)=count(b∣A)+1∑w∈Vcount(w∣A)+∣V∣=2+16+2=38P(b|A) = \dfrac{count(b|A) + 1}{\sum_{w \in V} count(w|A) + |V|} = \dfrac{2 + 1}{6 + 2} = \dfrac{3}{8}

and

  • P(a∣B)=count(a∣B)+1∑w∈Vcount(w∣B)+∣V∣=0+12+2=14P(a|B) = \dfrac{count(a|B) + 1}{\sum_{w \in V} count(w|B) + |V|} = \dfrac{0 + 1}{2 + 2} = \dfrac{1}{4}

  • P(b∣B)=count(b∣B)+1∑w∈Vcount(w∣B)+∣V∣=2+12+2=34P(b|B) = \dfrac{count(b|B) + 1}{\sum_{w \in V} count(w|B) + |V|} = \dfrac{2 + 1}{2 + 2} = \dfrac{3}{4}

where VV is the vocabulary, ∑w∈Vcount(w∣A)=6\sum_{w \in V} count(w|A) = 6 is the total count of words in class AA, ∑w∈Vcount(w∣B)=2\sum_{w \in V} count(w|B) = 2 is the total count of words in class BB, and ∣V∣|V| is the size of the vocabulary.

b. Considering each of the new data points:

  • aabaaaba:

    P(A∣aaba)=P(A)∗P(a∣A)∗P(a∣A)∗P(b∣A)∗P(a∣A)P(aaba)=34×58×58×38×58P(aaba)≈0.069P(aaba)\begin{aligned} P(A|aaba) &= \frac{P(A) * P(a|A) * P(a|A) * P(b|A) * P(a|A)}{P(aaba)} \\ &= \frac{\frac{3}{4} \times \frac{5}{8} \times \frac{5}{8} \times \frac{3}{8} \times \frac{5}{8}}{P(aaba)} \approx \frac{0.069}{P(aaba)} \end{aligned}

    P(B∣aaba)=P(B)∗P(a∣B)∗P(a∣B)∗P(b∣B)∗P(a∣B)P(aaba)=14×14×14×34×14P(aaba)≈0.003P(aaba)\begin{aligned} P(B|aaba) &= \frac{P(B) * P(a|B) * P(a|B) * P(b|B) * P(a|B)}{P(aaba)} \\ &= \frac{\frac{1}{4} \times \frac{1}{4} \times \frac{1}{4} \times \frac{3}{4} \times \frac{1}{4}}{P(aaba)} \approx \frac{0.003}{P(aaba)} \end{aligned}

    ⇒P(A∣aaba)>P(B∣aaba)\Rightarrow P(A|aaba) > P(B|aaba), so we predict that the class of the data point is AA.

  • aa:

    P(A∣a)=P(A)∗P(a∣A)P(a)=34×58P(a)≈0.469P(a)\begin{aligned} P(A|a) &= \frac{P(A) * P(a|A)}{P(a)} = \frac{\frac{3}{4} \times \frac{5}{8}}{P(a)} \approx \frac{0.469}{P(a)} \end{aligned} P(B∣a)=P(B)∗P(a∣B)P(a)=14×14P(a)≈0.063P(a)\begin{aligned} P(B|a) &= \frac{P(B) * P(a|B)}{P(a)} = \frac{\frac{1}{4} \times \frac{1}{4}}{P(a)} \approx \frac{0.063}{P(a)} \end{aligned}

    ⇒P(A∣a)>P(B∣a)\Rightarrow P(A|a) > P(B|a), so we predict that the class of the data point is AA.

  • bbbabbba:

    P(A∣bbba)=P(A)∗P(b∣A)∗P(b∣A)∗P(b∣A)∗P(a∣A)P(bbba)=34×38×38×38×58P(bbba)≈0.025P(bbba)\begin{aligned} P(A|bbba) &= \frac{P(A) * P(b|A) * P(b|A) * P(b|A) * P(a|A)}{P(bbba)} \\ &= \frac{\frac{3}{4} \times \frac{3}{8} \times \frac{3}{8} \times \frac{3}{8} \times \frac{5}{8}}{P(bbba)} \approx \frac{0.025}{P(bbba)} \end{aligned} P(B∣bbba)=P(B)∗P(b∣B)∗P(b∣B)∗P(b∣B)∗P(a∣B)P(bbba)=14×34×34×34×14P(bbba)≈0.026P(bbba)\begin{aligned} P(B|bbba) &= \frac{P(B) * P(b|B) * P(b|B) * P(b|B) * P(a|B)}{P(bbba)} \\ &= \frac{\frac{1}{4} \times \frac{3}{4} \times \frac{3}{4} \times \frac{3}{4} \times \frac{1}{4}}{P(bbba)} \approx \frac{0.026}{P(bbba)} \end{aligned}

    ⇒P(A∣bbba)<P(B∣bbba)\Rightarrow P(A|bbba) < P(B|bbba), so we predict that the class of the data point is BB.

  • bccbbabccbba: we have new word ‘c’ that was not in the vocabulary during training. The data point is reduced to ‘bbba’ and we can use the probabilities calculated for ‘bbba’ to predict the class of the data point.

    ⇒\Rightarrow bccbbabccbba is predicted to be in class BB (similar to bbbabbba).

  • bbbbbbbb:

    P(A∣bbbb)=P(A)∗P(b∣A)∗P(b∣A)∗P(b∣A)∗P(b∣A)P(bbbb)=34×38×38×38×38P(bbbb)≈0.015P(bbbb)\begin{aligned} P(A|bbbb) &= \frac{P(A) * P(b|A) * P(b|A) * P(b|A) * P(b|A)}{P(bbbb)} \\ &= \frac{\frac{3}{4} \times \frac{3}{8} \times \frac{3}{8} \times \frac{3}{8} \times \frac{3}{8}}{P(bbbb)} \approx \frac{0.015}{P(bbbb)} \end{aligned} P(B∣bbbb)=P(B)∗P(b∣B)∗P(b∣B)∗P(b∣B)∗P(b∣B)P(bbbb)=14×34×34×34×34P(bbbb)≈0.079P(bbbb)\begin{aligned} P(B|bbbb) &= \frac{P(B) * P(b|B) * P(b|B) * P(b|B) * P(b|B)}{P(bbbb)} \\ &= \frac{\frac{1}{4} \times \frac{3}{4} \times \frac{3}{4} \times \frac{3}{4} \times \frac{3}{4}}{P(bbbb)} \approx \frac{0.079}{P(bbbb)} \end{aligned}

    ⇒bbbb\Rightarrow bbbb is predicted to be in class BB.

Practice