Naive Bayes Cheat Sheet
A reference for Naive Bayes covering Gaussian, Multinomial, and Bernoulli variants in scikit-learn, Bayes' theorem, and Laplace smoothing.
1 PageBeginnerMar 15, 2026
GaussianNB
For continuous, normally distributed features.
python
from sklearn.naive_bayes import GaussianNBmodel = GaussianNB()model.fit(X_train, y_train)y_pred = model.predict(X_test)proba = model.predict_proba(X_test) # assumes features are normal per class
MultinomialNB for Text
A classic pipeline for text classification.
python
from sklearn.feature_extraction.text import CountVectorizerfrom sklearn.naive_bayes import MultinomialNBfrom sklearn.pipeline import make_pipelinetext_clf = make_pipeline( CountVectorizer(stop_words='english'), MultinomialNB(alpha=1.0) # alpha: Laplace/Lidstone smoothing)text_clf.fit(train_texts, train_labels)predictions = text_clf.predict(test_texts)
BernoulliNB
For binary/boolean feature vectors.
python
from sklearn.naive_bayes import BernoulliNBmodel = BernoulliNB(alpha=1.0, binarize=0.0) # binary/boolean feature presencemodel.fit(X_train, y_train)
Key Concepts
Core theory behind Naive Bayes.
- Bayes' theorem- P(y given x) is proportional to P(x given y) times P(y): combines likelihood and prior into a posterior
- Conditional independence- The 'naive' assumption that features are independent given the class; rarely true but works well in practice
- Laplace smoothing (alpha)- Prevents zero probabilities for feature/class combinations unseen during training
- GaussianNB- Assumes continuous features follow a normal distribution within each class
- MultinomialNB- Best for discrete counts, such as word frequencies in text classification
- BernoulliNB- Best for binary/boolean features, such as word presence or absence
Pro Tip
Naive Bayes is a fast, strong baseline for text classification despite its unrealistic independence assumption — but its predicted probabilities are often poorly calibrated even when the predicted class label is correct, so don't trust predict_proba() outputs at face value.
Was this cheat sheet helpful?
Explore Topics
#NaiveBayes#NaiveBayesCheatSheet#DataScience#Beginner#GaussianNB#MultinomialNBForText#BernoulliNB#KeyConcepts#MachineLearning#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance