Contents
09Interview questions and model answers
Q1What is naive Bayes, and why is it called naive?
A generative classifier: it models the prior P(c) and the likelihood P(x\mid c), and classifies with Bayes’ rule, picking the class with the highest posterior. It is naive because it assumes the features are independent given the class, P(x\mid c)=\prod_dP(x_d\mid c), which real features rarely are.
Q2Derive Bayes’ rule and name its parts.
The product rule gives P(c,x)=P(c\mid x)P(x)=P(x\mid c)P(c); divide by P(x). Prior P(c): belief before looking. Likelihood P(x\mid c): how likely the data are under class c. Evidence P(x)=\sum_{c'}P(x\mid c')P(c'): the same for every class, it normalizes. Posterior P(c\mid x): belief after looking.
Q3Is naive Bayes generative or discriminative? What is the difference?
Generative: it learns how each class generates its features and flips that with Bayes’ rule. A discriminative model such as logistic regression learns P(c\mid x) directly. Generative models are easy to fit and handle missing features; discriminative ones are usually more accurate.
Q4How do you train it?
By counting. For binary features the maximum likelihood estimates are \hat\theta_{dc}=N_{dc}/N_c, the share of class-c examples with feature d on, and \hat\pi_c=N_c/N; each parameter is estimated separately, in one pass, with no optimization.
Q5What is the zero-frequency problem, and how do you fix it?
A feature never seen with a class gets probability 0, which makes the whole product 0 and vetoes the class whatever the other features say. Laplace (add-one) smoothing, (N_{dc}+1)/(N_c+2), keeps every probability above zero; it is the estimate under a uniform prior.
Q6Why work in log space, and what does the classifier look like there?
A product of many probabilities underflows; the log turns it into a sum and keeps the winner. The score \log\pi_c+\sum_d\log P(x_d\mid c) is linear in the features, w_c^\top x+b_c with w_{dc}=\log\frac{\theta_{dc}}{1-\theta_{dc}}, so naive Bayes is a linear classifier, the same form as logistic regression.
Q7Why does naive Bayes work when its assumption is false?
Dependent features are counted twice, which stretches the scores but rarely changes which class is highest, and a decision only needs the highest (Domingos and Pazzani, 1997). It also has few parameters, so low variance. Copying every pixel of our digits leaves every answer unchanged.
Q8Can you trust its probabilities?
No: double counting pushes them toward 0 and 1. On our digits, when it says 90 to 99\% it is right 80\% of the time. Use its decisions or its ranking, and calibrate the probabilities (Platt scaling, isotonic regression) on held-out data if you need them.
Q9How do you handle continuous features?
Gaussian naive Bayes: a mean and a variance per feature and class, estimated from that class’s examples. Or discretize the feature, or use another one-dimensional density; the shape of each factor is part of the assumption.
Q10Naive Bayes or logistic regression?
Naive Bayes: one counting pass, needs fewer examples to reach its best, easy to update. Logistic regression: fits the weights jointly, accounts for dependent features, usually more accurate with enough data (Ng and Jordan, 2002). With very little data or as a fast baseline, naive Bayes; otherwise logistic regression.