Gaussian Naive Bayes Prediction with Missing Features

Quick Overview

Implement Gaussian naive Bayes prediction from a trained model when some feature values are missing, by marginalising the missing features out. Tests the per-class log-likelihood, stable log-sum-exp normalisation, the tie rule in predict, and why skipping a term is exact under the independence assumption.

Gaussian Naive Bayes Prediction with Missing Features

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Given a trained Gaussian Naive Bayes model (`class_log_prior` of shape (K,), `theta` and `var` of shape (K, d)) and a matrix `X` where `NaN` marks a missing feature, implement `joint_log_likelihood`, `predict_log_proba`, `predict_proba` and `predict`. Missing features are marginalised out: their Gaussian log-density term is skipped in the per-class sum, never imputed. ```python def joint_log_likelihood(X, class_log_prior, theta, var): ... def predict_log_proba(X, class_log_prior, theta, var): ... def predict_proba(X, class_log_prior, theta, var): ... def predict(X, classes, class_log_prior, theta, var): ... ``` The functions must behave as follows: - `joint_log_likelihood` returns shape (n_samples, K): the log prior plus the sum of the observed features' Gaussian log-pdfs, with the variance used as given. - An all-missing row yields the prior as its posterior. - Log posteriors must stay finite for extreme scores (for example `var = 1e-6` and a feature value of `1000`). Rows of `predict_proba` sum to 1. - `predict` returns `classes[argmax]`, and ties go to the earliest class index. Example: ```python class_log_prior = np.log([0.5, 0.5]) theta = [[0, 0], [1, 2]] var = [[1, 1], [1, 4]] X = [[1.0, np.nan]] joint_log_likelihood(X, class_log_prior, theta, var) # [[-2.1121, -1.6121]] log(0.5) - 0.5*log(2*pi) - 0.5, then log(0.5) - 0.5*log(2*pi) predict_proba(X, class_log_prior, theta, var) # [[0.3775, 0.6225]] predict(X, [0, 1], class_log_prior, theta, var) # [1] ``` Only feature 0 is observed. The value 1.0 is one standard deviation from class 0's mean and exactly at class 1's mean, so class 1 scores 0.5 higher and its posterior is ${1/(1 + e^{-0.5}) \approx 0.6225}$. ```hint Keeping NaN out of the sums NaN spreads through every sum it touches. Neutralise the missing entries before the per-class reduction. ``` ```hint Extreme scores Normalise in log space, and think about what you can subtract from every entry of a row without changing the posterior. ``` ### Constraints & Assumptions - `X` has shape (n_samples, d). A single 1-D row should also work. - All `var` entries are > 0 and are used as given, with no smoothing added. - On rows with no missing values, results must match scikit-learn's `GaussianNB`. ### What a Strong Answer Covers - A per-class Gaussian log-density that drops the masked terms without letting NaN reach the sum. - A stable log-sum-exp normalisation that keeps log posteriors finite on extreme inputs. - The all-missing row, the tie rule in `predict`, and agreement with `GaussianNB` on complete rows. - Why skipping the term is exact marginalisation under the independence assumption, and when the missingness itself carries information. ### Follow-up Questions - Why is skipping the term equivalent to marginalisation for Gaussian NB? - When does ignoring the missing values bias the posterior, and how could the model use the missingness pattern? - How would you fit `theta` and `var` when the training data also has missing values? - Would the same approach work for a class-conditional Gaussian with a full covariance matrix?

Overview: Implement Gaussian naive Bayes prediction from a trained model when some feature values are missing, by marginalising the missing features out. Tests the per-class log-likelihood, stable log-sum-exp normalisation, the tie rule in predict, and why skipping a term is exact under the independence assumption.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Given a trained Gaussian Naive Bayes model (class_log_prior of shape (K,), theta and var of shape (K, d)) and a matrix X where NaN marks a missing feature, implement joint_log_likelihood, predict_log_proba, predict_proba and predict. Missing features are marginalised out: their Gaussian log-density term is skipped in the per-class sum, never imputed.

def joint_log_likelihood(X, class_log_prior, theta, var): ...
def predict_log_proba(X, class_log_prior, theta, var): ...
def predict_proba(X, class_log_prior, theta, var): ...
def predict(X, classes, class_log_prior, theta, var): ...

The functions must behave as follows:

  • joint_log_likelihood returns shape (n_samples, K): the log prior plus the sum of the observed features' Gaussian log-pdfs, with the variance used as given.
  • An all-missing row yields the prior as its posterior.
  • Log posteriors must stay finite for extreme scores (for example var = 1e-6 and a feature value of 1000 ). Rows of predict_proba sum to 1.
  • predict returns classes[argmax] , and ties go to the earliest class index.

Example:

class_log_prior = np.log([0.5, 0.5])
theta = [[0, 0], [1, 2]]
var   = [[1, 1], [1, 4]]
X = [[1.0, np.nan]]

joint_log_likelihood(X, class_log_prior, theta, var)
# [[-2.1121, -1.6121]]   log(0.5) - 0.5*log(2*pi) - 0.5, then log(0.5) - 0.5*log(2*pi)
predict_proba(X, class_log_prior, theta, var)    # [[0.3775, 0.6225]]
predict(X, [0, 1], class_log_prior, theta, var)  # [1]

Only feature 0 is observed. The value 1.0 is one standard deviation from class 0's mean and exactly at class 1's mean, so class 1 scores 0.5 higher and its posterior is 1/(1+e−0.5)≈0.6225{1/(1 + e^{-0.5}) \approx 0.6225}.

Constraints & Assumptions

  • X has shape (n_samples, d). A single 1-D row should also work.
  • All var entries are > 0 and are used as given, with no smoothing added.
  • On rows with no missing values, results must match scikit-learn's GaussianNB .

What a Strong Answer Covers Guidance

  • A per-class Gaussian log-density that drops the masked terms without letting NaN reach the sum.
  • A stable log-sum-exp normalisation that keeps log posteriors finite on extreme inputs.
  • The all-missing row, the tie rule in predict , and agreement with GaussianNB on complete rows.
  • Why skipping the term is exact marginalisation under the independence assumption, and when the missingness itself carries information.

Follow-up Questions Guidance

  • Why is skipping the term equivalent to marginalisation for Gaussian NB?
  • When does ignoring the missing values bias the posterior, and how could the model use the missingness pattern?
  • How would you fit theta and var when the training data also has missing values?
  • Would the same approach work for a class-conditional Gaussian with a full covariance matrix?