Skip to content
DeepForgeby svx
All problems
EasyProbabilitypr-292

Bernoulli Naive Bayes Decision

Problem

Score each class with the Bernoulli naive Bayes log-posterior log(prior) plus the sum of feature log-probabilities, and return the index of the highest-scoring class, breaking ties toward the earlier class. priors is per class and feature_probs[c][i] is the probability that feature i equals 1 under class c. Return -1 for invalid input or a nonpositive prior.

Hint

Compute every class score, then take the argmax with the earlier class winning ties.

Test cases

case 1
in: [[1,1],[0.5,0.5],[[0.9,0.8],[0.4,0.3]]]
expected: 0
case 2
in: [[0,1],[0.5,0.5],[[0.9,0.8],[0.4,0.3]]]
expected: 1
case 3
in: [[0,0],[0.5,0.5],[[0.5,0.5],[0.5,0.5]]]
expected: 0
case 4
in: [[1],[0.3,0.7],[[0.9],[0.2]]]
expected: 0
case 5
in: [[1],[],[]]
expected: -1
Starter codePython
import math


def bernoulli_naive_bayes_decision(x, priors, feature_probs):
    # Your code here
    pass

5 test cases · run your code against them in the editor