Naive Bayes classifies an instance by asking which class is most probable given its features. It rests on Bayes' theorem and one simplifying assumption.
Bayes' Theorem
For a class C and features x, P(C | x) = P(x | C) × P(C) / P(x). Here P(C) is the prior (how common the class is), P(x | C) is the likelihood, and P(C | x) is the posterior. We only need to compare classes, so the denominator P(x), which is the same for every class, can be ignored: predict the class with the largest P(C) × P(x | C).
The Naive Assumption
Estimating P(x₁, x₂, ..., xₙ | C) for all feature combinations needs a huge amount of data. Naive Bayes assumes the features are conditionally independent given the class, so that P(x | C) = P(x₁ | C) × P(x₂ | C) × ... × P(xₙ | C). The assumption is rarely exactly true, hence "naive", yet the classifier often works very well, especially for text.
Variants and Smoothing
Gaussian Naive Bayes models each numerical feature by a normal distribution with a mean and variance per class. Multinomial and Bernoulli versions handle word counts and word presence in documents. If a feature value never occurs with a class, its probability is zero and wipes out the product; Laplace smoothing adds 1 to every count to avoid this. Naive Bayes is fast, needs little data and gives probabilities.
Definition: Naive Bayes classifier. A probabilistic classifier that applies Bayes' theorem with the assumption that the features are conditionally independent given the class.
Table 3.3: Parts of Bayes' theorem
| Term | Name | Meaning |
|---|---|---|
| P(C) | Prior | How likely the class is before seeing the features |
| P(x ∣ C) | Likelihood | How likely the features are within the class |
| P(C ∣ x) | Posterior | How likely the class is after seeing the features |
| P(x) | Evidence | Same for all classes, so it can be ignored |
Example 3.3: Spam or ham
Problem: P(spam) = 0.4 and P(ham) = 0.6. P(free ∣ spam) = 0.5, P(offer ∣ spam) = 0.6, P(free ∣ ham) = 0.1, P(offer ∣ ham) = 0.05. An email contains both words. Classify it.
Solution:
- For spam: 0.4 × 0.5 × 0.6 = 0.12.
- For ham: 0.6 × 0.1 × 0.05 = 0.003.
- Normalise: P(spam ∣ x) = 0.12 / (0.12 + 0.003) = 0.12 / 0.123 = 0.976.
- The spam score is much larger, so the email is classified as spam.
Result: P(spam ∣ x) = 0.976, so the email is spam.
from sklearn.naive_bayes import GaussianNB
spam = 0.4 * 0.5 * 0.6
ham = 0.6 * 0.1 * 0.05
print("spam score", round(spam, 3), "ham score", round(ham, 3))
print("P(spam|x) =", round(spam / (spam + ham), 3))
X = [[1], [2], [3], [7], [8], [9]]
y = [0, 0, 0, 1, 1, 1]
nb = GaussianNB().fit(X, y)
print("Gaussian NB: 4 ->", nb.predict([[4]])[0],
" 6 ->", nb.predict([[6]])[0])
Output
spam score 0.12 ham score 0.003
P(spam|x) = 0.976
Gaussian NB: 4 -> 0 6 -> 1
Common mistakes
- Forgetting the prior P(C) and using likelihoods alone.
- Believing the independence assumption must be exactly true for the method to work.
- Letting a zero probability wipe out the whole product; use Laplace smoothing.
- Comparing unnormalised scores as if they were probabilities.
Key takeaways
- Posterior ∝ prior × likelihood.
- Naive means conditional independence of features given the class.
- Predict the class with the highest P(C) × ΠP(xᵢ ∣ C).
- Laplace smoothing adds 1 to avoid zero probabilities.
Practice questions
- State Bayes' theorem.
- Why is Naive Bayes called naive?
- What problem does Laplace smoothing solve?
Answers to the practice questions
- P(C | x) = P(x | C) P(C) / P(x).
- It assumes the features are independent given the class.
- Zero probabilities for feature values never seen with a class.
Sources
- Official BCA syllabus Introduction to Machine Learning, course code 26BCA404DSE21, Unit III
