Naive Bayes Classifier
Source: Unit 3 §9
The setup
Naive Bayes is a highly practical Bayesian method, with performance comparable to neural nets and decision trees on a lot of real problems.
Each instance is a tuple of attribute values , and the target is drawn from a finite set. Assign the most probable value:
is easy - count how often appears in the training set. But needs a huge dataset, because it has one parameter for every combination of attribute values. Most combinations will never appear in your training data at all.
The naive conditional independence assumption
Naive assumption: attribute values are conditionally independent given the target value. The joint probability then becomes a product of individual probabilities:
- Number of terms to estimate drops to (number of distinct attribute values) × (number of distinct target values), which is far fewer than the joint.
- When the conditional independence assumption actually holds, NB classification equals MAP classification - nothing is lost.
- When it does not hold, the probabilities come out wrong, but the argmax is often still right, which is why the method survives its own bad assumption.
Worked example 1: Play Tennis
The same 14-row dataset as the decision-tree example. Classify:
Prior probabilities: and .
| Attribute value | P(· | Yes) | P(· | No) |
|---|---|---|
| Outlook = Sunny | 2/9 = 0.222 | 3/5 = 0.60 |
| Temp = Cool | 3/9 = 0.333 | 1/5 = 0.20 |
| Humidity = High | 3/9 = 0.333 | 4/5 = 0.80 |
| Wind = Strong | 3/9 = 0.333 | 3/5 = 0.60 |
- YES: .
- NO: .
- NO (0.0206) beats YES (0.0053), so PlayTennis(x) = NO.
The slide shows YES = 0.0211. The correct value is ≈ 0.0053. The final answer is unaffected - NO is still correct, because NO ≈ 0.0206 dominates either way - but do not reproduce 0.0211 in an exam.
Worked example 2: text classification
Training data:
| Sentence | Class |
|---|---|
| A great game | sports |
| The election is over | not sports |
| very clean match | sports |
| a clean but forgettable game | sports |
| it was a close election | not sports |
Task: classify "A very close game" as sports or not sports.
Ignore word order and grammar; treat a document as a set of words, with the features being word frequencies. It is simplistic and it works well.
Word independence turns a sentence probability into a product:
and we compare against , dropping the common divisor .
Priors: and .
The word "close" never appears in a sports sentence, so , and one zero factor annihilates the entire product. A single unseen word would veto a class no matter how much evidence the other words provide. The fix is Laplace smoothing.
Laplace smoothing: add 1 to every word count, and add the vocabulary size to the denominator.
- Sports has 11 words.
- Not-sports has 9 words.
- The vocabulary is 14 distinct words.
| Word | P(word | sports) | P(word | not sports) |
|---|---|---|
| a | (2+1)/(11+14) = 3/25 | (1+1)/(9+14) = 2/23 |
| very | (1+1)/25 = 2/25 | (0+1)/23 = 1/23 |
| close | (0+1)/25 = 1/25 | (1+1)/23 = 2/23 |
| game | (2+1)/25 = 3/25 | (0+1)/23 = 1/23 |
- sports: , that is .
- not sports: , that is .
- sports () beats not sports (), so the sentence is classified as SPORTS.
Note that smoothing did not just avoid a zero product: "close" still gets the smallest numerator in the sports column, so the evidence against sports is preserved, just not made absolute.
Advanced text techniques
- Remove stop words (a, able, the). "a very close game" becomes "very close game".
- Stemming / lemmatisation groups related forms: "election" and "elected" are counted as one.
- n-grams count sequences of words - "clean match", "close election" - instead of single words, recovering a little of the word order the bag threw away.
- TF-IDF (term frequency, inverse document frequency) is a numeric statistic reflecting how important a word is to a document within a corpus.