Naive Bayes
Weigh the evidence of each word to decide whether a message is spam.
Type a message, or try the next one. Each word adds evidence for spam or for normal, and the evidence adds up to a chance.
- points to spam
- points to normal
- Ltotal evidence
- αsmoothing
L = −0.41 start + 1.67 win+ 2.08 free− 1.10 lunch+ 0.00 today = +2.24
P(spam) = 1 ÷ (1 + e−2.24) = 90%
P(win | spam) = (3 + 1.0) ÷ (45 + 1.0 × 64) = 0.037 P(win | normal) = (0 + 1.0) ÷ (81 + 1.0 × 64) = 0.007
| win | 3 | 0 | 0.037 | 0.007 | +1.67 |
| free | 5 | 0 | 0.055 | 0.007 | +2.08 |
| lunch | 0 | 3 | 0.009 | 0.028 | −1.10 |
| today | 2 | 3 | 0.028 | 0.028 | 0.00 |
Try it in Python
The same 25 training messages, message and α as the Lab, in a few lines of Python. Plain Python counts the words and adds up each word's evidence by hand.
What you are looking at
The model has read 25 short messages that someone already sorted: 10 spam and 15 normal. From them it learned how often each word turns up in each kind of message. Now it reads a new message and weighs every word.
Each bar is one word of the message. A bar to the right means the word is more common in spam; to the left, more common in normal messages. The longer the bar, the stronger the evidence. A word the model never saw gives no evidence at all, so it is ignored. The messages are made up, in simple words, so every count is easy to check.
What each symbol means
- w
- One word of the message.
- P(w | spam)
- How likely a spam message is to use the word w. Read the bar "|" as "given".
- P(spam)
- How common spam is before reading anything: here 10 of 25 messages.
- L
- The total evidence. Above zero points to spam; below zero, to normal.
- α
- Smoothing: a small extra count given to every word.
- V
- The number of different words seen in training.
How the model decides
- Start from how common each kind is. Most messages here are normal, so before reading a single word the model leans a little towards normal. That is the first bar.
- Weigh each word by comparing how likely it is in spam and in normal messages. "Free" turns up in many spam messages and in no normal ones, so it points strongly to spam.
- Add up the evidence. Taking the log of each ratio turns multiplying chances into adding bars, which is easier to see and to compute.
- Turn the total into a chance between 0% and 100%. A total of zero means 50%: the words balance out.
Why "naive"?
The model treats every word as separate evidence, as if words never went together. It ignores order, too: "money free win" scores the same as "win free money". Real language is not like that, so the assumption is naive. Even so, the model is fast, needs very little data, and works well for sorting messages.
Switching a word off shows how much the answer rests on it. It is the same as deleting the word from the message.
Smoothing, α
Without smoothing, a word never seen in normal messages would have a chance of exactly zero there. One such word would then make a message certain spam, however normal the rest looks. Smoothing adds α to every count, as if each word had been seen a little in both kinds.
A small α trusts the counts: rare words give strong evidence. A large α pulls every word's chance towards the same value, so the bars shrink and the answer moves back towards where the model started.
The messages it learned from
Spam
- win a free prize now
- claim your free money today
- you won cash click now
- free gift card click here
- urgent offer win money
- cheap deal buy now
- click here to claim your prize
- limited offer free cash
- win big money today
- act now free offer
Normal
- are we still meeting for lunch today
- can you send me the report
- see you at the meeting tomorrow
- thanks for the lunch
- here is the report for today
- call me when you get home
- the meeting moved to friday
- can we talk tomorrow
- happy birthday see you soon
- please send the photos from friday
- dinner is at home tonight
- did you get my message
- the train is late again
- lunch at noon works for me
- we won the game today
The model in short
- Count every word in messages that are already sorted.
- Smooth the counts, so no word is ever impossible.
- Weigh each word of a new message: is it likelier in spam or in normal messages?
- Add the evidence to the starting point, and turn the total into a chance.