Naive Bayes Log-Probability

Medium
~20 min
code completion

Naive Bayes

Naive Bayes is a probabilistic classifier that applies Bayes' theorem with the "naive" assumption that features are conditionally independent given the class:

For Gaussian Naive Bayes, .

In practice we work in log-space to avoid underflow:

Your task:

Implement naive_bayes_log_probs(x, log_priors, means, stds) that returns the unnormalized log-posterior for each class. Return a 1D array of shape (n_classes,).

Example Tests

2 classes, 1 feature: higher prior wins with equal likelihood

Input: {"x":[0],"stds":[[1],[1]],"means":[[0],[0]],"log_priors":[-0.5,-1]}

Expected: [-1.41894,-1.91894]

argmax gives correct class

Input: {"x":[5],"stds":[[1],[1]],"means":[[0],[5]],"log_priors":[0,0]}

Expected: 1

Output shape is (n_classes,)

Input: {"x":[1,2],"stds":[[1,1],[1,1],[1,1]],"means":[[1,2],[3,4],[5,6]],"log_priors":[0,0,0]}

Expected: [3]

Sign in to solve this problem

You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.