Naive Bayes Text Classification: Classify a Message Word by Word

Classify `free project meeting` with multinomial Naive Bayes. The worked example builds priors and smoothed likelihoods, then verifies the 75% Study result through raw scores, odds, and natural logs.

KnowledgeGate Team

Exam prep & CS education

Updated 22 Nov 20265 min read
Naive Bayes Text Classification: Classify a Message Word by Word

Bayes' rule is manageable for one event, but text classification seems to need a new probability for every sentence. Multinomial Naive Bayes simplifies the task by breaking a message into word evidence, estimating each word inside each class, and comparing class scores. In the six-message corpus below, free project meeting is classified as Study with a 75% posterior. Raw probabilities, odds, and log scores all reach the same decision.

What Naive Bayes compares when a message arrives

Assign document d to c in {Promo, Study}. For words w_1, ..., w_n, multinomial Naive Bayes compares:

Score(c) = P(c) product_i P(w_i | c)

Here, P(c) is the class prior, P(w_i | c) is a word likelihood, and the larger score wins. The evidence P(d) is shared by both classes for a fixed message, so comparison can omit it. Normalisation restores it for the posterior P(c | d).

Do not reverse P(word | class) into P(class | word). The naive assumption makes word occurrences conditionally independent once the class is known. Bayes' rule itself is not naive. Probability for GATE CS reviews the probability identities. Conditional Probability for Machine Learning derives classifier precision from prevalence, sensitivity, and false-positive rate. Multinomial Naive Bayes instead compares token counts with Laplace smoothing, repeated-word factors, and log-space scores.

Turn six training messages into priors and token counts

The corpus contains three Promo and three Study messages, each with three tokens. Its values are hypothetical, so the resulting probabilities describe only this corpus.

ID

Class

Training message

P1

Promo

free offer now

P2

Promo

free prize offer

P3

Promo

claim free prize

S1

Study

project meeting now

S2

Study

project review meeting

S3

Study

submit project review

Preprocessing lowercases, removes surrounding punctuation, splits on whitespace, retains repeats, and performs no stemming or stop-word removal. The fixed training vocabulary is {free, offer, now, prize, claim, project, meeting, review, submit}, so |V| = 9. Each class has three documents and nine tokens.

Document counts give P(Promo) = 3/6 = 1/2 and P(Study) = 3/6 = 1/2. Likelihoods instead use class token counts. These equal priors belong to this corpus, not Naive Bayes generally.

Apply Laplace smoothing before any word can erase a class

P(w | c) = [count(w,c) + alpha] / [N_c + alpha|V|]

With alpha = 1, both classes have N_c = 9; with |V| = 9, each denominator is 9 + 1 x 9 = 18.

Word

Promo count

Study count

P(word | Promo)

P(word | Study)

free

3

0

4/18

1/18

offer

2

0

3/18

1/18

now

1

1

2/18

2/18

prize

2

0

3/18

1/18

claim

1

0

2/18

1/18

project

0

3

1/18

4/18

meeting

0

2

1/18

3/18

review

0

2

1/18

3/18

submit

0

1

1/18

2/18

Without smoothing, project and meeting zero Promo, while free zeros Study, giving 0 versus 0. A pseudocount preserves preferences without making unseen pairs impossible. These are corpus fractions, not real-world rates.

Classify free project meeting from start to finish

Each line contains the prior, then likelihoods for free, project, and meeting in order:

Score(Promo) = (1/2)(4/18)(1/18)(1/18) = 1/2916 = 0.000342935...

Score(Study) = (1/2)(1/18)(4/18)(3/18) = 1/972 = 0.001028806...

Since 1/972 is three times 1/2916, predict Study. These raw joint scores are not posterior percentages.

The evidence is 1/2916 + 1/972 = 4/2916. Thus P(Promo | d) = (1/2916)/(4/2916) = 1/4 = 25% and P(Study | d) = (1/972)/(4/2916) = 3/4 = 75%. These values apply only to this corpus, vocabulary, smoothing, and two-class model.

Class scores for Promo and Study on “free project meeting,” with Study reaching 75%.

Read the same decision as a word-by-word odds update

In Study:Promo odds, common factors cancel. Start at (1/2)/(1/2) = 1, or 1:1. For free, multiply by (1/18)/(4/18) = 1/4, giving 1:4. For project, multiply by (4/18)/(1/18) = 4, returning to 1:1. For meeting, multiply by (3/18)/(1/18) = 3, finishing at 3:1.

Thus Study = 3/(3+1) = 75% and Promo = 1/(3+1) = 25%. One Promo-leaning word is neutralised, then outweighed. Equal class token totals let the 18 denominators cancel here. General calculations must retain unequal denominators.

Study:Promo odds update word by word to 3:1, yielding 75% Study and 25% Promo.

Understand what the independence assumption buys and misses

The model treats occurrences of free, project, and meeting as independent once the class is known. Real language violates this because order, phrases, and context interact. The assumption still turns a huge sentence model into counts estimable from modest labelled data, but does not ensure accuracy or calibration.

Multinomial Naive Bayes counts occurrences. In free free project meeting, the extra free multiplies the previous Study:Promo odds 3:1 by 1/4. The result is 3:4, so Promo wins with 4/7 = 57.1%. Bernoulli Naive Bayes models presence, so a repeat adds no factor.

Build the vocabulary from training data only, then reuse the same tokenisation at inference. Adding message words to the vocabulary leaks inference input and changes every smoothed denominator.

Avoid arithmetic traps and verify the classifier in log space

Wrong move

What breaks

Correct move

Reverse P(word | class)

Changes the conditional

Keep likelihoods inside each class

Use tokens for priors

Confuses documents and tokens

Use 3/6 documents

Use N_c + alpha

Wrong denominator

Use N_c + alpha|V| = 9 + 1 x 9 = 18

Omit smoothing

Produces 0 versus 0

Add one pseudocount

Count a repeat once

Changes the multinomial model

Count all 9 class-token occurrences

Add message words to vocabulary

Leaks inference input

Freeze training |V|= 9

Natural logarithms avoid underflow:

log Score(Promo) = log(1/2) + log(4/18) + log(1/18) + log(1/18) = -7.97797...

log Score(Study) = log(1/2) + log(1/18) + log(4/18) + log(3/18) = -6.87936...

Study's less negative score wins. The difference, 1.09861... = log(3), matches 3:1 odds.

Code
count documents, vocabulary, class tokens
set alpha = 1
for each class:
    score = log(document prior)
    for each message token:
        score += log((token count + alpha) / (class tokens + alpha * vocabulary size))
predict argmax(score)

Use the Python Course: Concepts, MCQs & Coding to practise the loops, dictionaries, and logarithms needed to implement this pseudocode.

How assessments test Naive Bayes, then the short version

Four assessment checks are: priors 1/2 and 1/2 from document counts, smoothed denominator 18, unsmoothed scoring of 0 versus 0, and the Study prediction with raw-score ratio 3:1 and posterior 75%. If only the priors change to P(Promo)=0.8 and P(Study)=0.2, final Study:Promo odds become 3:4, so Promo wins with 4/7 = 57.1%.

The recall chain is: choose classes, estimate document priors, count class tokens, smooth every vocabulary likelihood, add log scores or multiply probabilities, then compare and normalise. Rebuild 1:1 -> 1:4 -> 1:1 -> 3:1 without looking back.

For a broader learning path, continue with AI & ML for Placements. Use Skill Development Courses for an implementation-focused route. Then rebuild the full chain from preprocessing and priors through smoothing, scoring, and normalisation without looking back.