TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
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(y∣x)=P(x∣y) P(y)P(x)P(y \mid \mathbf{x}) = \frac{P(\mathbf{x} \mid y)\, P(y)}{P(\mathbf{x})} P(y∣x)=P(x)P(x∣y)P(y)​

Naive Independence Assumption:

P(x∣y)=∏i=1dP(xi∣y)P(\mathbf{x} \mid y) = \prod_{i=1}^{d} P(x_i \mid y) P(x∣y)=i=1∏d​P(xi​∣y)

Log-space (for numerical stability):

log⁡P(y∣x)∝log⁡P(y)+∑i=1dlog⁡P(xi∣y)\log P(y \mid \mathbf{x}) \propto \log P(y) + \sum_{i=1}^{d} \log P(x_i \mid y) logP(y∣x)∝logP(y)+i=1∑d​logP(xi​∣y)

Bernoulli feature likelihood:

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

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

θiy=P(xi=1∣y)=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} θiy​=P(xi​=1∣y)=ny​+2count(xi​=1 in class y)+1​

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=1∣y)P(x_i=1|y)P(xi​=1∣y) = (xi=1x_i=1xi​=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=1∣y)P(x_i=1|y)P(xi​=1∣y) and P(xi=0∣y)P(x_i=0|y)P(xi​=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
Gaussian Naive BayesBernoulli PmfBigram ProbabilitiesLogistic Regression TrainingGini Impurity

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: array

You must run your code first.
PrevNext

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(y∣x)=P(x∣y) P(y)P(x)P(y \mid \mathbf{x}) = \frac{P(\mathbf{x} \mid y)\, P(y)}{P(\mathbf{x})} P(y∣x)=P(x)P(x∣y)P(y)​

Naive Independence Assumption:

P(x∣y)=∏i=1dP(xi∣y)P(\mathbf{x} \mid y) = \prod_{i=1}^{d} P(x_i \mid y) P(x∣y)=i=1∏d​P(xi​∣y)

Log-space (for numerical stability):

log⁡P(y∣x)∝log⁡P(y)+∑i=1dlog⁡P(xi∣y)\log P(y \mid \mathbf{x}) \propto \log P(y) + \sum_{i=1}^{d} \log P(x_i \mid y) logP(y∣x)∝logP(y)+i=1∑d​logP(xi​∣y)

Bernoulli feature likelihood:

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

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

θiy=P(xi=1∣y)=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} θiy​=P(xi​=1∣y)=ny​+2count(xi​=1 in class y)+1​

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=1∣y)P(x_i=1|y)P(xi​=1∣y) = (xi=1x_i=1xi​=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=1∣y)P(x_i=1|y)P(xi​=1∣y) and P(xi=0∣y)P(x_i=0|y)P(xi​=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
Gaussian Naive BayesBernoulli PmfBigram ProbabilitiesLogistic Regression TrainingGini Impurity

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: array

You must run your code first.