Naive Bayes Common Mistakes and Best Practices
1. Choosing a variant by data type alone
The likelihood model matters:
GaussianNBmodels each continuous feature with a class-conditional Gaussian distribution.MultinomialNBmodels non-negative event counts or count-like values.BernoulliNBmodels binary feature presence and absence.CategoricalNBmodels discrete categories for each feature.
“Continuous,” “text,” or “categorical” is a starting clue, not a guarantee that the assumptions fit. Compare alternatives with leakage-safe validation.
2. Fitting the vocabulary before the split
Text vectorizers learn a vocabulary and sometimes document-frequency weights. They belong inside the pipeline so validation and test documents cannot influence the feature space.
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import cross_val_score
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import make_pipeline
documents = [
"project meeting tomorrow", "review the project report",
"team lunch on friday", "meeting agenda attached",
"claim free cash now", "winner gets a free prize",
"urgent cash reward", "limited prize offer",
"project notes are ready", "team report review",
"free reward today", "claim your cash prize",
]
labels = ["ham"] * 4 + ["spam"] * 4 + ["ham"] * 2 + ["spam"] * 2
model = make_pipeline(CountVectorizer(), MultinomialNB(alpha=1.0))
scores = cross_val_score(model, documents, labels, cv=3, scoring="f1_macro")
print(f"Mean macro F1: {scores.mean():.3f}")
3. Applying inappropriate scaling
Standardization is not universally beneficial for Naive Bayes. It produces signed values that violate MultinomialNB's non-negative input requirement and changes count semantics. GaussianNB does not need scaling for the mathematical decision rule when applied consistently, although transformations may make its Gaussian approximation more plausible or improve numerical behavior.
4. Forgetting smoothing and log space
For count models, an unseen feature/class combination can otherwise produce a zero likelihood. Tune the smoothing parameter within training folds. Maintained implementations add log probabilities instead of multiplying many small probabilities, avoiding numerical underflow.
5. Trusting predict_proba without calibration checks
Conditional-independence violations can make Naive Bayes probabilities extreme even when class rankings are useful. Evaluate calibration with reliability diagrams, log loss, or Brier score. If calibration is needed, fit the calibrator on data independent of the base estimator's fitting predictions, such as cross-validation predictions.
6. Claiming automatic missing-value support
scikit-learn Naive Bayes estimators do not generally accept arbitrary missing values as “ignored evidence.” Define an imputation or missingness model inside the pipeline, fitted on training data only, and verify that it is compatible with the chosen likelihood.