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)+1X_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}2D array of shape (n_test, n_classes) containing unnormalized log posteriors for each test sample and each class (sorted in ascending class order).
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.
Use np.unique() to find all classes and compute class priors.
For Bernoulli: P(xi=1∣y) = (xi=1 in class y + α) / (class size + 2α).
Use np.log() for all probability computations to work in log-space.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: array
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)+1X_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}2D array of shape (n_test, n_classes) containing unnormalized log posteriors for each test sample and each class (sorted in ascending class order).
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.
Use np.unique() to find all classes and compute class priors.
For Bernoulli: P(xi=1∣y) = (xi=1 in class y + α) / (class size + 2α).
Use np.log() for all probability computations to work in log-space.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: array