Naive Bayes Log-Likelihood (Bernoulli)
Naive Bayes Log-Likelihood (Bernoulli)
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)P(x∣y)P(y)Naive Independence Assumption:
P(x∣y)=i=1∏dP(xi∣y)Log-space (for numerical stability):
logP(y∣x)∝logP(y)+i=1∑dlogP(xi∣y)Bernoulli feature likelihood:
logP(xi∣y)=xilogθiy+(1−xi)log(1−θiy)where θiy is computed with Laplace smoothing (α=1):
θiy=P(xi=1∣y)=ny+2count(xi=1 in class y)+1Function Arguments
X_train: array-like, shape (n_train, d)- Binary training features {0,1}y_train: array-like, shape (n_train,)- Training labelsX_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).
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) = (xi=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) and 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
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: array
Naive Bayes Log-Likelihood (Bernoulli)
Naive Bayes Log-Likelihood (Bernoulli)
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)P(x∣y)P(y)Naive Independence Assumption:
P(x∣y)=i=1∏dP(xi∣y)Log-space (for numerical stability):
logP(y∣x)∝logP(y)+i=1∑dlogP(xi∣y)Bernoulli feature likelihood:
logP(xi∣y)=xilogθiy+(1−xi)log(1−θiy)where θiy is computed with Laplace smoothing (α=1):
θiy=P(xi=1∣y)=ny+2count(xi=1 in class y)+1Function Arguments
X_train: array-like, shape (n_train, d)- Binary training features {0,1}y_train: array-like, shape (n_train,)- Training labelsX_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).
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) = (xi=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) and 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
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: array