Train and Predict with Gaussian Naive Bayes Practice Problem
This data science coding problem helps you practice Gaussian Naive Bayes, train and predict with gaussian naive bayes, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Gaussian Naive Bayes.
- Problem ID: 202
- Problem key: 202-train-and-predict-with-gaussian-naive-bayes
- URL: https://datacrack.app/solve/202-train-and-predict-with-gaussian-naive-bayes
- Difficulty: hard
- Topic: Gaussian Naive Bayes
- Module: Supervised Learning
Problem Statement
# 🧩 Train and Predict with Gaussian Naive Bayes
---
### 🎯 Goal
Train a Gaussian Naive Bayes model once, store priors, means, and variances, then predict one numeric query using log scores.
---
### 📖 Introduction
This problem is the Gaussian version of the earlier train-and-predict Naive Bayes problem.
The structure is the same:
1. Train from `X` and `y`.
2. Store what the model learned.
3. Predict from the stored model only.
The difference is what training stores.
Categorical Naive Bayes stored count-based likelihood tables:
$$
P(feature=value\mid class)
$$
Gaussian Naive Bayes stores numeric parameters:
- class priors
- means per class per feature
- variances per class per feature
Prediction uses:
$$
log\_score(class)=\log(P(class))+\sum_i \log(GaussianLikelihood(feature_i))
$$
Do not use Laplace smoothing. Gaussian likelihoods come from the Gaussian density formula, not categorical counts.
---
### 💻 Task
Implement `gaussian_naive_bayes_predict(X, y, query)`.
Your nested training helper should store:
```python
{
"classes": [...],
"priors": {class_label: ...},
"means": {class_label: [...]},
"variances": {class_label: [...]}
}
```
Your function should:
- Train from numeric `X` and labels `y`.
- Use population variance.
- Predict one query using only the trained model.
- Calculate one log score per class.
- Return log scores rounded to 6 decimals.
- Return the class with the highest log score.
- Break ties alphabetically.
- Return `{"log_scores": {}, "prediction": None}` when there are no classes.
Assume variances are greater than `0` in scored test cases.
---
### 📥 Input / 📤 Output
**Input:** numeric feature matrix `X`, labels `y`, and one numeric `query`.
**Output:**
```python
{
"log_scores": {...},
"prediction": ...
}
```
---
### 🧩 Starter Code
```python
def gaussian_naive_bayes_predict(X, y, query):
def train_model():
# Learn priors, means, and variances.
pass
def predict_with_model(model):
# Use only the trained model to calculate log scores.
pass
# Train once, then predict with that model.
pass
```
---
### 💡 Example
```python
gaussian_naive_bayes_predict(
[[1, 2], [2, 4], [5, 8], [7, 10]],
["A", "A", "B", "B"],
[1, 2]
)
```
Expected output:
```python
{"log_scores": {"A": -2.837877, "B": -39.531024}, "prediction": "A"}
```
---
### ⚠️ Common Mistakes
- Building categorical likelihood tables.
- Using Laplace smoothing.
- Recalculating means and variances inside prediction.
- Multiplying raw likelihoods instead of adding logs.
- Forgetting alphabetical tie-breaking.