Intermediate level · Module 4: Naive Bayes and Text Classification · Lesson 1 of 3
How Naive Bayes Classifies Text
Learners work through Multinomial Naive Bayes by hand (priors, word likelihoods and Laplace smoothing) and check their numbers against scikit-learn.
In this lesson you will
- Calculate Naive Bayes probabilities by hand
- Explain smoothing and the independence assumption
Key concepts
priorlikelihoodLaplace (add-one) smoothingindependence assumptionlog probabilities
The lesson
Naive Bayes chooses the category c that maximises P(c) × Π P(word | c). P(c) is the share of training texts in the category; P(word | c) is how often the word appears in that category's texts, divided by the category's total word count.
A word never seen in a category would make the product zero. Laplace smoothing adds 1 to every count: (count + 1) / (total + vocabulary size). scikit-learn's MultinomialNB uses alpha=1.0 by default, which is this add-one smoothing. Computations use log probabilities to avoid tiny numbers.
The 'naive' assumption is that words are independent given the category. It is wrong, because words come in phrases, but the classifier still ranks categories well in practice, and its probabilities tend to be over-confident.
Step by step
- Take the eight-message training set from the example.
- Compute the priors and the smoothed likelihoods of three words for two categories.
- Score a new message for both categories using log probabilities.
- Compare with MultinomialNB's predict_proba.
Practice activity
Hand-calculate a Naive Bayes prediction for one message and confirm it with scikit-learn.
Expected result: A correct calculation that matches the library's predicted category.
Check your understanding
-
Why is smoothing needed?
Show answer
Without it, a word unseen in a category gives that category zero probability.
-
Why are log probabilities used?
Show answer
Multiplying many small probabilities gives numbers too small to store accurately.
Further learning
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.