Prediction of Playing Golf using Naive Bayes Classifier ( Toy example)
Experiment
Title
Prediction of Playing Golf using Naive Bayes Classifier
🎯 Objective
- To implement a Naive Bayes classifier
-
To compute probabilities using:
- Prior probabilities
- Conditional probabilities
- To predict whether Play Golf = Yes/No
📊 Dataset
| Outlook | Temp | Humidity | Windy | Play |
|---|---|---|---|---|
| Rainy | Hot | High | False | Yes |
| Rainy | Hot | High | True | No |
| Overcast | Hot | High | False | Yes |
| Sunny | Mild | High | False | No |
| Sunny | Cool | Normal | False | Yes |
| Sunny | Cool | Normal | True | No |
| Overcast | Cool | Normal | True | Yes |
| Rainy | Mild | High | False | No |
| Rainy | Cool | Normal | False | Yes |
| Sunny | Mild | Normal | False | Yes |
| Rainy | Mild | Normal | True | Yes |
| Overcast | Mild | High | True | Yes |
| Overcast | Hot | Normal | False | Yes |
| Sunny | Mild | High | True | No |
This creates a small dataset with:
| Feature | Meaning |
|---|---|
| Outlook | Weather condition |
| Temperature | Hot/Mild/Cool |
| Humidity | High/Normal |
| Windy | True/False |
| Play | Target class |
Play is what we want to predict.
Example:
| Outlook | Temp | Humidity | Windy | Play |
|---|---|---|---|---|
| Sunny | Cool | High | True | No |
📚 Theory
🔹 Naive Bayes
Bayes theorem is used in machine learning to calculate the probability that a data sample belongs to a particular class based on its features.
The formula is:
Where:
| Term | Meaning |
|---|---|
| Class label (e.g., Yes/No, Spam/Not Spam) | |
| Feature vector (input data) | |
| Prior Probability - initial probability of class before seeing data | |
| Evidence - total probability of observing features |
In classification problems, the goal is to compute the posterior probability for each class and choose the class with the highest probability.
In Naive Bayes classification, the conditional independence assumption simplifies the likelihood term:
Thus, the classifier becomes:
Meaning:
- Start with the prior probability of a class
- Multiply by probabilities of each feature given that class
- Compute this for all classes
- Select the class with maximum posterior probability
The algorithm assumes:
Meaning:
- Outlook independent of Temperature
- Temperature independent of Humidity
- etc.
Laplace Smoothing
return (count + 1) / (total + unique_vals)
This is:
where:
- = number of unique feature values
Why smoothing?
Without smoothing:
If a feature value never appeared in a class:
Then the entire posterior becomes zero because probabilities are multiplied.
Laplace smoothing avoids this by adding 1 to all counts.
⚙️ Steps
- Compute prior probabilities
- Compute conditional probabilities
- Apply Naive Bayes formula
- Predict class
💻 Simple Python Program
📈 Output
📊 Key Insight
- Each feature contributes independently
- Final decision = product of probabilities
Result
-
Naive Bayes is:
- Simple
- Fast
- Effective for categorical data
- Works well even with small datasets
- Smoothing is essential
Comments
Post a Comment