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 07 of 10

07Interview follow-ups

7.1Why do we call naive Bayes naive?

Because it assumes that, given the class, all the features are independent (Murphy, 2022, §9.3). That oversimplifies the real world, where features usually depend on each other, like the pixels along one stroke, or words that appear together in an e-mail (“credit” and “card”). Note what the assumption does not say: it does not say the pixels are independent overall. Overall they are strongly dependent, because one pixel tells you something about the digit, which tells you about the others. The assumption is only independence once the class is known, and even that is false.

7.2What about continuous features?

Give each feature a bell curve per class: Gaussian naive Bayes uses P(x_d\mid c)=\mathcal N(x_d\mid\mu_{dc},\sigma^2_{dc}), and maximum likelihood is again simple, the mean and variance of feature d among the training examples of class c (Murphy, 2022, eq. 9.54–9.55). It is Gaussian discriminant analysis with a diagonal covariance matrix. Because each class has its own variances, the boundary is in general curved (quadratic); with variances shared across classes it is linear again.

(a) Gaussian naive Bayes on the raw ink counts: the counts of one pixel (row 3, column 4) in the training zeros and sevens, with the bell curve fitted to each. (b) Test accuracy against the number of training digits for naive Bayes and logistic regression (average over 20 random training sets per size). Computed for this tutorial, with the same data as the companion video.
Figure 7. (a) Gaussian naive Bayes on the raw ink counts: the counts of one pixel (row 3, column 4) in the training zeros and sevens, with the bell curve fitted to each. (b) Test accuracy against the number of training digits for naive Bayes and logistic regression (average over 20 random training sets per size). Computed for this tutorial, with the same data as the companion video.

The choice of each factor is part of the assumption. On our digits, the raw counts run from 0 to 16 and pile up at 0, so a bell curve fits them poorly (Figure 7a), and Gaussian naive Bayes on the raw counts gets 84.8\% of the test digits right, below the 89.6\% of the on/off model. For word counts in text, the standard choice is the multinomial model, in which each occurrence of a word votes (Manning et al., 2008, Ch. 13); that is the model of the classic spam filters (Sahami et al., 1998).

7.3Naive Bayes or logistic regression?

They are the same kind of model, linear in the features (Proposition 3), fitted in two ways. Naive Bayes trains in one counting pass and handles missing features easily (Murphy, 2022, §9.4.2); logistic regression fits the weights together, corrects for dependent features and usually ends more accurate (Murphy, 2022, §9.4.1). Ng and Jordan (2002) made the trade-off precise: naive Bayes approaches its best accuracy with far fewer examples (growing with the logarithm of the number of features, against linearly for logistic regression), but logistic regression is better in the limit, so on many data sets naive Bayes leads with few examples and logistic regression overtakes it. On our digits logistic regression leads at every size we tried (Figure 7b): the crossover is common, not guaranteed.