Mathwords logoMathwords

Naive Bayes Classifier — Definition, Formula & Example

A Naive Bayes classifier is a method that sorts an item into its most likely category. It combines how common each category is with how strongly each observed feature points toward that category. Bayes' theorem updates a probability using one piece of evidence. Naive Bayes applies that update to many features at once and then picks a winner.

Let CC be a class variable with possible classes cc, and let x=(x1,…,xn)\mathbf{x} = (x_1, \dots, x_n) be the observed features. Naive Bayes assumes the features are conditionally independent given the class: P(x1,…,xn∣c)=∏i=1nP(xi∣c)P(x_1, \dots, x_n \mid c) = \prod_{i=1}^{n} P(x_i \mid c). Bayes' theorem then gives P(c∣x)∝P(c)∏i=1nP(xi∣c)P(c \mid \mathbf{x}) \propto P(c) \prod_{i=1}^{n} P(x_i \mid c), and the classifier predicts the class with the largest value. The denominator P(x)P(\mathbf{x}) is the same for every class. You can ignore it when you only need to choose a class, but you need it to report actual posterior probabilities.

Key Formula

c^=arg⁡max⁡c  P(c)∏i=1nP(xi∣c)\hat{c} = \underset{c}{\arg\max}\; P(c) \prod_{i=1}^{n} P(x_i \mid c)
Where:
  • c^\hat{c} = the predicted class
  • cc = a possible class, such as spam or not spam
  • P(c)P(c) = prior probability of class c, before any features are seen
  • xix_i = the i-th observed feature, such as whether a certain word appears
  • P(xi∣c)P(x_i \mid c) = likelihood of feature xix_i within class c
  • nn = number of features

Worked Example

Problem: A phone filter labels text messages as spam (S) or not spam (N). From past data, 20% of messages are spam, so P(S)=0.2P(S) = 0.2 and P(N)=0.8P(N) = 0.8. The word "win" appears in 60% of spam and 5% of non-spam messages. The word "prize" appears in 50% of spam and 10% of non-spam messages. A new message contains both words. How does Naive Bayes classify it, and what is the posterior probability that it is spam?
Step 1: Compute the spam score. Multiply the prior by each word's likelihood under spam. The conditional independence assumption is what lets you multiply.
P(S) P(win∣S) P(prize∣S)=(0.2)(0.6)(0.5)=0.06P(S)\,P(\text{win} \mid S)\,P(\text{prize} \mid S) = (0.2)(0.6)(0.5) = 0.06
Step 2: Compute the not-spam score the same way.
P(N) P(win∣N) P(prize∣N)=(0.8)(0.05)(0.1)=0.004P(N)\,P(\text{win} \mid N)\,P(\text{prize} \mid N) = (0.8)(0.05)(0.1) = 0.004
Step 3: The larger score wins. To turn the scores into a probability, divide each by their sum.
P(S∣win, prize)=0.060.06+0.004=0.060.064=0.9375P(S \mid \text{win, prize}) = \frac{0.06}{0.06 + 0.004} = \frac{0.06}{0.064} = 0.9375
Answer: The message is classified as spam, with posterior probability 0.9375 (93.75%). The prior favored not spam 4 to 1. However, "win" is 12 times as likely in spam, and "prize" is 5 times as likely. Together the two words make this message 60 times as likely under spam. That evidence turns 1-to-4 odds into 15-to-1 odds for spam.

Why It Matters

Naive Bayes is commonly taught early in machine learning and natural language processing courses, especially for text classification. Training it only requires counting how often each feature appears in each class. It remains a standard baseline for spam filtering and document categorization. It also works well with thousands of word features, a setting where estimating full joint probabilities would be impossible.

Common Mistakes

Mistake: Letting a feature that never appeared in a class in the training data get likelihood 0. That one zero wipes out the entire product for that class.
Correction: A single unseen word should not make a class impossible. Use Laplace smoothing: add 1 to every count, and adjust the denominator so each feature's probabilities still sum to 1.

Related Terms