Naive Bayes Score from Training Data Practice Problem
This data science coding problem helps you practice Naive Bayes, naive bayes score from training data, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Naive Bayes.
- Problem ID: 191
- Problem key: 191-naive-bayes-score-from-training-data
- URL: https://datacrack.app/solve/191-naive-bayes-score-from-training-data
- Difficulty: medium
- Topic: Naive Bayes
- Module: Supervised Learning
Problem Statement
# 🧩 Naive Bayes Score from Training Data
---
### 🎯 Goal
Use a multi-feature training dataset to calculate one complete Naive Bayes score.
---
### 📖 Introduction
Problems 1 and 2 introduced the two probabilities Naive Bayes needs:
- **Class prior:** how common a class is in the training data.
$$
P(Class)
$$
- **Feature likelihood:** how often a feature value appears inside a class.
$$
P(feature\_value \mid Class)
$$
Now we combine them.
For a new sample, Naive Bayes looks at each feature value and asks:
"How likely is this value if the sample belongs to this class?"
For example, a new sample can be represented as a row:
| color | size |
|---|---|
| red | small |
For target class `A`:
$$
Score(A)=P(A)\times P(color=red|A)\times P(size=small|A)
$$
This problem calculates the score for **one target class**.
For example, we calculate:
$$
Score(A)
$$
The next problem will repeat the same process for every possible class:
$$
Score(A)
$$
and
$$
Score(B)
$$
Then Naive Bayes chooses the class with the highest score as the prediction.
---
### 📖 Training Data
Example training dataset
| color | size | class |
|---|---|---|
| red | small | A |
| red | large | A |
| blue | small | B |
| blue | large | B |
| red | small | A |
This is an example training dataset. The test cases may use different feature values and class labels, but the structure remains the same.
Each column represents a feature with possible values. For example:
- `color` can have values such as `red` or `blue`.
- `size` can have values such as `small` or `large`.
For query `["red", "small"]` and target class `A`, the function learns the required probabilities from the training data.
---
### 💻 Task
Implement `naive_bayes_class_score(X, y, query, target_class)`.
Your function should:
- Calculate $P(target\_class)$ from `y`.
- For every feature column, calculate $P(query\_value\mid target\_class)$ from the target-class rows.
- Multiply the learned prior and learned likelihoods.
- Return `prior`, `likelihoods`, and `score`, rounded to 6 decimals.
- Return `prior: 0.0`, `likelihoods: []`, and `score: 0.0` if the target class is absent.
---
### 📥 Input / 📤 Output
**Input:** categorical matrix `X`, labels `y`, one categorical `query`, and `target_class`.
**Output**
```python
{
"prior": ...,
"likelihoods": [...],
"score": ...
}
```
---
### 🧩 Starter Code
```python
def naive_bayes_class_score(X, y, query, target_class):
# Your code here
pass
```
---
### 💡 Example
```python
naive_bayes_class_score(
[["red", "small"], ["red", "large"], ["blue", "small"], ["blue", "large"], ["red", "small"]],
["A", "A", "B", "B", "A"],
["red", "small"],
"A"
)
```
Expected output:
```python
{"prior": 0.6, "likelihoods": [1.0, 0.666667], "score": 0.4}
```
---
### ⚠️ Common Mistakes
- Passing in random likelihoods instead of calculating them from the training data.
- Dividing likelihood counts by all rows rather than target-class rows.
- Combining feature values from different columns.