Bernoulli Naive Bayes Classifier
Probability Theory DS practice problem on Onlearn.
Difficulty: medium.
Topics: Understanding Bernoulli Naive Bayes Classifier, Laplace Smoothing, Log-Likelihood, Bernoulli Distribution, Prior Probability, Posterior Estimation, Probability & Statistics, Supervised Learning, Numerical Computing, Information Theory, Algorithm Design, Bayesian Inference, Generative Modeling, Vectorized Operations, Smoothing Techniques, Classification Metrics.
Write a Python class to implement the Bernoulli Naive Bayes classifier for binary (0/1) feature data. Your class should have two methods: forward(self, X, y) to train on the input data (X: 2D NumPy array of binary features, y: 1D NumPy array of class labels) and predict(self, X) to output predicted labels for a 2D test matrix X. Use Laplace smoothing (parameter: smoothing=1.0). Return predictions as a NumPy array. Only use NumPy. Predictions must be binary (0 or 1) and you must handle cases where the training data contains only one class. All log/likelihood calculations should use log probabilities for numerical stability.