Naive Bayes
🎲 What is naive Bayes? Naive Bayes classifies a point by asking which class makes it most likely, using Bayes' rule. The "naive" part is one assumption: given the class, every feature is independent of the others, so the likelihood of a whole point is a product of one small distribution per feature. Training is counting and averaging; there is no loop to converge.
It is a common warm-up in from-scratch classic-ML rounds because it is short, and because the obvious version breaks on real data in a way that tests whether you know to work in log space.
The Prompt
The math fits on an index card, so the interviewer grades the engineering: log probabilities, smoothing, shapes, and how you normalize at the end.
"Implement Gaussian naive Bayes with
fitandpredict_proba. Then do a multinomial version for word counts."
Clarifying Questions
You: What do the inputs look like? I'll assume X is (n, d) floats and y holds labels 0..C-1, and
every class appears at least once in training.
Interviewer: Yes.
You: Should priors come from the class frequencies, or be uniform?
Interviewer: Class frequencies.
You: A feature can be constant within a class, which gives a variance of zero. May I add a small floor to the variances?
Interviewer: Yes. Say how you chose it.
You: For the multinomial version, the input is a count matrix, (n, V) for a vocabulary of V words? And
smoothing is add-alpha with alpha = 1 by default?
Interviewer: Right. Make alpha a parameter.
What You'll Build
GaussianNB(var_smoothing)withfit(X, y), storinglog_prior(C,),mu(C, d)andvar(C, d).joint_log_likelihood(X) -> (m, C):log p(c) + log p(x | c)for every point and class.predict_proba(X) -> (m, C)andpredict(X) -> (m,): normalized with log-sum-exp.MultinomialNB(alpha)with the same three methods, for count data.