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.
| word | log 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 |
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.
- 1. Multinomial naive Bayes represents a document by…
- 2. Smoothing gives unseen words…
- 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:
- Turn every document into a vector of word counts over a fixed vocabulary.
- For each class, add up the counts of each word over its training documents.
- Smooth: θₖⱼ = (countₖⱼ + α)/(totalₖ + αV).
- Score a new document: log πₖ + Σⱼ xⱼ log θₖⱼ.
- Normalise with log-sum-exp and pick the best class. Tune α by validation.
- 1. Laplace-smoothed probability of an unseen word, total 6, V = 4?
- 2. A word appearing 3 times in a document contributes…
- 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 classUse 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.
- 1. Additive smoothing is the posterior mean under a…
- 2. Multinomial NB's decision function is…
- 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.
- 1. Larger α makes word distributions…
- 2. Burstiness means…
- 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.
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
- A. McCallum, K. Nigam (1998). “A comparison of event models for naive Bayes text classification.” AAAI Workshop on Learning for Text Categorization.
- J. D. M. Rennie, L. Shih, J. Teevan, D. R. Karger (2003). “Tackling the poor assumptions of naive Bayes text classifiers.” ICML.
- K. Nigam, A. McCallum, S. Thrun, T. Mitchell (2000). “Text classification from labeled and unlabeled documents using EM.” Machine Learning 39(2–3).
- M. E. Maron (1961). “Automatic indexing: an experimental inquiry.” Journal of the ACM 8(3).
- C. D. Manning, P. Raghavan, H. Schütze (2008). Introduction to Information Retrieval, chapter 13. Cambridge University Press.