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}
  1. For each class c and feature j, compute the mean and population variance:
μcj=1ncicxij,σcj2=1ncic(xijμcj)2\mu_{cj} = \frac{1}{n_c} \sum_{i \in c} x_{ij}, \quad \sigma^2_{cj} = \frac{1}{n_c} \sum_{i \in c} (x_{ij} - \mu_{cj})^2
  1. For each test sample, compute the log posterior for each class:
logP(cx)logP(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]
  1. Predict the class with the highest log posterior. Add a small epsilon (1e-9) to variances to avoid division by zero.
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]

Class 0 has mean 2 and class 1 has mean 11. Test points near class 0's mean are classified as 0, near class 1's mean as 1. The midpoint sample x=6 is closer to class 0 in this Gaussian model.

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]

With 2D features, each class has a cluster of points. The test points fall clearly within each cluster's distribution.

Hint 1

Group training data by class. For each class, compute the mean and variance for each feature. Then for each test point, compute log_prior + sum of log-likelihoods for each class, and pick the class with the highest total.

Hint 2

The Gaussian log-likelihood for feature j given class c is: -0.5 * log(2 * pi * var) - (x_j - mean)^2 / (2 * var). Remember to add epsilon to var before using it.

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