TensorTonic
Problems
Naive Bayes Log-Likelihood (Bernoulli)
Classic ML
Hard

Train a Bernoulli Naive Bayes model on binary features and return the unnormalized log posterior for every test sample and class.

θjc=Njc+1Nc+2\theta_{jc}=\frac{N_{jc}+1}{N_c+2}θjc​=Nc​+2Njc​+1​ log⁡P~(c∣x)=log⁡P(c)+∑j=1D[xjlog⁡θjc+(1−xj)log⁡(1−θjc)]\log \widetilde{P}(c\mid\mathbf{x})=\log P(c)+\sum_{j=1}^{D}\left[x_j\log\theta_{jc}+(1-x_j)\log(1-\theta_{jc})\right]logP(c∣x)=logP(c)+j=1∑D​[xj​logθjc​+(1−xj​)log(1−θjc​)]

Here, NcN_cNc​ is the number of training samples in class ccc, NjcN_{jc}Njc​ counts class-ccc samples whose feature jjj is one, and DDD is the feature count. The added one and two implement Laplace smoothing. Order output columns by ascending class label and return a NumPy array of shape (n_test, n_classes). You do not need to round the returned values; answers that match the expected values to four decimal places are accepted.

Loading visualization...

Examples

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

Output: [[-2.8904, -1.5041]]

Explanation: The columns correspond to classes 0 and 1, and the observed feature pattern 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.7726], [-2.7726, -1.674]]

Hint 1

Use np.unique(y_train, return_counts=True) to obtain sorted classes and priors.

Hint 2

For one class, X_train[y_train == label].sum(axis=0) gives all feature-one counts.

Hint 3

Evaluate the Bernoulli terms with matrix multiplication against np.log(theta) and np.log1p(-theta).

Requirements

  • Estimate class priors from training frequencies
  • Apply Laplace smoothing to every Bernoulli feature probability
  • Include contributions from both present and absent features in log space
  • Return a NumPy array. Rounding to four decimals is optional

Constraints

  • Training and test features contain only 0 and 1
  • Training labels contain at least one sample per represented class
  • Use NumPy only
Try Similar Problems
Gaussian Naive BayesHardLogistic Regression Training LoopMediumCompute Gini Impurity for a SplitMediumBigram Probabilities (Add-1 Smoothing)HardBernoulli Probability Mass Function & MomentsEasyDecision Tree Best SplitHardRandom Forest Majority VoteMediumCompute Entropy for a NodeEasyRandom Forest from ScratchHardSVM with Hinge Loss (SGD)HardAdaBoost from ScratchHardBagging ClassifierMedium
Scratchpad

Accepts: array

Accepts: array

Accepts: array

You must run your code first.
Naive Bayes Log-Likelihood (Bernoulli)

Naive Bayes Log-Likelihood (Bernoulli)

Classic ML
Hard

Train a Bernoulli Naive Bayes model on binary features and return the unnormalized log posterior for every test sample and class.

θjc=Njc+1Nc+2\theta_{jc}=\frac{N_{jc}+1}{N_c+2}θjc​=Nc​+2Njc​+1​ log⁡P~(c∣x)=log⁡P(c)+∑j=1D[xjlog⁡θjc+(1−xj)log⁡(1−θjc)]\log \widetilde{P}(c\mid\mathbf{x})=\log P(c)+\sum_{j=1}^{D}\left[x_j\log\theta_{jc}+(1-x_j)\log(1-\theta_{jc})\right]logP(c∣x)=logP(c)+j=1∑D​[xj​logθjc​+(1−xj​)log(1−θjc​)]

Here, NcN_cNc​ is the number of training samples in class ccc, NjcN_{jc}Njc​ counts class-ccc samples whose feature jjj is one, and DDD is the feature count. The added one and two implement Laplace smoothing. Order output columns by ascending class label and return a NumPy array of shape (n_test, n_classes). You do not need to round the returned values; answers that match the expected values to four decimal places are accepted.

Loading visualization...

Examples

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

Output: [[-2.8904, -1.5041]]

Explanation: The columns correspond to classes 0 and 1, and the observed feature pattern 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.7726], [-2.7726, -1.674]]

Hint 1

Use np.unique(y_train, return_counts=True) to obtain sorted classes and priors.

Hint 2

For one class, X_train[y_train == label].sum(axis=0) gives all feature-one counts.

Hint 3

Evaluate the Bernoulli terms with matrix multiplication against np.log(theta) and np.log1p(-theta).

Requirements

  • Estimate class priors from training frequencies
  • Apply Laplace smoothing to every Bernoulli feature probability
  • Include contributions from both present and absent features in log space
  • Return a NumPy array. Rounding to four decimals is optional

Constraints

  • Training and test features contain only 0 and 1
  • Training labels contain at least one sample per represented class
  • Use NumPy only
Try Similar Problems
Gaussian Naive BayesHardLogistic Regression Training LoopMediumCompute Gini Impurity for a SplitMediumBigram Probabilities (Add-1 Smoothing)HardBernoulli Probability Mass Function & MomentsEasyDecision Tree Best SplitHardRandom Forest Majority VoteMediumCompute Entropy for a NodeEasyRandom Forest from ScratchHardSVM with Hinge Loss (SGD)HardAdaBoost from ScratchHardBagging ClassifierMedium
Scratchpad

Accepts: array

Accepts: array

Accepts: array

You must run your code first.
Problems
Loading...
1 / 1