Foundations to Frontier
Tests
D. Classical Machine Learning › D3 Probabilistic and Bayesian Classifiers

Multinomial Naive Bayes

Multinomial naive Bayes models a document as a bag of words drawn from a class-specific word distribution. Training is counting words per class; prediction sums each word's log-probability weighted by its count. With additive (Laplace or Lidstone) smoothing it is the classic text classifier: fast, data-efficient, and strong with short documents and few labels. This page covers the multinomial model, maximum-likelihood and smoothed estimates, the zero-frequency problem, log-space scoring, feature representations (counts, TF-IDF), its linear form, and how it compares with logistic regression as data grow. In the exercises you implement multinomial naive Bayes with smoothing and check it by hand; then you route spacecraft anomaly reports to subsystem teams and study how the smoothing strength and training-set size affect accuracy and calibration against softmax regression.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Which team’s words does this report use?

Anomaly reports from the propulsion team say “valve”, “leak” and “pressure”. Power reports say “battery” and “voltage”. Multinomial naive Bayes learns how often each team uses each word. For a new report, it multiplies the probabilities of its words under each team’s vocabulary and picks the team whose word habits fit best. Unseen words get a small “smoothing” probability so a single new word cannot rule a team out completely.

Route a report: multinomial naive Bayes
propulsion0.681
power0.064
comms0.255
wordlog P(word | propulsion)log P(word | power)log P(word | comms)
valve-2.56-3.91-3.91
pressure-2.85-3.91-3.91
signal-3.95-3.91-2.53
Vocabulary: 34 words from 12 training reports. Each word adds its log-probability; the class with the largest total wins. Set α = 0 and type "valve voltage": each class has never seen one of the words, so every class gets probability zero (the demo then shows 1/3 each): the zero-frequency problem that smoothing fixes. Large α flattens all word probabilities toward 1/V.
Why engineers care

Operations generate floods of text: anomaly reports, logs, operator notes, maintenance tickets. Multinomial naive Bayes routes and tags them with a few hundred labelled examples, trains in milliseconds, and explains itself: you can list the words that drove each decision.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Multinomial naive Bayes represents a document by…
  2. 2. Smoothing gives unseen words…
  3. 3. In the demo with α = 0, 'valve voltage' gets…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Counting by hand

The vocabulary is valve, leak, battery, voltage. The training word counts are:

  • propulsion: (3, 3, 0, 0), 6 words in total;
  • power: (0, 1, 4, 3), 8 words.

With Laplace smoothing (α = 1, V = 4), θ = (count + 1)/(total + 4):

| Word | P(word | propulsion) | P(word | power) | |—|—|—|—| | valve | 4/10 = 0.4 | 1/12 | | leak | 4/10 = 0.4 | 2/12 | | battery | 1/10 = 0.1 | 5/12 | | voltage | 1/10 = 0.1 | 4/12 |

New report “leak voltage”, with equal priors:

  • propulsion: 0.4 × 0.1 = 0.040;
  • power: (2/12)(4/12) = 0.056;
  • P(propulsion) = 0.040/0.096 = 0.419.

Without smoothing, propulsion would give “voltage” probability 0, and power would give “valve” probability 0. Any report containing both words would then be impossible under both classes.

The steps in plain English:

  1. Turn every document into a vector of word counts over a fixed vocabulary.
  2. For each class, add up the counts of each word over its training documents.
  3. Smooth: θₖⱼ = (countₖⱼ + α)/(totalₖ + αV).
  4. Score a new document: log πₖ + Σⱼ xⱼ log θₖⱼ.
  5. Normalise with log-sum-exp and pick the best class. Tune α by validation.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Laplace-smoothed probability of an unseen word, total 6, V = 4?
  2. 2. A word appearing 3 times in a document contributes…
  3. 3. Smoothed word probabilities for a class sum to…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

The model

Generative story: pick class k with probability πₖ, then draw L words independently from Multinomial(θₖ). The likelihood is p(x | k) ∝ ∏ⱼ θₖⱼ^xⱼ. The multinomial coefficient L!/∏xⱼ! is the same for every class and can be dropped.

Estimates:

  • maximum likelihood: θ̂ₖⱼ = nₖⱼ/nₖ;
  • additive (Lidstone) smoothing: (nₖⱼ + α)/(nₖ + αV), the posterior mean under a symmetric Dirichlet(α) prior. α = 1 is Laplace smoothing; α below 1 is Lidstone smoothing.

Linear classifier. The score is log πₖ + xᵀ log θₖ, linear in the counts. Multinomial NB is therefore a linear classifier whose weights are log word probabilities, estimated generatively rather than discriminatively.

Representations:

  • raw counts;
  • binary presence (closer to Bernoulli NB, D3.33);
  • TF-IDF weights, which work well in practice even though they break the count semantics;
  • length normalisation.

Complement NB (D3.34) fixes some biases with imbalanced classes.

from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import make_pipeline
clf = make_pipeline(CountVectorizer(ngram_range=(1, 2), min_df=2), MultinomialNB(alpha=0.1)).fit(texts, labels)
clf.predict_proba(new_texts)
top = clf[-1].feature_log_prob_.argsort(1)[:, -10:]          # most characteristic words per class

Use it when classifying text or other count data with little labelled data, as a fast baseline, or for streaming updates (counts just add). Prefer logistic regression or linear SVMs on TF-IDF with ample data. Transformers are much stronger when large models are allowed.

🐍 Exercise 1: multinomial naive Bayes
Implement multinomial naive Bayes with additive smoothing, joint log-likelihoods, posteriors and predictions; check the smoothed word probabilities and the 'leak voltage' posterior against the hand computation, show the zero-frequency problem without smoothing, and classify synthetic reports.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Additive smoothing is the posterior mean under a…
  2. 2. Multinomial NB's decision function is…
  3. 3. feature_log_prob_ in scikit-learn holds…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Theory and practice

Smoothing as a bias–variance knob.

  • Small α trusts the counts: low bias, high variance, and extreme probabilities from rare words.
  • Large α pulls every θₖ toward uniform: stable, but it washes out real differences.
  • In exercise 2, α = 0.01 gave log loss 6.3 with 20 training reports (overconfident), while α = 10 dropped accuracy by 8.6 points.
  • The best α for accuracy and the best α for calibration differ, so tune α for the metric you care about.

Why it works for text.

  • Words are far from independent, but their dependencies are similar across classes, so the class ranking survives (Domingos and Pazzani).
  • The model has V(K − 1) effective parameters, each estimated from simple counts, so it learns fast.
  • Its weaknesses: burstiness (a repeated word counted many times), length effects, and imbalance (Rennie et al., 2003).

Generative versus discriminative, again. When the model is correctly specified (as in exercise 2, where documents really were multinomial draws), naive Bayes is better at every sample size. On real text, logistic regression or SVMs usually overtake NB once there are thousands of labelled documents (Ng and Jordan).

Semi-supervised NB. EM can use unlabelled documents by treating their classes as latent (Nigam et al., 2000). This helps when labels are scarce.

Interview questions.

  • What is the zero-frequency problem? An unseen word gets probability 0, so the whole product becomes 0 regardless of the other evidence. Smoothing fixes it.
  • Why use logs? Long documents multiply hundreds of small probabilities, which underflow. Summing logs also exposes the linear form.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Larger α makes word distributions…
  2. 2. Burstiness means…
  3. 3. Unlabelled documents can help NB via…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Routing anomaly reports

History.

  • Maron (1961) used naive Bayes for automatic document indexing.
  • McCallum and Nigam (1998) compared the multinomial and Bernoulli event models for text and found the multinomial better with large vocabularies.
  • Sahami et al. (1998) and Graham’s “A plan for spam” (2002) made naive Bayes the first widely deployed spam filter.
  • Rennie et al. (2003) analysed its biases and proposed fixes (complement NB, TF transforms).

Exercise 1. The smoothed probabilities match the hand table, and P(propulsion | “leak voltage”) = 0.419. Unsmoothed, an unseen word has log-probability −∞. On synthetic three-class reports (300-word vocabulary), test accuracy is 1.000. That task is easy; exercise 2 makes it hard.

Exercise 2: routing reports to four teams. Setup: about 15 words per report, 85% shared background vocabulary, V = 400, 4 repetitions. Each cell shows test accuracy / log loss.

Training reports MNB α = 0.01 α = 0.1 α = 1 α = 10 Softmax regression
20 0.406 / 6.31 0.408 / 3.64 0.383 / 1.92 0.322 / 1.39 0.380 / 1.38
100 0.592 / 2.99 0.609 / 1.83 0.600 / 1.17 0.505 / 1.16 0.543 / 1.10
1000 0.806 / 0.63 0.817 / 0.52 0.821 / 0.48 0.792 / 0.58 0.769 / 0.70
  • Naive Bayes is the most accurate at every size. The generative model here is exactly right, so it converts few reports into good decisions.
  • Small α maximises accuracy but destroys calibration with little data. With 20 reports, α = 0.01 has log loss 6.3 (random guessing is 1.39). For probabilities, use α ≈ 1 or more, or calibrate.
  • Softmax regression is the best-calibrated model with little data, because its L2 penalty keeps scores modest, but it is less accurate.
  • With 1000 reports, α = 1 is best on both metrics: once counts are reliable, moderate smoothing costs nothing.
🐍 Exercise 2 (research): smoothing, sample size and calibration
Simulate short anomaly reports for four subsystem teams from overlapping word distributions; compare multinomial naive Bayes with α = 0.01, 0.1, 1 and 10 and L2-penalised softmax regression on counts, for 20, 100 and 1000 training reports, by test accuracy and log loss.

Multinomial naive Bayes in AI, robotics and aerospace

  • Anomaly-report and ticket routing to the right engineering team.
  • Log classification: tagging event logs by subsystem or severity.
  • Spam and phishing filtering of operations email.
  • Baselines for NLP pipelines before fine-tuning large language models.

References

  1. A. McCallum, K. Nigam (1998). “A comparison of event models for naive Bayes text classification.” AAAI Workshop on Learning for Text Categorization.
  2. J. D. M. Rennie, L. Shih, J. Teevan, D. R. Karger (2003). “Tackling the poor assumptions of naive Bayes text classifiers.” ICML.
  3. K. Nigam, A. McCallum, S. Thrun, T. Mitchell (2000). “Text classification from labeled and unlabeled documents using EM.” Machine Learning 39(2–3).
  4. M. E. Maron (1961). “Automatic indexing: an experimental inquiry.” Journal of the ACM 8(3).
  5. C. D. Manning, P. Raghavan, H. Schütze (2008). Introduction to Information Retrieval, chapter 13. Cambridge University Press.