Katkat

Implement Naive Bayes classifier from scratch

PinterestMachine Learning Engineer · Online Assessment

Last 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).