Gaussian Naive Bayes Score Practice Problem
This data science coding problem helps you practice Gaussian Naive Bayes, gaussian naive bayes score, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Gaussian Naive Bayes.
- Problem ID: 199
- Problem key: 199-gaussian-naive-bayes-score
- URL: https://datacrack.app/solve/199-gaussian-naive-bayes-score
- Difficulty: medium
- Topic: Gaussian Naive Bayes
- Module: Supervised Learning
Problem Statement
# 🧩 Gaussian Naive Bayes Score
---
### 🎯 Goal
Calculate one Gaussian Naive Bayes log score for one target class.
---
### 📖 Introduction
The previous problem calculated one Gaussian likelihood:
$$
P(x\mid class)
$$
A full sample usually has multiple numeric features. Gaussian Naive Bayes calculates one Gaussian likelihood per feature, then combines them with the class prior.
To continue the previous Naive Bayes flow, this problem uses log probabilities:
$$
log\_score(class)=\log(P(class))+\sum_i \log(GaussianLikelihood(feature_i))
$$
where \(i\) refers to the position of a feature in the query. For example, if the query is `[1, 2]`, then the first likelihood uses `1` with the first mean/variance, and the second likelihood uses `2` with the second mean/variance.
This problem scores **one class only**. It does not choose the final prediction yet.
---
### 💻 Task
Implement `gaussian_naive_bayes_score(prior, query, means, variances)`.
Your function should:
- Calculate one Gaussian likelihood for each query feature.
- Add log probabilities to create one class log score.
- Return the individual likelihoods and final log score rounded to 6 decimals.
Assume all variances are greater than `0`.
---
### 📥 Input / 📤 Output
**Input:** class prior, query values, class feature means, and class feature variances.
**Output:**
```python
{
"likelihoods": [...],
"log_score": ...
}
```
---
### 🧩 Starter Code
```python
def gaussian_naive_bayes_score(prior, query, means, variances):
def gaussian_likelihood(value, mean, variance):
# Reuse the Gaussian likelihood calculation from the previous problem.
pass
# Use gaussian_likelihood for each feature.
# Then add log probabilities to calculate the class score.
pass
```
---
### 💡 Example
```python
gaussian_naive_bayes_score(
prior=0.5,
query=[1, 2],
means=[1.5, 3],
variances=[0.25, 1]
)
```
Expected output:
```python
{"likelihoods": [0.483941, 0.241971], "log_score": -2.837877}
```
---
### ⚠️ Common Mistakes
- Choosing the prediction in this problem.
- Using categorical counts instead of Gaussian likelihoods.
- Multiplying raw probabilities instead of adding logs.
- Applying Laplace smoothing.
Starter Code
def gaussian_naive_bayes_score(prior, query, means, variances):
def gaussian_likelihood(value, mean, variance):
# Reuse the Gaussian likelihood calculation from the previous problem.
pass
# Use gaussian_likelihood for each feature.
# Then add log probabilities to calculate the class score.
pass