Problem ¶ Suppose we have the following training data to predict whether a person buys a computer or not:
RID age income student credit_rating Class: buys_computer 1 <=30 high no fair no 2 <=30 high no excellent no 3 31...40 high no fair yes 4 >40 medium no fair yes 5 >40 low yes fair yes 6 >40 low yes excellent no 7 31...40 low yes excellent yes 8 <=30 medium no fair no 9 <=30 low yes fair yes 10 >40 medium yes fair yes 11 <=30 medium yes excellent yes 12 31...40 medium no excellent yes 13 31...40 high yes fair yes 14 >40 medium no excellent no
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.
a. Let B B B be the event that a person buys a computer, and B ˉ \bar{B} B ˉ be the event that a person does not buy a computer. Then the prior probabilities are:
P ( B ) = c o u n t ( B ) c o u n t ( B ) + c o u n t ( B ˉ ) = 9 14 P(B) = \frac{count(B)}{count(B) + count(\bar{B})} = \frac{9}{14} P ( B ) = co u n t ( B ) + co u n t ( B ˉ ) co u n t ( B ) = 14 9 P ( B ˉ ) = c o u n t ( B ˉ ) c o u n t ( B ) + c o u n t ( B ˉ ) = 5 14 P(\bar{B}) = \frac{count(\bar{B})}{count(B) + count(\bar{B})} = \frac{5}{14} P ( B ˉ ) = co u n t ( B ) + co u n t ( B ˉ ) co u n t ( B ˉ ) = 14 5 b. The posterior probabilities of the classes are given by:
Let A 1 = age ≤ 30 A_1 = \text{age} \leq 30 A 1 = age ≤ 30 , A 2 = income = medium A_2 = \text{income} = \text{medium} A 2 = income = medium , A 3 = student = yes A_3 = \text{student} = \text{yes} A 3 = student = yes , and A 4 = credit_rating = fair A_4 = \text{credit\_rating} = \text{fair} A 4 = credit_rating = fair , then A = { A 1 , A 2 , A 3 , A 4 } A = \{A_1, A_2, A_3, A_4\} A = { A 1 , A 2 , A 3 , A 4 } .
Then, for each event A i ∈ A A_i \in A A i ∈ A , we have:
P ( A 1 ∣ B ) = c o u n t ( A 1 ∩ B ) c o u n t ( B ) = 2 9 P(A_1 | B) = \frac{count(A_1 \cap B)}{count(B)} = \frac{2}{9} P ( A 1 ∣ B ) = co u n t ( B ) co u n t ( A 1 ∩ B ) = 9 2 and P ( A 1 ∣ B ˉ ) = c o u n t ( A 1 ∩ B ˉ ) c o u n t ( B ˉ ) = 3 5 P(A_1 | \bar{B}) = \frac{count(A_1 \cap \bar{B})}{count(\bar{B})} = \frac{3}{5} P ( A 1 ∣ B ˉ ) = co u n t ( B ˉ ) co u n t ( A 1 ∩ B ˉ ) = 5 3
P ( A 2 ∣ B ) = c o u n t ( A 2 ∩ B ) c o u n t ( B ) = 4 9 P(A_2 | B) = \frac{count(A_2 \cap B)}{count(B)} = \frac{4}{9} P ( A 2 ∣ B ) = co u n t ( B ) co u n t ( A 2 ∩ B ) = 9 4 and P ( A 2 ∣ B ˉ ) = c o u n t ( A 2 ∩ B ˉ ) c o u n t ( B ˉ ) = 2 5 P(A_2 | \bar{B}) = \frac{count(A_2 \cap \bar{B})}{count(\bar{B})} = \frac{2}{5} P ( A 2 ∣ B ˉ ) = co u n t ( B ˉ ) co u n t ( A 2 ∩ B ˉ ) = 5 2
P ( A 3 ∣ B ) = c o u n t ( A 3 ∩ B ) c o u n t ( B ) = 6 9 P(A_3 | B) = \frac{count(A_3 \cap B)}{count(B)} = \frac{6}{9} P ( A 3 ∣ B ) = co u n t ( B ) co u n t ( A 3 ∩ B ) = 9 6 and P ( A 3 ∣ B ˉ ) = c o u n t ( A 3 ∩ B ˉ ) c o u n t ( B ˉ ) = 1 5 P(A_3 | \bar{B}) = \frac{count(A_3 \cap \bar{B})}{count(\bar{B})} = \frac{1}{5} P ( A 3 ∣ B ˉ ) = co u n t ( B ˉ ) co u n t ( A 3 ∩ B ˉ ) = 5 1
P ( A 4 ∣ B ) = c o u n t ( A 4 ∩ B ) c o u n t ( B ) = 6 9 P(A_4 | B) = \frac{count(A_4 \cap B)}{count(B)} = \frac{6}{9} P ( A 4 ∣ B ) = co u n t ( B ) co u n t ( A 4 ∩ B ) = 9 6 and P ( A 4 ∣ B ˉ ) = c o u n t ( A 4 ∩ B ˉ ) c o u n t ( B ˉ ) = 2 5 P(A_4 | \bar{B}) = \frac{count(A_4 \cap \bar{B})}{count(\bar{B})} = \frac{2}{5} P ( A 4 ∣ B ˉ ) = co u n t ( B ˉ ) co u n t ( A 4 ∩ B ˉ ) = 5 2
The likelihood of the evidence A A A for each class is given by:
P ( A ∣ B ) = P ( A 1 ∣ B ) × P ( A 2 ∣ B ) × P ( A 3 ∣ B ) × P ( A 4 ∣ B ) = 2 9 × 4 9 × 6 9 × 6 9 = 32 729 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 ( A 1 ∣ B ) × P ( A 2 ∣ B ) × P ( A 3 ∣ B ) × P ( A 4 ∣ B ) = 9 2 × 9 4 × 9 6 × 9 6 = 729 32
P ( A ∣ B ˉ ) = P ( A 1 ∣ B ˉ ) × P ( A 2 ∣ B ˉ ) × P ( A 3 ∣ B ˉ ) × P ( A 4 ∣ B ˉ ) = 3 5 × 2 5 × 1 5 × 2 5 = 12 625 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} P ( A ∣ B ˉ ) = P ( A 1 ∣ B ˉ ) × P ( A 2 ∣ B ˉ ) × P ( A 3 ∣ B ˉ ) × P ( A 4 ∣ B ˉ ) = 5 3 × 5 2 × 5 1 × 5 2 = 625 12
The posterior probabilities of the classes are given by:
P ( B ∣ A ) = P ( A ∣ B ) × P ( B ) P ( A ) = 32 729 × 9 14 P ( A ) ≈ 0.028 P ( 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)} P ( B ∣ A ) = P ( A ) P ( A ∣ B ) × P ( B ) = P ( A ) 729 32 × 14 9 ≈ P ( A ) 0.028 and
P ( B ˉ ∣ A ) = P ( A ∣ B ˉ ) × P ( B ˉ ) P ( A ) = 12 625 × 5 14 P ( A ) ≈ 0.007 P ( 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)} P ( B ˉ ∣ A ) = P ( A ) P ( A ∣ B ˉ ) × P ( B ˉ ) = P ( A ) 625 12 × 14 5 ≈ P ( A ) 0.007 where P ( A ) P(A) P ( A ) is the probability of the evidence A A A .
It is easy to see that P ( A ) 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) P ( B ∣ A ) > P ( B ˉ ∣ A ) , we predict that the person buys a computer.
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 a a A b a A a b A b b B \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} Training data d aa ab c A A d ba bb c A B a. Calculate P ( A ) P(A) P ( A ) , P ( B ) P(B) P ( B ) , P ( a ∣ A ) P(a|A) P ( a ∣ A ) , P ( b ∣ A ) P(b|A) P ( b ∣ A ) , P ( a ∣ B ) P(a|B) P ( a ∣ B ) , and P ( b ∣ B ) 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:
a a b a aaba aaba
a a a
b b b a bbba bbba
b c c b b a bccbba b cc bba
b b b b bbbb bbbb
a. The probabilities are:
There are two words in the vocabulary, a a a and b b b . Using Laplace smoothing, we have:
P ( a ∣ A ) = c o u n t ( a ∣ A ) + 1 ∑ w ∈ V c o u n t ( w ∣ A ) + ∣ V ∣ = 4 + 1 6 + 2 = 5 8 P(a|A) = \dfrac{count(a|A) + 1}{\sum_{w \in V} count(w|A) + |V|} = \dfrac{4 + 1}{6 + 2} = \dfrac{5}{8} P ( a ∣ A ) = ∑ w ∈ V co u n t ( w ∣ A ) + ∣ V ∣ co u n t ( a ∣ A ) + 1 = 6 + 2 4 + 1 = 8 5
P ( b ∣ A ) = c o u n t ( b ∣ A ) + 1 ∑ w ∈ V c o u n t ( w ∣ A ) + ∣ V ∣ = 2 + 1 6 + 2 = 3 8 P(b|A) = \dfrac{count(b|A) + 1}{\sum_{w \in V} count(w|A) + |V|} = \dfrac{2 + 1}{6 + 2} = \dfrac{3}{8} P ( b ∣ A ) = ∑ w ∈ V co u n t ( w ∣ A ) + ∣ V ∣ co u n t ( b ∣ A ) + 1 = 6 + 2 2 + 1 = 8 3
and
P ( a ∣ B ) = c o u n t ( a ∣ B ) + 1 ∑ w ∈ V c o u n t ( w ∣ B ) + ∣ V ∣ = 0 + 1 2 + 2 = 1 4 P(a|B) = \dfrac{count(a|B) + 1}{\sum_{w \in V} count(w|B) + |V|} = \dfrac{0 + 1}{2 + 2} = \dfrac{1}{4} P ( a ∣ B ) = ∑ w ∈ V co u n t ( w ∣ B ) + ∣ V ∣ co u n t ( a ∣ B ) + 1 = 2 + 2 0 + 1 = 4 1
P ( b ∣ B ) = c o u n t ( b ∣ B ) + 1 ∑ w ∈ V c o u n t ( w ∣ B ) + ∣ V ∣ = 2 + 1 2 + 2 = 3 4 P(b|B) = \dfrac{count(b|B) + 1}{\sum_{w \in V} count(w|B) + |V|} = \dfrac{2 + 1}{2 + 2} = \dfrac{3}{4} P ( b ∣ B ) = ∑ w ∈ V co u n t ( w ∣ B ) + ∣ V ∣ co u n t ( b ∣ B ) + 1 = 2 + 2 2 + 1 = 4 3
where V V V is the vocabulary, ∑ w ∈ V c o u n t ( w ∣ A ) = 6 \sum_{w \in V} count(w|A) = 6 ∑ w ∈ V co u n t ( w ∣ A ) = 6 is the total count of words in class A A A , ∑ w ∈ V c o u n t ( w ∣ B ) = 2 \sum_{w \in V} count(w|B) = 2 ∑ w ∈ V co u n t ( w ∣ B ) = 2 is the total count of words in class B B B , and ∣ V ∣ |V| ∣ V ∣ is the size of the vocabulary.
b. Considering each of the new data points:
a a b a aaba aaba :
P ( A ∣ a a b a ) = P ( A ) ∗ P ( a ∣ A ) ∗ P ( a ∣ A ) ∗ P ( b ∣ A ) ∗ P ( a ∣ A ) P ( a a b a ) = 3 4 × 5 8 × 5 8 × 3 8 × 5 8 P ( a a b a ) ≈ 0.069 P ( a a b a ) \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 ( A ∣ aaba ) = P ( aaba ) P ( A ) ∗ P ( a ∣ A ) ∗ P ( a ∣ A ) ∗ P ( b ∣ A ) ∗ P ( a ∣ A ) = P ( aaba ) 4 3 × 8 5 × 8 5 × 8 3 × 8 5 ≈ P ( aaba ) 0.069
P ( B ∣ a a b a ) = P ( B ) ∗ P ( a ∣ B ) ∗ P ( a ∣ B ) ∗ P ( b ∣ B ) ∗ P ( a ∣ B ) P ( a a b a ) = 1 4 × 1 4 × 1 4 × 3 4 × 1 4 P ( a a b a ) ≈ 0.003 P ( a a b a ) \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 ( B ∣ aaba ) = P ( aaba ) P ( B ) ∗ P ( a ∣ B ) ∗ P ( a ∣ B ) ∗ P ( b ∣ B ) ∗ P ( a ∣ B ) = P ( aaba ) 4 1 × 4 1 × 4 1 × 4 3 × 4 1 ≈ P ( aaba ) 0.003
⇒ P ( A ∣ a a b a ) > P ( B ∣ a a b a ) \Rightarrow P(A|aaba) > P(B|aaba) ⇒ P ( A ∣ aaba ) > P ( B ∣ aaba ) , so we predict that the class of the data point is A A A .
a a a :
P ( A ∣ a ) = P ( A ) ∗ P ( a ∣ A ) P ( a ) = 3 4 × 5 8 P ( a ) ≈ 0.469 P ( 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 ( A ∣ a ) = P ( a ) P ( A ) ∗ P ( a ∣ A ) = P ( a ) 4 3 × 8 5 ≈ P ( a ) 0.469
P ( B ∣ a ) = P ( B ) ∗ P ( a ∣ B ) P ( a ) = 1 4 × 1 4 P ( a ) ≈ 0.063 P ( 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 ( B ∣ a ) = P ( a ) P ( B ) ∗ P ( a ∣ B ) = P ( a ) 4 1 × 4 1 ≈ P ( a ) 0.063
⇒ P ( A ∣ a ) > P ( B ∣ a ) \Rightarrow P(A|a) > P(B|a) ⇒ P ( A ∣ a ) > P ( B ∣ a ) , so we predict that the class of the data point is A A A .
b b b a bbba bbba :
P ( A ∣ b b b a ) = P ( A ) ∗ P ( b ∣ A ) ∗ P ( b ∣ A ) ∗ P ( b ∣ A ) ∗ P ( a ∣ A ) P ( b b b a ) = 3 4 × 3 8 × 3 8 × 3 8 × 5 8 P ( b b b a ) ≈ 0.025 P ( b b b a ) \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 ( A ∣ bbba ) = P ( bbba ) P ( A ) ∗ P ( b ∣ A ) ∗ P ( b ∣ A ) ∗ P ( b ∣ A ) ∗ P ( a ∣ A ) = P ( bbba ) 4 3 × 8 3 × 8 3 × 8 3 × 8 5 ≈ P ( bbba ) 0.025
P ( B ∣ b b b a ) = P ( B ) ∗ P ( b ∣ B ) ∗ P ( b ∣ B ) ∗ P ( b ∣ B ) ∗ P ( a ∣ B ) P ( b b b a ) = 1 4 × 3 4 × 3 4 × 3 4 × 1 4 P ( b b b a ) ≈ 0.026 P ( b b b a ) \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 ( B ∣ bbba ) = P ( bbba ) P ( B ) ∗ P ( b ∣ B ) ∗ P ( b ∣ B ) ∗ P ( b ∣ B ) ∗ P ( a ∣ B ) = P ( bbba ) 4 1 × 4 3 × 4 3 × 4 3 × 4 1 ≈ P ( bbba ) 0.026
⇒ P ( A ∣ b b b a ) < P ( B ∣ b b b a ) \Rightarrow P(A|bbba) < P(B|bbba) ⇒ P ( A ∣ bbba ) < P ( B ∣ bbba ) , so we predict that the class of the data point is B B B .
b c c b b a bccbba b cc bba : 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 ⇒ b c c b b a bccbba b cc bba is predicted to be in class B B B (similar to b b b a bbba bbba ).
b b b b bbbb bbbb :
P ( A ∣ b b b b ) = P ( A ) ∗ P ( b ∣ A ) ∗ P ( b ∣ A ) ∗ P ( b ∣ A ) ∗ P ( b ∣ A ) P ( b b b b ) = 3 4 × 3 8 × 3 8 × 3 8 × 3 8 P ( b b b b ) ≈ 0.015 P ( b b b b ) \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 ( A ∣ bbbb ) = P ( bbbb ) P ( A ) ∗ P ( b ∣ A ) ∗ P ( b ∣ A ) ∗ P ( b ∣ A ) ∗ P ( b ∣ A ) = P ( bbbb ) 4 3 × 8 3 × 8 3 × 8 3 × 8 3 ≈ P ( bbbb ) 0.015
P ( B ∣ b b b b ) = P ( B ) ∗ P ( b ∣ B ) ∗ P ( b ∣ B ) ∗ P ( b ∣ B ) ∗ P ( b ∣ B ) P ( b b b b ) = 1 4 × 3 4 × 3 4 × 3 4 × 3 4 P ( b b b b ) ≈ 0.079 P ( b b b b ) \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} P ( B ∣ bbbb ) = P ( bbbb ) P ( B ) ∗ P ( b ∣ B ) ∗ P ( b ∣ B ) ∗ P ( b ∣ B ) ∗ P ( b ∣ B ) = P ( bbbb ) 4 1 × 4 3 × 4 3 × 4 3 × 4 3 ≈ P ( bbbb ) 0.079
⇒ b b b b \Rightarrow bbbb ⇒ bbbb is predicted to be in class B B B .
Thr probabilities P ( a a b a ) P(aaba) P ( aaba ) , P ( a ) P(a) P ( a ) , P ( b b b a ) P(bbba) P ( bbba ) , and P ( b b b b ) P(bbbb) P ( bbbb ) are the probabilities of the input features and no need to be calculated because they are not needed to compare the posterior probabilities of the classes.
However, they can be calculated as follows:
P ( a a b a ) = P ( a a b a ∣ A ) × P ( A ) + P ( a a b a ∣ B ) × P ( B ) = P ( a ∣ A ) × P ( a ∣ A ) × P ( b ∣ A ) × P ( a ∣ A ) × P ( A ) + P ( a ∣ B ) × P ( a ∣ B ) × P ( b ∣ B ) × P ( b ∣ B ) × P ( B ) \begin{aligned} P(aaba) = & P(aaba|A) \times P(A) + P(aaba|B) \times P(B) \\ = & P(a|A) \times P(a|A) \times P(b|A) \times P(a|A) \times P(A) \\ & + P(a|B) \times P(a|B) \times P(b|B) \times P(b|B) \times P(B) \end{aligned} P ( aaba ) = = P ( aaba ∣ A ) × P ( A ) + P ( aaba ∣ B ) × P ( B ) P ( a ∣ A ) × P ( a ∣ A ) × P ( b ∣ A ) × P ( a ∣ A ) × P ( A ) + P ( a ∣ B ) × P ( a ∣ B ) × P ( b ∣ B ) × P ( b ∣ B ) × P ( B )
… \ldots …