Neural Mastery
← Practice
Gaussian Naive Bayes Classifiermedium

Gaussian Naive Bayes Classifier

medium Classical ML from Scratch 20–25 min

NumPy allowed · plain Python earns 10 bonus points

Implement Gaussian Naive Bayes end to end -- parameter estimation (per-class means/variances) AND MAP prediction (log-prior plus log-likelihood, maximized over classes) -- a genuinely different exercise from a single Bayes'-theorem posterior calculation.

Task

Implement gaussian_naive_bayes_predict(X_train, y_train, x_test): for each class, estimate its prior and a per-feature Gaussian (mean, variance) from the training rows of that class, then predict the class with the highest log(prior) + sum(log Gaussian pdf) for x_test.

Function Signature

gaussian_naive_bayes_predict(X_train: list[list[float]], y_train: list[int], x_test: list[float]) -> int

Examples

Example 1: Clearly Class 0
Input: X_train = [[1], [1.2], [0.8], [5], [5.2], [4.8]] y_train = [0, 0, 0, 1, 1, 1] x_test = [0.5]
Output: 0
Explanation: x=0.5 sits well inside the class-0 cluster (mean 1.0)
Example 2: Clearly Class 1
Input: X_train = [[1], [1.2], [0.8], [5], [5.2], [4.8]] y_train = [0, 0, 0, 1, 1, 1] x_test = [4.5]
Output: 1
Explanation: x=4.5 sits well inside the class-1 cluster (mean 5.0)

Constraints

  • Work in log-space (log-prior + sum of log-likelihoods) to avoid numerical underflow from multiplying many small probabilities.
  • Add 1e-9 to every estimated variance before using it, to avoid a divide-by-zero on a feature with zero within-class spread.
  • Break ties between classes by choosing the smaller class label.
Python3Saved ✓
import math

def gaussian_naive_bayes_predict(X_train: list[list[float]], y_train: list[int], x_test: list[float]) -> int:
"""Fit a per-class, per-feature Gaussian on X_train/y_train (assuming
feature independence given the class -- the 'naive' assumption), then
return the class label that maximizes log(prior) + sum of per-feature
log Gaussian likelihoods for x_test. Add 1e-9 to every feature's
variance to avoid division by zero. Break ties by the smaller class
label."""
# Your implementation here
pass

Case 1: Clearly Class 0
Input: X_train = [[1], [1.2], [0.8], [5], [5.2], [4.8]] y_train = [0, 0, 0, 1, 1, 1] x_test = [0.5]
Expected: 0
Case 2: Clearly Class 1
Input: X_train = [[1], [1.2], [0.8], [5], [5.2], [4.8]] y_train = [0, 0, 0, 1, 1, 1] x_test = [4.5]
Expected: 1