Bernoulli Naive Bayes

medium
Unit 17 · Data Splitting0/3 to milestone

For binary features (word present / absent), Bernoulli naive Bayes models each feature as a coin flip per class, and an absent word counts as evidence too.

Implement BernoulliNB(alpha=1.0) with:

  • fit(X, y): class log-priors and per-class feature probabilities pcj=Ncj+αNc+2αp_{cj} = \frac{N_{cj} + \alpha}{N_c + 2\alpha}
  • predict_log_proba_unnormalized(X): log⁡P(c)+∑jxjlog⁡pcj+(1−xj)log⁡(1−pcj)\log P(c) + \sum_j x_j \log p_{cj} + (1-x_j)\log(1-p_{cj}) for each class (shape (n, n_classes), classes in sorted order)
  • predict(X): the argmax class label
#naive-bayes#classification#text2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Bernoulli Naive Bayes · TechGradify