Implement Naive Bayes classifier from scratch
PinterestMachine Learning Engineer · Online AssessmentLast updated
This question evaluates understanding of probabilistic classification, specifically competence in estimating class priors, feature likelihood parameters, and computing posterior scores for Naive Bayes variants.
Implement a Naive Bayes classifier from scratch (you may use NumPy).
Write a class with:
fit(X, y): estimate class priors and feature likelihood parameters.predict(X): compute posteriors (or unnormalized log-posteriors) and output the most likely class.
Specify which variant you implement (e.g., Multinomial Naive Bayes for count features, Bernoulli NB for binary features, or Gaussian NB for continuous features). Your implementation should clearly compute:
- prior (P(y=c))
- likelihood parameters for (P(x\mid y=c))
- posterior scoring using Bayes’ rule with numerical stability (typically log-probabilities).
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.