Natural Language Processing · TEE · papers

Q5b · Multinomial Naive Bayes

2022-11-22 · final · recovered AI paper

Previous examination · complete worked answers

Question. Explain Naive Bayes for text classification. Using the four documents below, classify “Chinese Chinese Chinese Tokyo Japan”: Chinese Beijing Chinese=yes; Chinese Chinese Shanghai=yes; Chinese Macao=yes; Tokyo Japan Chinese=no. [10 marks]

Answer

Multinomial NB assumes token draws are conditionally independent given the class; it combines the class prior and class-specific word probabilities. Repeated Chinese contributes its likelihood three times.

Given

Training textClass
Chinese Beijing Chineseyes
Chinese Chinese Shanghaiyes
Chinese Macaoyes
Tokyo Japan Chineseno

Test: Chinese Chinese Chinese Tokyo Japan

To find

Classify the test using multinomial Naive Bayes.

Formula: which one and why

Assume add-one smoothing unless the question supplies probabilities. Keep repeated words as repeated factors. Use training vocabulary only; ignore out-of-vocabulary words unless an unknown-token model is defined.

\mathrm{score}(c)=P(c)\prod_{w\in V}\left(\frac{c(w,c)+1}{N_c+|V|}\right)^{c(w,d)}LaTeX: \mathrm{score}(c)=P(c)\prod_{w\in V}\left(\frac{c(w,c)+1}{N_c+|V|}\right)^{c(w,d)}

Step 1. Vocabulary and class priors

V = {beijing, chinese, japan, macao, shanghai, tokyo}; |V|=6.

ClassDocumentsPrior
no11/4
yes33/4

Step 2. Count every training word within each class

ClassTotal tokensbeijingchinesejapanmacaoshanghaitokyo
no3011001
yes8150110

Step 3. Count the test words

WordCount
chinese3
tokyo1
japan1

OOV words: none

Step 4. Calculate likelihoods and multiply with the prior

ClassPriorIndividual factorsExact scoreDecimal score
no1/4chinese: (1+1)/(3+1×6) = 2/9, used 3 time(s); tokyo: (1+1)/(3+1×6) = 2/9, used 1 time(s); japan: (1+1)/(3+1×6) = 2/9, used 1 time(s)8/590490.000135480702467
yes3/4chinese: (5+1)/(8+1×6) = 3/7, used 3 time(s); tokyo: (0+1)/(8+1×6) = 1/14, used 1 time(s); japan: (0+1)/(8+1×6) = 1/14, used 1 time(s)81/2689120.000301213779973

Step 5. Compare and optionally normalize

Normalized scores: no = 0.310241388, yes = 0.689758612

Division by the same total does not change the winning class.

Final answer

Predicted class: yes.

Yaad rakho: Count → prior → smoothed word chances → repeated factors → compare.