Sub topic 17
Complement naive Bayes for imbalanced text
This chapter assumes you know multinomial naive Bayes, Laplace smoothing, and bag-of-words features from earlier chapters. It covers a variant that helps when one class has far more documents than the others.
The problem with skewed classes
Multinomial naive Bayes scores each class with a log prior plus the sum of log likelihoods. When 90% of documents belong to one class, that class gets a large prior. It also has more word counts, so its likelihood estimates are sharper. Rare classes have noisier estimates, and the model leans toward the majority label.
Complement naive Bayes (CNB) reduces this effect. Instead of asking how well a document matches class c, it estimates word statistics from every document that is not in class c, the complement. It then picks the class whose complement matches the document worst.
How it works
For each class c, CNB computes smoothed word probabilities over the complement:
θcw = (Nc̃w + α) / (N_c̃ + α·|V|)
Here Nc̃w is the count of word w in all documents outside class c, Nc̃ is the total word count outside class c, and |V| is the vocabulary size.
The class score is the negative of the summed complement log likelihoods. A high complement likelihood means the document looks like the other classes, so it counts against c:
score(c) = −Σw fw · log θ_cw
Here f_w is the count of word w in the document. The predicted class is the one with the highest score.
The complement of a small class contains most of the data. Its estimates are therefore stable, and the size of the majority class has less influence on the result.
Using it in scikit-learn
ComplementNB has the same interface as MultinomialNB. This example builds an imbalanced dataset with a rare "billing" class.
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import ComplementNB
from sklearn.pipeline import make_pipeline
texts = [
"reset my password",
"cannot log in to account",
"password reset link broken",
"login page shows error",
"app crashes on login",
"account locked after login",
"refund my last invoice",
"invoice shows wrong charge",
]
labels = ["support"] * 6 + ["billing"] * 2
clf = make_pipeline(TfidfVectorizer(), ComplementNB(alpha=1.0))
clf.fit(texts, labels)
print(clf.predict(["wrong invoice charge"]))
print(clf.predict(["password reset"]))The pipeline uses TF-IDF weights instead of raw counts. CNB is often paired with TF-IDF, and it accepts non-negative real-valued features.
Practical notes
- CNB often beats multinomial naive Bayes on imbalanced text. This is common but not guaranteed, so compare the two on a validation set.
- Evaluate with per-class precision, recall, and F1 (or macro-F1). Accuracy hides poor minority-class performance.
- The
norm=Trueoption in scikit-learn normalizes the weights. It can help when documents differ greatly in length. Try it as a hyperparameter. - CNB scores are not probabilities. Calibrate them if you need probability estimates.
Exercise
Why does complement naive Bayes give a rare class more stable word estimates than multinomial naive Bayes?
Show answer
It estimates word likelihoods for class c from all documents outside c. For a rare class, that complement contains most of the training data, so its counts are large. Multinomial naive Bayes uses only the few documents inside the rare class.