Problems
Loading...
1 / 1

Naive Bayes Log-Likelihood (Bernoulli)

Classic ML
Hard

Implement Bernoulli Naive Bayes by computing class priors P(y) and feature likelihoods P(x|y) in log-space.

Naive Bayes is a probabilistic classifier based on Bayes' theorem with the "naive" assumption of conditional independence between features. The Bernoulli variant is designed for binary/boolean features.

Naive Bayes Formulas:

Bayes' Theorem:

P(yx)=P(xy)P(y)P(x)P(y \mid \mathbf{x}) = \frac{P(\mathbf{x} \mid y)\, P(y)}{P(\mathbf{x})}

Naive Independence Assumption:

P(xy)=i=1dP(xiy)P(\mathbf{x} \mid y) = \prod_{i=1}^{d} P(x_i \mid y)

Log-space (for numerical stability):

logP(yx)logP(y)+i=1dlogP(xiy)\log P(y \mid \mathbf{x}) \propto \log P(y) + \sum_{i=1}^{d} \log P(x_i \mid y)

Bernoulli feature likelihood:

logP(xiy)=xilogθiy+(1xi)log(1θiy)\log P(x_i \mid y) = x_i \log \theta_{iy} + (1 - x_i) \log(1 - \theta_{iy})

where θiy\theta_{iy} is computed with Laplace smoothing (α=1\alpha=1):

θiy=P(xi=1y)=count(xi=1 in class y)+1ny+2\theta_{iy} = P(x_i=1 \mid y) = \frac{\text{count}(x_i=1 \text{ in class } y) + 1}{n_y + 2}

Function Arguments

  • X_train: array-like, shape (n_train, d) - Binary training features {0,1}
  • y_train: array-like, shape (n_train,) - Training labels
  • X_test: array-like, shape (n_test, d) - Binary test features {0,1}

Returns

2D array of shape (n_test, n_classes) containing unnormalized log posteriors for each test sample and each class (sorted in ascending class order).

Loading visualization...

Examples

Input: X_train = [[1, 0], [0, 1]], y_train = [1, 0], X_test = [[1, 0]]

Output: [[-2.8904, -1.5041]]

Classes [0, 1]. Class 0 has prior 1/2, P(x1=1|0)=1/3, P(x2=0|0)=1/3. Class 1 has prior 1/2, P(x1=1|1)=2/3, P(x2=0|1)=2/3. Test point [1,0] is more likely under class 1.

Input: X_train = [[1, 0], [1, 1], [0, 0], [0, 1]], y_train = [0, 0, 1, 1], X_test = [[1, 0], [0, 1]]

Output: [[-1.674, -2.773], [-2.773, -1.674]]

Symmetric case: [1,0] is more likely class 0, [0,1] is more likely class 1. Log posteriors are mirror images.

Hint 1

Use np.unique() to find all classes and compute class priors.

Hint 2

For Bernoulli: P(xi=1y)P(x_i=1|y) = (xi=1x_i=1 in class y + α) / (class size + 2α).

Hint 3

Use np.log() for all probability computations to work in log-space.

Requirements

  • Return numpy array of shape (n_test, n_classes) with log probabilities
  • Compute P(y) from class frequencies in training data
  • Compute P(xi=1y)P(x_i=1|y) and P(xi=0y)P(x_i=0|y) for each feature and class
  • Use Laplace smoothing (α=1) to avoid zero probabilities
  • Work in log-space for numerical stability
  • Support multi-class problems

Constraints

  • n_train, n_test ≤ 1000; d ≤ 100; NumPy only
  • Time limit: 200ms; Memory: 128MB
Try Similar Problems