Implement Naive Bayes classifier from scratch

Quick Overview

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 Naive Bayes classifier from scratch

Company: Pinterest

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: hard

Interview Round: Take-home Project

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

Quick Answer: This question evaluates understanding of probabilistic classification, specifically competence in estimating class priors, feature likelihood parameters, and computing posterior scores for Naive Bayes variants.

|Home/Machine Learning/Pinterest
Pinterest logo
Pinterest
Feb 9, 2026, 12:00 AM
hardMachine Learning EngineerTake-home ProjectMachine Learning
6
0

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)P(y=c)
  • likelihood parameters for P(xy=c)P(x\mid y=c)
  • posterior scoring using Bayes’ rule with numerical stability (typically log-probabilities).
Loading comments...