TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Gaussian Naive Bayes

Classic ML
Hard

Gaussian Naive Bayes is a classification algorithm based on Bayes' theorem with a "naive" assumption that features are conditionally independent given the class. Each feature's likelihood is modeled as a Gaussian distribution, making it fast and effective for many real-world problems.

Given labeled training data and unlabeled test data, predict the class for each test sample by computing the posterior probability for each class.

Algorithm

  1. For each class c, compute the prior probability:
P(c)=ncnP(c) = \frac{n_c}{n}P(c)=nnc​​
  1. For each class c and feature j, compute the mean and population variance:
μcj=1nc∑i∈cxij\mu_{cj} = \frac{1}{n_c} \sum_{i \in c} x_{ij}μcj​=nc​1​i∈c∑​xij​ σcj2=1nc∑i∈c(xij−μcj)2\sigma^2_{cj} = \frac{1}{n_c} \sum_{i \in c} (x_{ij} - \mu_{cj})^2σcj2​=nc​1​i∈c∑​(xij​−μcj​)2
  1. For each test sample, compute the log posterior for each class:
log⁡P(c∣x)∝log⁡P(c)+∑j[−12log⁡(2πσcj2)−(xj−μcj)22σcj2]\log P(c | x) \propto \log P(c) + \sum_j \left[ -\frac{1}{2} \log(2\pi\sigma^2_{cj}) - \frac{(x_j - \mu_{cj})^2}{2\sigma^2_{cj}} \right]logP(c∣x)∝logP(c)+j∑​[−21​log(2πσcj2​)−2σcj2​(xj​−μcj​)2​]
  1. Predict the class with the highest log posterior. Add a small epsilon (1e-9) to variances to avoid division by zero.

Return one integer class label for each row of X_test.

Loading visualization...

Examples

Input: X_train = [[1], [2], [3], [10], [11], [12]], y_train = [0, 0, 0, 1, 1, 1], X_test = [[2], [11], [6]]

Output: [0, 1, 0]

Explanation: Each test value is assigned to the class with the larger Gaussian log posterior.

Input: X_train = [[0, 0], [1, 0], [0, 1], [10, 10], [11, 10], [10, 11]], y_train = [0, 0, 0, 1, 1, 1], X_test = [[0.5, 0.5], [10.5, 10.5]]

Output: [0, 1]

Hint 1

Group training rows by class before computing each feature mean and population variance.

Hint 2

Add log prior and per-feature Gaussian log likelihoods, then keep the largest class score.

Requirements

  • Compute the prior P(c) = n_c / n for each class
  • Compute the mean and population variance (divide by n_c, not n_c - 1) for each feature per class
  • Add epsilon = 1e-9 to all variances to handle zero-variance features
  • Compute log posteriors and predict the class with the highest value
  • Return a list of predicted class labels for the test set

Constraints

  • X_train has at least 2 rows with at least 2 distinct classes in y_train
  • X_test has at least 1 row
  • All feature values are numeric
  • Return a list of integer class labels with the same length as X_test
  • Time limit: 300 ms
Try Similar Problems
Naive Bayes BernoulliLinear Regression Closed FormRidge RegressionLogistic Regression TrainingBernoulli Pmf

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

Gaussian Naive Bayes

Classic ML
Hard

Gaussian Naive Bayes is a classification algorithm based on Bayes' theorem with a "naive" assumption that features are conditionally independent given the class. Each feature's likelihood is modeled as a Gaussian distribution, making it fast and effective for many real-world problems.

Given labeled training data and unlabeled test data, predict the class for each test sample by computing the posterior probability for each class.

Algorithm

  1. For each class c, compute the prior probability:
P(c)=ncnP(c) = \frac{n_c}{n}P(c)=nnc​​
  1. For each class c and feature j, compute the mean and population variance:
μcj=1nc∑i∈cxij\mu_{cj} = \frac{1}{n_c} \sum_{i \in c} x_{ij}μcj​=nc​1​i∈c∑​xij​ σcj2=1nc∑i∈c(xij−μcj)2\sigma^2_{cj} = \frac{1}{n_c} \sum_{i \in c} (x_{ij} - \mu_{cj})^2σcj2​=nc​1​i∈c∑​(xij​−μcj​)2
  1. For each test sample, compute the log posterior for each class:
log⁡P(c∣x)∝log⁡P(c)+∑j[−12log⁡(2πσcj2)−(xj−μcj)22σcj2]\log P(c | x) \propto \log P(c) + \sum_j \left[ -\frac{1}{2} \log(2\pi\sigma^2_{cj}) - \frac{(x_j - \mu_{cj})^2}{2\sigma^2_{cj}} \right]logP(c∣x)∝logP(c)+j∑​[−21​log(2πσcj2​)−2σcj2​(xj​−μcj​)2​]
  1. Predict the class with the highest log posterior. Add a small epsilon (1e-9) to variances to avoid division by zero.

Return one integer class label for each row of X_test.

Loading visualization...

Examples

Input: X_train = [[1], [2], [3], [10], [11], [12]], y_train = [0, 0, 0, 1, 1, 1], X_test = [[2], [11], [6]]

Output: [0, 1, 0]

Explanation: Each test value is assigned to the class with the larger Gaussian log posterior.

Input: X_train = [[0, 0], [1, 0], [0, 1], [10, 10], [11, 10], [10, 11]], y_train = [0, 0, 0, 1, 1, 1], X_test = [[0.5, 0.5], [10.5, 10.5]]

Output: [0, 1]

Hint 1

Group training rows by class before computing each feature mean and population variance.

Hint 2

Add log prior and per-feature Gaussian log likelihoods, then keep the largest class score.

Requirements

  • Compute the prior P(c) = n_c / n for each class
  • Compute the mean and population variance (divide by n_c, not n_c - 1) for each feature per class
  • Add epsilon = 1e-9 to all variances to handle zero-variance features
  • Compute log posteriors and predict the class with the highest value
  • Return a list of predicted class labels for the test set

Constraints

  • X_train has at least 2 rows with at least 2 distinct classes in y_train
  • X_test has at least 1 row
  • All feature values are numeric
  • Return a list of integer class labels with the same length as X_test
  • Time limit: 300 ms
Try Similar Problems
Naive Bayes BernoulliLinear Regression Closed FormRidge RegressionLogistic Regression TrainingBernoulli Pmf

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: array

You must run your code first.