Natural Language Processing · TEE · papers

Q3b · Naive Bayes text classifier

2023-01-30 · re-exam · recovered AI paper

Previous examination · complete worked answers

Question. Explain Naive Bayes text classification with a suitable example. [10 marks]

Answer

The paper supplies no numerical training corpus for this part. The following clearly labelled illustrative corpus demonstrates the complete calculation.

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.

Source / convention note. Chinese corpus is an authored/reused teaching illustration here; it is actual given data in F22 Q5b, not this prompt.