← 9. Prediction & machine learning

Naive Bayes

A method that uses the probabilities of the words appearing in an email to classify it as spam or legitimate mail.

Bayes' theoremConditional independence assumptionText classificationLaplace smoothing

“Check free offer”freeoffercheckper-word prob.× prior= scoreSpam

Countless emails arrive in your inbox every day. Some are important work messages, while others may be ads or scams. How does a computer read an email and decide that it is spam? Is the mere appearance of certain words enough?

In this way, the method that distinguishes legitimate mail from spam using the words in an email and the probability of belonging to each class is Naive Bayes. It learns the patterns in which words appear in emails that have already been classified, then calculates which group a new email is more likely to belong to.

KNN looks for similar emails, and SVM learns a decision boundary that separates the mail. Naive Bayes differs in that it combines the probabilities of words appearing in each class. Because it uses the simplifying assumption that words are independent of one another given the class, it computes quickly, but that assumption does not accurately reflect the structure of real language.

Naive Bayes is used in email spam filters, document topic classification, sorting customer inquiries by type and more. In this article we use a spam detection example to look at how it works, the probability calculations, preprocessing and smoothing, interpreting classification performance, how it differs from other analyses, and what to watch for in real applications.

SECTION 1

What is Naive Bayes?

A probability-based classification method that uses Bayes' theorem and the conditional independence assumption.

Naive Bayes first determines the proportions of spam and legitimate mail among emails whose correct labels are already known. Next, it learns which words appear how often in each group. When a new email comes in, it classifies it by comparing how likely its words are to appear in spam versus in legitimate mail.

At this point, it assumes that the words are independent of one another given the class. In reality, words like “special” and “discount” tend to appear together, but to keep the calculation simple it combines the probabilities of the individual words. That is why it is called “naive” (simple).

SECTION 2

Why do we need Naive Bayes?

It can quickly classify documents that contain many words.

Emails vary in length and are made up of many different words. Rather than turning the whole document into a single number, making features from the frequency or presence of the words that appear in an email gives you clues for telling spam from legitimate mail.

Training and prediction are relatively fast, and you can also see which words appear frequently in each class. On the other hand, it cannot fully capture the context of expressions or complex relationships between words, so it has limits with sentences whose meaning changes with context.

SECTION 3

How does Naive Bayes work?

It compares groups by combining prior probabilities with the conditional probabilities of words.

The words in an email are clues for detecting spamConcept diagram of multinomial Naive Bayes, which combines word frequencies and prior probabilitiesNew email“Check free offer”free · offer · checkP(word | class)“free” in spam“free” in legitimateCombined with the priorPredictionSpamHigh estimated score※ A mockup for explaining the principle, not the result of collecting real email content or classifying it in real time.
Mockup of the spam detection flow. After learning how often each word appears in each class, it classifies the new email.

① Turn the email into word features

Extract words from the subject and body of the email and, where needed, apply lowercasing, tokenization, removal of unnecessary symbols and so on. In multinomial Naive Bayes, word counts are the typical input values.

② Compute the prior probabilities of spam and legitimate mail

If 20% of the training data is spam and 80% is legitimate mail, the prior probabilities are 0.20 and 0.80, respectively. A new email is not classified from these alone; they are updated with the word information.

③ Estimate the conditional probability of each word appearing in each group

If the word “free” appears relatively often among the word tokens belonging to spam emails, that word can contribute more to the spam score. In the multinomial model, probabilities are calculated relative to the total number of word tokens in each class.

④ Compare the scores for each class

Combine the prior probability with the conditional probabilities of the words in the email to calculate a score for each of the two groups, and assign the email to the group with the higher posterior probability. In practice, log probabilities are summed to handle very small numbers stably.

Key formula

P(spam | words) ∝ P(spam) × P(words | spam). Applying the conditional independence assumption, the second term is approximated by the product of the per-word conditional probabilities. To get the full posterior probability, normalize by dividing by the sum of the scores of all classes.

SECTION 4

Real-world example: email spam detection

We use hypothetical email data to separate legitimate mail from spam.

Of 1,000 hypothetical emails assumed to have been collected from a mail server, 800 are legitimate and 200 are spam. We use 800 for training and set aside 200, with their labels hidden, for the final evaluation. Both sets were split so that they keep the ratio of 80% legitimate and 20% spam.

ItemExample valueRole
Email content“Check out your free offer”Raw text
Word frequenciesfree 1 · offer 1 · check 1Input features
True classLegitimate / spamTarget variable
Training data800 emails (640 legitimate / 160 spam)Training and choosing settings
Test data200 emails (160 legitimate / 40 spam)Checking final performance

The performance figures that follow are hypothetical results for learning constructed so that the calculations are consistent; they were not obtained by training or evaluating a model on real email data.

SECTION 5

What should you check before the analysis?

Check text quality and data leakage together.

Item to checkWhat do you check?Why does it matter?
Labeling criteriaIs the definition of spam consistent?Label quality
Duplicate emailsAre identical emails duplicated across training and test?Prevents overestimating performance
PreprocessingHow tokenization, stop words and letter case are handledConsistency of features
Train/test separationWere the vocabulary and feature selection built from the training data only?Prevents evaluation leakage
Class proportionsIs the share of spam similar to the operating environment?Interpreting precision and predicted probabilities
Personal informationPolicy for handling personal information contained in emailsSafe use of data
Do not build the vocabulary from the test data. The makeup of the vocabulary, feature selection, frequency cutoffs and model settings must be decided within the training data. If you pick important words after looking at the test data, final performance can be overestimated.
SECTION 6

In what order do you run a Naive Bayes analysis?

Proceed step by step, from preprocessing the emails to checking classification performance.

① Prepare the raw emails and labels

Organize the email bodies, subjects and spam labels, and check for duplicates and mislabeled emails.

② Split into training and test data

Keep the 200 test emails aside until the very end, and compare settings with cross-validation within the training data.

③ Build text features from the training data

Fit the vocabulary on the training data and turn each email into a word-frequency vector. For English emails, apply lowercasing and tokenization rules consistently.

④ Learn the probabilities and the smoothing parameter

Apply multinomial Naive Bayes, and choose the Laplace smoothing parameter α by cross-validation on the training data.

⑤ Check the errors on the final test data

Separate cases where spam was missed from cases where legitimate mail was wrongly blocked, and review the confusion matrix, precision, recall and F1 score.

SECTION 7

Why do we need Laplace smoothing?

It adjusts the estimates so that a word not seen in the training data does not make the whole probability 0.

Suppose a new email contains a word that never appeared in the spam emails of the training data. If conditional probabilities are calculated from observed frequencies alone, that word's probability becomes 0, and the spam score obtained by multiplying probabilities also becomes 0. Smoothing eases this problem by adding a small value to each word's estimated frequency.

SettingMeaningInterpretation
α = 0No smoothingZero-probability problem for unseen words
α = 1Laplace smoothingAdds a pseudo-count of 1 for each word
0 < α < 1Weaker smoothingKeeps the influence of training-data frequencies relatively large
Large αStronger smoothingEvens out differences in per-word probabilities

In multinomial Naive Bayes, the estimated probability of each word is (frequency of the word in that class + α) ÷ (total number of words in that class + α × vocabulary size). α is not simply set large; it is chosen by validation within the training data.

SECTION 8

How do you interpret spam detection results?

Check not only how many spam emails were caught but also how many legitimate emails were wrongly blocked.

Suppose the results of applying the model to 200 hypothetical test emails are as shown below. Rows are the actual class and columns are the predicted class.

Actual \ PredictedLegitimateSpamTotal
Legitimate1546160
Spam122840
Total16634200
Checking spam detection results with a confusion matrix200 emails for final evaluation · the numbers below are a hypothetical examplePredicted legitimatePredicted spamActual legitimate1546Actual spam1228
A hypothetical confusion matrix in which 6 legitimate emails are wrongly blocked as spam and 12 spam emails are wrongly classified as legitimate.

Accuracy is 91.0% (182/200), spam precision is 82.4% (28/34), and spam recall is 70.0% (28/40). The spam F1 score is about 75.7%. Accuracy alone looks high, but the model misses 3 out of every 10 spam emails, so depending on the goal you may need to adjust the threshold or the model.

SECTION 9

How do you read class probabilities and decision thresholds?

An output saying an email is likely spam and the decision whether to block it in practice are separate decisions.

Even if a predicted probability of 0.80 is displayed, it is not automatically guaranteed that about 80% of such emails are actually spam. If Naive Bayes's conditional independence assumption does not hold exactly in reality, the output probabilities can be skewed toward overconfidence.

Operating goalMetric to prioritizeDecision to consider
Minimize blocking of important mailSpam precision · false positivesBlock spam more cautiously
Minimize missed spamSpam recallCatch more suspicious emails
Balance the two errorsF1 scoreCompare precision and recall together
Downstream decisions based on probabilityCalibration curve · reliabilityConsider probability calibration

If you are actually deciding on automatic deletion, quarantine or warnings, you must also consider the harm done when legitimate mail is lost. It is safer to set the classification threshold in a separate validation process, not on the test data.

SECTION 10

Things to watch out for with Naive Bayes

Performance can change when the context in which words appear and the time period change.

Are the words really independent?

Words in an email are related to one another, and sentence order matters too. Because Naive Bayes simplifies these relationships, it can misjudge sarcasm, negation and figurative expressions.

Is it wrongly blocking legitimate mail?

False positives, where legitimate mail is classified as spam, can lead to important work messages being missed. You should not run the system by looking at accuracy alone.

Are the words at training time different from those in operation?

Spam wording and sending patterns keep changing. Re-measure performance on mail collected after some time has passed, and retrain if necessary.

How do you manage personal information and bias?

Emails can contain sensitive information such as names, contact details and contract terms. Avoid unnecessary retention and use the data in line with access rights and purpose.

SECTION 11

How does it differ from other analyses?

Even when the classification target is similar, the basis for prediction and the way of learning differ.

MethodCore principleDifference from Naive Bayes
Naive BayesCombines feature probabilities by classConditional independence assumption
Logistic regressionModels class probabilities from input variablesLearns feature coefficients jointly
SVMOptimizes the margin of the class boundaryFocus on the decision boundary
KNNUses the labels of nearby observationsDistance-based
Decision treeRepeatedly splits the data by rulesPredicts with condition rules

For text data, multinomial Naive Bayes is a typical choice suited to word frequencies. There are also Bernoulli Naive Bayes, which handles whether a word appears, and Gaussian Naive Bayes, which assumes a normal distribution for continuous variables, used according to the form of the data.

SECTION 12

How do you report Naive Bayes results?

Describe the data split, preprocessing and misclassifications so they can be reproduced.

The report should include the number of emails and the share of spam, tokenization and vocabulary construction, the type of Naive Bayes used, the smoothing parameter, the cross-validation scheme, and the confusion matrix and evaluation metrics on independent test data. Depending on the operating purpose, false positives and false negatives should also be presented.

Example reporting sentence

A multinomial Naive Bayes classifier using word frequencies as input variables was applied for email spam detection. Of 1,000 hypothetical emails, 800 were used for training and 200 for final evaluation. On the final test data, accuracy was 91.0% (182/200), spam precision 82.4% (28/34), recall 70.0% (28/40) and F1 score 75.7%. Six legitimate emails were misclassified as spam and 12 spam emails were classified as legitimate. These figures are a hypothetical example for explaining the method.

In a real paper or report, also state the α value chosen on the training data and the feature extraction method, and when comparing with other models, use the same test data and metrics.

SECTION 13

Where can Naive Bayes be used?

It can be applied to classification problems in which features appear in different patterns across categories.

FieldClassification goalExample input data
Email managementLegitimate / spamSubject and body words
Customer supportInquiry typeInquiry text
Document organizationNews and report topicsWord frequencies in documents
Review analysisPositive / negativeReview words and expressions
Basic quality controlNormal / abnormal categoryContinuous sensor features (consider the Gaussian variant)

Naive Bayes is a good simple baseline to start from, but it is not the best model for every classification problem. It is appropriate to compare it with other methods, considering the structure and context of the data and the cost of errors in operation.

✓ Wrap-up

Naive Bayes classifies emails by combining word occurrence patterns as probabilities.

The probability calculation is simple, but when you actually use the results you must also look at mail that was wrongly blocked.


In email spam detection, Naive Bayes calculates which group a new email is more likely to belong to based on the frequencies of words in mail that has already been classified. It has the advantage of training quickly by simplifying the relationships between words, but its classifications can waver when the same word means different things in different contexts or when new spam expressions appear. Smoothing reduces the problem of probabilities becoming 0 because of words not in the training data, but it does not eliminate every error.

So when evaluating Naive Bayes, you should look not only at overall accuracy but also separately at how many spam emails were missed and how many legitimate emails were wrongly blocked. In this hypothetical case, accuracy was 91.0%, but spam recall stayed at 70.0%. Which error matters more to reduce depends on the purpose of the actual service. Results can be trusted only when preprocessing and model settings are decided on the training data alone and performance is also checked on emails from a new period.

KEY TAKEAWAY

Naive Bayes estimates the probability that a new email is spam using Bayes' theorem and the conditional independence assumption. Word frequencies, prior probabilities and Laplace smoothing affect the classification, and preprocessing must be fit on the training data only. Do not look at overall accuracy alone; check spam precision and recall together with wrongly blocked legitimate mail.