MoonSay /Tutorials/Machine Learning Interview #16
Contents
  1. 01Introduction
  2. 02Bayes’ rule, part by part
  3. 03A generative classifier
  4. 04The naive assumption
  5. 05Every pixel votes
  6. 06Why it still works
  7. 07Interview follow-ups
  8. 08Common misconceptions
  9. 09Interview questions and model answers
  10. 10Summary
  11. References
Machine Learning Interview #16 · Tutorial

Naive BayesWhy a False Assumption Still Works

Bayes’ rule, one bold assumption, and why the decisions survive it

A tutorial companion to the video Machine Learning Interview #16: Naive Bayes: Why a False Assumption Still Works PDF

01Introduction

1.1The question

Some of the first spam filters that learned from examples ran on a very simple classifier, naive Bayes (Sahami et al., 1998): Bayes’ rule plus one assumption that everybody knows is false. It is still a standard baseline for text and a favorite interview topic, because a good answer touches the core of probabilistic machine learning. Interviewers ask three things: what is naive Bayes, why is it called naive, and why does it work anyway? This tutorial answers all three, and derives every step on the way.

1.2Prerequisites and notation

Prerequisites. Basic probability (conditional probability, independence) and logarithms. Maximum likelihood (Episode 5), generative and discriminative models (Episode 6) and logistic regression (Episode 15) help but are explained where they are used.

Notation. The class is c\in\{0,1,\dots,9\}, the digit. An image is a vector of D=64 pixels, x=(x_1,\dots,x_D), with x_d=1 if pixel d is on and 0 if it is off; pixels are named by row and column, counted from 1 at the top left. We have N training images; N_c of them show digit c, and N_{dc} of those have pixel d on. The model’s parameters are the prior \pi_c=P(c) and the pixel probabilities \theta_{dc}=P(x_d=1\mid c); a hat, as in \hat\theta_{dc}, marks a value estimated from the training images. We write \log for the natural logarithm.

1.3A short history

The rule is named after Thomas Bayes, whose essay on inverse probability was published by Richard Price in 1763, two years after Bayes’s death (Bayes and Price, 1763); Pierre-Simon Laplace developed it into a general method of inference, and his rule of succession, which adds one to each count, is the smoothing we use in Chapter 4 (Laplace, 1814). As a classifier with the independence assumption, the method appeared early in document classification: Maron (1961) sorted documents into subject categories from the words they contain. Sahami et al. (1998) applied it to junk e-mail, the model behind many early learned spam filters. Why such a simple model works so well was explained by Domingos and Pazzani (1997), and reviewed by Hand and Yu (2001) under its nickname, “idiot’s Bayes”. Textbook treatments are Murphy (2022, §9.3), Hastie et al. (2009, §6.6.3) and, for text, Manning et al. (2008, Ch. 13).

All examples use one real data set, the optical handwritten digits of Alpaydin and Kaynak (1998): 1{,}797 digits written by 13 people, each scanned and reduced to an 8\times8 grid in which every cell counts the inked pixels of a small block, from 0 to 16. A pixel is on if its block is at least half inked (a count of 8 or more). We train on 1{,}257 digits and test on the other 540. All numbers in this tutorial were computed for this tutorial, with the same data as the companion video.