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
Naive Bayes/Chapter 05 of 10

05Every pixel votes

We have the ten probability pictures. To classify a new image, compute the posterior of every digit with (4), and pick the highest.

5.1Drop the evidence, take logs

Picking the class with the highest posterior minimizes the chance of a mistake. Two simplifications make it cheap. The evidence at the bottom of Bayes’ rule is the same for every digit, so it cannot change which one is highest, and we drop it. And a product of 64 probabilities is tiny (for text, with thousands of words, it underflows to zero in floating point), so we take logs, which also cannot change the winner because the logarithm is increasing (Manning et al., 2008, §13.2):

\hat c=\operatorname*{arg\,max}_c\;\underbrace{\log\pi_c+\sum_{d=1}^{D}\Bigl[x_d\log\theta_{dc}+(1-x_d)\log(1-\theta_{dc})\Bigr]}_{\text{score}_c(x)}. \tag{11}

Each pixel adds one term to each digit’s score: \log\theta_{dc} if it is on, \log(1-\theta_{dc}) if it is off. Every pixel casts a vote.

Between two digits a and b, the evidence cancels exactly, and the difference of the scores is the log of the posterior odds:

\log\frac{P(a\mid x)}{P(b\mid x)}=\text{score}_a(x)-\text{score}_b(x)=\log\frac{\pi_a}{\pi_b}+\sum_{d=1}^{D}v_d,\qquad v_d=x_d\log\frac{\theta_{da}}{\theta_{db}}+(1-x_d)\log\frac{1-\theta_{da}}{1-\theta_{db}} . \tag{12}

A positive vote v_d favors a, a negative one favors b.

Every pixel votes. (a) A test digit. (b) The vote v_d of each pixel in (12) between the two highest digits: blue for three, orange for nine. (c) The scores (11) of the ten digits; three beats nine by 7.5. Computed for this tutorial, with the same data as the companion video.
Figure 5. Every pixel votes. (a) A test digit. (b) The vote v_d of each pixel in (12) between the two highest digits: blue for three, orange for nine. (c) The scores (11) of the ten digits; three beats nine by 7.5. Computed for this tutorial, with the same data as the companion video.
Try itClassify any test digit

Pick any of the 540 test digits. Left: the image and the vote of every pixel between the two digits in question (blue for the first, orange for the second). Right: the scores of all ten digits, the log prior plus the sum of the votes, as in Figure 5.

Worked example 3 (Three or nine?). For the test digit of Figure 5, the highest score is the three’s, -20.17, and the runner-up is the nine’s, -27.65. Three wins by 7.49 in log odds, so its posterior odds against nine are e^{7.49}\approx1{,}800 to 1, and its posterior is 99.93\%. The priors contribute almost nothing (\log(\pi_3/\pi_9)=0.016); the pixels decide. On all 540 test digits, naive Bayes gets 89.6\% right; logistic regression, trained on the same pixels, gets 93.0\%.

5.2A linear classifier

Proposition 3 (Naive Bayes is a linear classifier, Murphy, 2022, eq. 9.61–9.62). The score (11) is a linear function of the pixels,

\text{score}_c(x)=w_c^\top x+b_c,\qquad w_{dc}=\log\frac{\theta_{dc}}{1-\theta_{dc}},\qquad b_c=\log\pi_c+\sum_{d=1}^{D}\log(1-\theta_{dc}), \tag{13}

and the posterior is the softmax of these linear scores, P(c\mid x)=e^{w_c^\top x+b_c}\big/\sum_{c'}e^{w_{c'}^\top x+b_{c'}}.

Try itStep through the derivation: from Bayes' rule to a linear score

Press Next step to go from "pick the highest posterior" to a linear classifier, one line at a time. Blue marks what changes in each step.

For one pixel, x_d\log\theta_{dc}+(1-x_d)\log(1-\theta_{dc})=\log(1-\theta_{dc})+x_d\bigl[\log\theta_{dc}-\log(1-\theta_{dc})\bigr]: a constant plus the pixel times a fixed weight, the log-odds that the pixel is on in digit c. Sum over the pixels and add \log\pi_c. For the posterior, put the evidence back: P(c\mid x)=P(x\mid c)P(c)/\sum_{c'}P(x\mid c')P(c'), and P(x\mid c)P(c)=e^{\text{score}_c(x)}.

So each vote is a fixed weight times a pixel, and the boundary between two digits, where their scores are equal, is the flat surface (w_a-w_b)^\top x+(b_a-b_b)=0. This is exactly the form of multinomial logistic regression (Episode 15). The difference is how the weights are chosen: naive Bayes sets each weight from its own pixel’s counts, maximizing the joint likelihood \prod_nP(y_n,x_n), while logistic regression chooses all the weights together to maximize the conditional likelihood \prod_nP(y_n\mid x_n) (Murphy, 2022, §9.3.4).

Pause and think. Both models are linear in the same pixels, yet logistic regression is more accurate here. What can it do that counting cannot?

Show the answer

It chooses the weights jointly. If two pixels almost always switch on together, logistic regression can share the credit between them, giving each about half the weight. Naive Bayes sets each weight from its own pixel as if the other did not exist, so the pair gets full weight twice. The next chapter shows what that does.

Not bad for a false assumption. But is the assumption really false? And why does it still work?