Naive Bayes & N-Grams
Text classification from scratch: a naive Bayes bag-of-words classifier and n-gram language models, both with add-k smoothing.
╌╌╌╌
Two text classifiers built from scratch — a naive Bayes bag-of-words model and a set of n-gram language models — that learn word statistics from a corpus and label unseen documents by them. Both estimate probabilities by counting, and both smooth those counts so an unseen word does not zero out a whole document.
Naive Bayes picks the most probable class. For a document and classes , the classifier returns the maximum a posteriori class. Dropping the shared denominator and assuming words are conditionally independent given the class,
where is the class prior and is the likelihood of word under class . The product hides a modeling choice: the bag-of-words representation treats a document as its multiset of word counts with position discarded, so scores each token as an independent draw from the class's word distribution. That independence is wrong — words in text are correlated — but it keeps estimation to simple counts and still classifies well. Products of many small probabilities underflow, so the implementation works in log space, where the product becomes a sum: , and the is unchanged.
Smoothing handles unseen words. A word absent from a class in training gives and annihilates the product. Add- (Laplace) smoothing shifts every count up by :
with the vocabulary. The numerator keeps an unseen word from zeroing the whole product, and the matching term in the denominator keeps the row summing to one, so smoothing rescues the estimate without breaking it into an improper distribution. Small trusts the counts; large pulls every word toward uniform.
For tasks like sentiment, whether a word appears matters more than how many times. Boolean (binary) naive Bayes clips each document's counts to presence or absence, , before estimating and scoring, so a review repeating great ten times cannot swamp the decision on its own. On short texts this variant often edges out the count-based model.
The classifier comes together as four counting steps:
- Tokenize. Split each document into words and reduce it to its bag of counts.
- Estimate the prior. Set from the fraction of training documents in each class.
- Estimate the likelihoods. Compute each as an add- smoothed count ratio over the class's text.
- Score. Sum for every class and return the .
N-grams model the next word. The n-gram side estimates the probability of a sequence with the chain rule under a Markov assumption — each word depends only on the previous :
Each conditional is a smoothed count ratio, exactly as above but keyed on the preceding context. Larger captures more context but fragments the counts, so unigram, bigram, and trigram models trade coverage against sharpness. Used as a classifier, the model trained on each class scores a test document, and the class whose model assigns higher probability wins.
You can read the full report.
References
- Project repository
- Reference notes: Naive Bayes and Sentiment Classification
- Reference notes: N-Gram Language Models
- Reference notes: Smoothing and Backoff
╌╌ END ╌╌