liveBook · Manning

Naive bayes classifier

As explained in Grokking Machine Learning

A probabilistic classification model that estimates class probabilities from observed features using the naive Bayes assumption.

Suppose an email contains the word “lottery.” Is it spam? A naive Bayes classifier estimates the probability of a label, such as spam, from observed features, such as words. It starts with the probability of spam before reading the email—the prior—and updates that estimate using what the words tell us. The updated probability is the posterior. section 8.0 section 8.2

One word changes the estimate

Imagine 100 emails: 20 are spam, so the prior probability of spam is 20%. Of the 20 emails containing “lottery,” 15 are spam. Once you know an email contains that word, the estimated probability of spam becomes 15 ÷ 20 = 75%. The relevant comparison is now among emails containing “lottery,” not among all emails. section 8.2

What if the email contains both “lottery” and “sale”? Naive Bayes makes its simplifying assumption: within each class, treat the observed words as independent. Estimate the chance of seeing both words in spam by multiplying their separate chances in spam; do the same for ham. Multiply each result by that class’s prior, then divide the spam result by the sum of both results to obtain the spam probability. For words x₁ through xₙ, the assumption is P(x₁, …, xₙ | spam) = P(x₁ | spam) × … × P(xₙ | spam). It makes a many-word calculation manageable, but it remains an assumption about the words. section 8.2

Figure 8.11 Say 20% of the emails contain the word lottery, and 10% of the emails contain the word sale. We make the naive assumption that these two words are independent of each other. Under this assumption, the percentage of emails that contain both words can be estimated as 2%, namely, the product of 20% and 10%.

The diagram illustrates the simplifying independence assumption by estimating how often two words occur together from their separate frequencies. section 8.2

To build a word-based spam classifier by hand:

  • Load emails labeled spam or ham. Lowercase each email and keep a set of its words, so repeated occurrences count only once. section 8.3
  • Count spam and ham emails to find the priors. For each known word, count the spam and ham emails containing it; the Python example starts each word’s counts at one. section 8.3
  • For a new email, combine its known words’ class-specific estimates with the priors, then turn the two results into a spam probability. The example prediction function skips words absent from its stored counts. section 8.3

Try a prediction

A hand-coded classifier gives “buy cheap lottery easy money now” a spam probability of about 0.99997, while an unfamiliar word by itself returns the prior, about 0.239. For another hands-on test, use symptoms such as cough and fever to estimate whether a patient is sick; an unmentioned symptom is unknown, not automatically absent. section 8.3 section 8.5

A probability is useful for sorting likely spam, but predictions still need evaluation. One workflow trains Scikit-Learn’s GaussianNB alongside other classifiers, chooses among models using validation data and metrics such as accuracy and F1-score, and reserves the test set until the end. section 8.2 section 13.4

How Grokking Machine Learning covers it

How deep it goes
Works from priors and conditional probabilities through the independence calculation and a hand-coded predictor, then shows library-based model training.
Examples
Uses lottery-bearing emails, a labeled email dataset with sample predictions, and a symptom-based diagnosis exercise.
What it assumes
Introduces conditional probability and Bayes’ theorem before the classifier, and introduces Pandas when loading the email dataset.
What you can do after
Build it from scratch in Python for spam prediction, or apply Scikit-Learn’s GaussianNB and evaluate it on validation data.
How it connects
Connects Bayes’ theorem, conditional probability and feature independence to spam detection and classification-model evaluation.

Also explained in

Compare how 5 books explain naive bayes classifier →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Machine Learning. The section links point to the parts of the book it draws on.