Notes AI Security Machine Learning

Machine Learning

Naive Bayes

Introduction

Naive Bayes classifies a message by asking, for each class, how likely that class is to produce the message’s words. The word probabilities come from counting words in the labeled training messages. Training is one pass of counting, so it is fast even with tens of thousands of word columns. Paul Graham’s A Plan for Spam (2002) made it a standard method for spam filtering.

The numbers below come from the SMS Spam Collection after the preparation steps in Preparing Text: 5,169 messages, of which 653 are spam.

Bayes’ Rule

Bayes’ rule turns one conditional probability around:

P(A∣B)=P(B∣A)⋅P(A)P(B)P(A \mid B) = \frac{P(B \mid A) \cdot P(A)}{P(B)}

For spam, AA is “the message is spam” and BB is the message:

P(spam∣message)=P(message∣spam)⋅P(spam)P(message)P(\text{spam} \mid \text{message}) = \frac{P(\text{message} \mid \text{spam}) \cdot P(\text{spam})}{P(\text{message})}
TermNameMeaning
P(spam)P(\text{spam})PriorThe share of spam before reading the message: 653/5169=12.6%653 / 5169 = 12.6\%
P(message∣spam)P(\text{message} \mid \text{spam})LikelihoodHow likely spam is to contain this message’s words
P(message)P(\text{message})EvidenceHow likely the message is overall
P(spam∣message)P(\text{spam} \mid \text{message})PosteriorThe probability of spam after reading the message

The evidence is the same for both classes, so the classifier only compares the two numerators, P(message∣spam)⋅P(spam)P(\text{message} \mid \text{spam}) \cdot P(\text{spam}) and P(message∣ham)⋅P(ham)P(\text{message} \mid \text{ham}) \cdot P(\text{ham}), and picks the larger.

The Naive Assumption

P(message∣spam)P(\text{message} \mid \text{spam}) cannot be counted directly, because almost every new message is unique: no training message has exactly the same words. Naive Bayes assumes that, within a class, each word appears independently of the others. The probability of the whole message is then the product of the probabilities of its words:

P(message∣spam)=P(w1∣spam)⋅P(w2∣spam)⋯P(wn∣spam)P(\text{message} \mid \text{spam}) = P(w_1 \mid \text{spam}) \cdot P(w_2 \mid \text{spam}) \cdots P(w_n \mid \text{spam})

Each P(w∣spam)P(w \mid \text{spam}) can be counted. The assumption is false for real text. free appears in 22% of spam messages and entri in 2.8%, so if they were independent, both would appear together in 0.6% of spam messages. They appear together in 2.3%, almost four times as often. The predicted class is still often right, but the probabilities are overconfident. Words that often appear together are counted as separate evidence, so the output is pushed close to 0 or 1. On 1,034 held-out test messages, 88% get a spam probability above 0.99 or below 0.01.

Multinomial Naive Bayes

The multinomial variant works with word counts. The probability of a word given a class is the number of times the word appears in that class’s messages, divided by the total number of words in that class:

P(w∣spam)=count of w in spamtotal words in spamP(w \mid \text{spam}) = \frac{\text{count of } w \text{ in spam}}{\text{total words in spam}}

The spam messages contain 8,936 words in total, and the ham messages 34,834:

WordIn spamIn ham
free19055
claim980
prize820
txt14013
lunch038
love10218
ok5247

scikit-learn has several variants, each for a different kind of feature:

ClassFeatures
MultinomialNBCounts, such as word counts from CountVectorizer
BernoulliNBYes/no features, such as whether a word appears at all
GaussianNBContinuous numbers, modeled as a normal distribution per class
ComplementNBCounts, like MultinomialNB, with a correction for imbalanced classes

The Zero-Count Problem

claim appears 98 times in spam and never in ham, so P(claim∣ham)=0/34834=0P(\text{claim} \mid \text{ham}) = 0 / 34834 = 0. One zero in the product makes the whole product zero. Any message containing claim then gets P(ham)=0P(\text{ham}) = 0, whatever its other words are. With the counts as they are, ok i will claim it at home is classified as spam with probability 100%, although ok appears 247 times in ham and 5 times in spam.

Additive smoothing

Smoothing adds a small number α\alpha (alpha) to every count, so no word has probability zero:

P(w∣c)=count of w in c+αtotal words in c+α⋅VP(w \mid c) = \frac{\text{count of } w \text{ in } c + \alpha}{\text{total words in } c + \alpha \cdot V}

VV is the vocabulary size, 7,101 words here. Adding α⋅V\alpha \cdot V to the denominator keeps the probabilities of all words in a class summing to 1. α=1\alpha = 1 is called Laplace smoothing, and values below 1 are called Lidstone smoothing. With α=1\alpha = 1:

WordP(w∣spam)P(w \mid \text{spam})P(w∣ham)P(w \mid \text{ham})
free190+18936+7101=0.01191\frac{190 + 1}{8936 + 7101} = 0.0119155+134834+7101=0.001335\frac{55 + 1}{34834 + 7101} = 0.001335
claim98+18936+7101=0.006173\frac{98 + 1}{8936 + 7101} = 0.0061730+134834+7101=0.0000238\frac{0 + 1}{34834 + 7101} = 0.0000238

ok i will claim it at home now comes out 89% ham. claim still counts strongly toward spam, but ok and home now outweigh it.

A Worked Example

The message free claim, with α=1\alpha = 1. Multiply the prior by each word’s probability:

spam: 0.1263×0.01191×0.006173=9.29×10−6\text{spam: } 0.1263 \times 0.01191 \times 0.006173 = 9.29 \times 10^{-6} ham: 0.8737×0.001335×0.0000238=2.78×10−8\text{ham: } 0.8737 \times 0.001335 \times 0.0000238 = 2.78 \times 10^{-8}

The two numbers are not probabilities yet, because the evidence was left out. Dividing each by their sum turns them into probabilities that add up to 1:

P(spam∣free claim)=9.29×10−69.29×10−6+2.78×10−8=0.997P(\text{spam} \mid \text{free claim}) = \frac{9.29 \times 10^{-6}}{9.29 \times 10^{-6} + 2.78 \times 10^{-8}} = 0.997

MultinomialNB(alpha=1).predict_proba returns the same 0.997.

Each word multiplies the score by a small number, mostly below 0.01. After a few hundred words, the product is smaller than the smallest number a 64-bit float can store, about 10−30810^{-308}, and becomes 0. Implementations add logarithms instead of multiplying probabilities, since log⁡(a⋅b)=log⁡a+log⁡b\log(a \cdot b) = \log a + \log b. scikit-learn stores them in feature_log_prob_ and class_log_prior_.

Choosing Alpha

α\alpha is a hyperparameter: a setting chosen before training, as opposed to the word probabilities, which training computes. A larger α\alpha moves every word’s probability closer to the same value, so rare words count for less. A smaller α\alpha trusts the counts more.

A grid search over eight values, scoring each with the mean F1 over 5-fold cross-validation, gives:

α\alpha0.010.10.150.20.250.50.751.0
Mean F10.9170.9250.9270.9260.9280.9250.9250.925

0.25 scores highest, but every value from 0.1 to 1.0 is within 0.004 of it, while the F1 of a single value varies by about 0.01 from one fold to the next. The ranking between those values comes from which messages landed in which fold: on a different split of the same data, the grid search picked 0.5. Only 0.01, close to no smoothing, is clearly worse.

What Naive Bayes Needs from the Data

  • Non-negative counts: MultinomialNB raises an error on negative values, so features scaled with standardization cannot be used with it. Word counts need no scaling.
  • Sparse input: it works directly on the sparse matrix from CountVectorizer, with tens of thousands of columns.
  • A representative prior: the prior comes from the class shares in the training data. If spam is 12.6% of training messages but 50% of real traffic, the prior pulls predictions toward ham.

Naive Bayes and Attackers

Each word multiplies the score independently, so an attacker can add words that are common in legitimate messages until their factors outweigh the spam words. This is called a good word attack, studied by Lowd and Meek in “Good Word Attacks on Statistical Spam Filters” (2005). The attacker’s spam text stays the same, and the added words only change the score.

Against a model trained on this dataset with unigrams and bigrams and α=0.25\alpha = 0.25:

MessageP(spam)
Urgent! Your account has been compromised. Verify your details here: www.fakebank.com/verify0.96
the same, plus ok0.60
the same, plus ok see you at home0.02

Three ordinary words move the phishing message from 96% spam to 2% spam. The attacker does not need the model: a list of words that are common in normal messages is enough. The same idea applies to the decision boundary of any linear model, as described in Logistic Regression.