Bayes' Theorem
Let X be a data tuple and C a class. Bayes' theorem states P(C | X) = P(X | C) x P(C) / P(X). P(C) is the prior probability of the class, P(X | C) is the likelihood and P(C | X) is the posterior probability. A Bayesian classifier predicts the class with the highest posterior probability, and Bayesian classifiers have the minimum error rate when their probabilities are correct.
Naive Bayesian Classifier
Because P(X) is the same for all classes, we only compare P(X | C) x P(C). With the class conditional independence assumption,
P(X | C) = P(x1 | C) x P(x2 | C) x ... x P(xn | C).
For categorical attributes P(xi | C) is the fraction of tuples of class C with value xi. For continuous attributes a Gaussian (normal) distribution is used. If a count is zero, the Laplace correction adds 1 to every count so that the whole product does not become zero.
Bayesian Belief Networks
When attributes are not independent, a Bayesian belief network (graphical model) is used. It is a directed acyclic graph in which each node is a variable and each arrow a dependency, with a conditional probability table for every node. It allows some attributes to depend on one another while keeping others independent, and it can be trained from data.
Strengths and Limits
Naive Bayes is simple, fast, needs little training data and gives probabilities. Its accuracy falls when attributes are strongly correlated.
Definition: Posterior probability. The probability that a tuple belongs to a class after the tuple's attribute values have been observed, written P(C | X).
Table 3.3: Terms in Bayes' theorem
| Term | Symbol | Meaning |
|---|---|---|
| Prior | P(C) | Class probability before seeing X |
| Likelihood | P(X given C) | Chance of X inside the class |
| Evidence | P(X) | Same for all classes |
| Posterior | P(C given X) | Class probability after seeing X |
Example 3.3: Naive Bayes with two attributes
Problem: Eight days are recorded as (Outlook, Windy, Play): (Sunny, No, Y), (Sunny, Yes, N), (Rain, No, Y), (Rain, Yes, N), (Sunny, No, Y), (Rain, No, Y), (Sunny, Yes, N), (Rain, Yes, Y). Classify the new day X = (Sunny, Windy = Yes).
Solution:
- Counts: Play = Y in 5 days and N in 3 days, so P(Y) = 5/8 and P(N) = 3/8.
- For Y: P(Sunny | Y) = 2/5 and P(Windy = Yes | Y) = 1/5, so the score is 5/8 x 2/5 x 1/5 = 0.05.
- For N: P(Sunny | N) = 2/3 and P(Windy = Yes | N) = 3/3, so the score is 3/8 x 2/3 x 1 = 0.25.
- Posterior of N = 0.25 / (0.05 + 0.25) = 0.833 and of Y = 0.167.
Result: The day is classified as Play = N with probability 0.833.
data = [("Sunny","No","Y"), ("Sunny","Yes","N"), ("Rain","No","Y"),
("Rain","Yes","N"), ("Sunny","No","Y"), ("Rain","No","Y"),
("Sunny","Yes","N"), ("Rain","Yes","Y")]
x = ("Sunny", "Yes")
score = {}
for c in ("Y", "N"):
rows = [r for r in data if r[2] == c]
p = len(rows) / len(data)
for i, v in enumerate(x):
p *= sum(1 for r in rows if r[i] == v) / len(rows)
score[c] = p
total = sum(score.values())
for c in score:
print(c, "score", round(score[c], 3), "posterior",
round(score[c] / total, 3))
Output
Y score 0.05 posterior 0.167
N score 0.25 posterior 0.833
Common mistakes
- Forgetting to multiply by the prior P(C).
- Letting a zero count make the whole product zero instead of using the Laplace correction.
- Treating the scores as probabilities without normalising them.
- Using Naive Bayes on strongly dependent attributes and trusting the exact values.
Key takeaways
- P(C | X) = P(X | C) x P(C) / P(X).
- Naive Bayes assumes class conditional independence.
- Use the Laplace correction for zero counts.
- A belief network models dependencies with a directed acyclic graph.
Practice questions
- What is a prior probability?
- What is the Laplace correction?
- What does a Bayesian belief network represent?
Answers to the practice questions
- The probability of a class before any attribute values are observed.
- Adding 1 to each count so that no probability is zero.
- Dependencies among variables as a directed acyclic graph with probability tables.
Sources
- Official BCA syllabus Data Warehousing and Mining, course code 25BCA406DS03, Unit III
