Naive Bayes

Michael BrenndoerferOctober 26, 20262 min read
Machine Learning from ScratchAll chapters
Part 7 of 15Supervised Learning Beyond Linear Models
Chapter 54Naive Bayes
Reading section 1 of 10 · Naive Bayes · 2 minSection 1 of 10 · 2 min

Section 1 of 10 · 2 min read

Naive BayesSection view · Sections in this chapter1 / 10 ↓
Reading tools Definitions, links & notes

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Notes & comments

Two measurements can carry nearly the same information. A classifier that multiplies their class-conditional likelihoods can then count that information twice: the predicted class may stay useful while its reported probability becomes too extreme. Naive Bayes makes this tradeoff explicit. It models each class with simple feature distributions and combines them using a conditional-independence assumption. For text counts, the corresponding multinomial model instead assumes independent token draws given the class and document length.

We will fit both Gaussian measurements and multinomial word counts, compare the primitive calculations with scikit-learn, and inspect a seeded example with nearly duplicated measurements. Training uses class counts and feature summaries; prediction adds one contribution per feature or observed token count. These simple operations make it possible to trace a prediction back to its inputs.

The model is generative rather than discriminative. Logistic regression, which we studied in Logistic Regression, models p(y∣x)p(y \mid \mathbf{x}) directly. Naive Bayes models p(x∣y)p(\mathbf{x} \mid y) and p(y)p(y), then combines them with Bayes' theorem. This difference matters: a generative model can marginalize unobserved features under an appropriate observation model, but neither class implemented here silently accepts missing entries. Its probability estimates still depend on its distributional assumptions.

This chapter derives the Bayes decision rule, explains the conditional independence assumption, develops the Gaussian and multinomial event models, shows how smoothing and log-sum-exp keep computations stable, and examines what happens when independence fails. We will implement Gaussian and multinomial naive Bayes from NumPy primitives, compare them with scikit-learn, and study calibration and class imbalance.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy