iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Naive Bayes

Naive Bayes: the surprisingly effective classifier built on conditional probability and a strong independence assumption.

ML — Naive Bayes

EXAMPLE
# ===== The math (in one line) =====
# P(class | features) ∝ P(class) * Π P(feature_i | class)
# The 'naive' part: assume features are conditionally independent given the class.
# Wrong in practice; works astonishingly well anyway, especially on text.

# ===== Variants =====
# GaussianNB     features are continuous (assumed Gaussian per class)
# MultinomialNB  feature counts / frequencies (e.g. word counts)
# BernoulliNB    binary features (word presence)
# ComplementNB   imbalanced text; often best for short docs

# ===== Quick text classification =====
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report

texts = [
    'great service highly recommended',
    'rude staff bad food',
    'wonderful experience kind people',
    'never coming back terrible',
    'love this place amazing',
    'awful worst meal',
]
labels = [1, 0, 1, 0, 1, 0]

Xtr, Xte, ytr, yte = train_test_split(texts, labels, test_size=0.33, random_state=42)

pipe = Pipeline([
    ('tfidf', TfidfVectorizer(ngram_range=(1, 2), min_df=1)),
    ('nb', MultinomialNB(alpha=1.0)),    # alpha = additive smoothing
])
pipe.fit(Xtr, ytr)
print(classification_report(yte, pipe.predict(Xte), zero_division=0))

# ===== Continuous features =====
from sklearn.naive_bayes import GaussianNB
from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score
X, y = load_iris(return_X_y=True)
print(cross_val_score(GaussianNB(), X, y, cv=5).mean())

# ===== When NB wins =====
# - Spam / not-spam, sentiment (text classification)
# - Tiny + medium datasets (works well with little data)
# - Real-time + low-memory inference (great for edge / mobile)
# - Multi-class with clear class structure
# - A sane baseline for ANY classification task

# ===== When NB hurts =====
# - Features that are STRONGLY correlated (the independence assumption hurts)
# - High-dimensional regression (it does classification, not regression)
# - When calibrated probabilities matter (NB probabilities are notoriously poorly calibrated)
# - Image / structured-data classification (use trees / nets)

# ===== Calibration =====
from sklearn.calibration import CalibratedClassifierCV
clf = CalibratedClassifierCV(MultinomialNB(), method='isotonic', cv=5)

# ===== Why so fast =====
# Training and prediction are both O(n*d) — linear in data size and feature count.
# No iterative optimisation; closed-form parameter estimation from counts.

# ===== Smoothing (alpha) =====
# alpha = 1.0       Laplace smoothing (default)
# Higher alpha      smoother / less overconfident
# alpha = 0         pure MLE; zero counts -> zero probability -> always wrong on unseen
# Tune via cross-validation.

# ===== Patterns to internalise =====
# - Start with MultinomialNB + TF-IDF for any text classification baseline
# - Pipeline it: vectoriser + classifier, fit once, score on held-out
# - Cross-validate to pick alpha + ngram_range
# - Treat NB as a baseline; only move to more complex models when it underperforms

# ===== Pitfalls =====
# - Negative-valued features with MultinomialNB (it expects non-negative)
# - Trusting raw NB probabilities for thresholding (calibrate first)
# - Ignoring class imbalance (use class_prior or ComplementNB)
# - Over-cleaning text removes useful signal (lowercasing is fine; stripping all punctuation can hurt)

Why it matters

Naive Bayes is the strongest baseline in machine learning. Cheap, fast, surprisingly accurate for its complexity. For text classification it is often within a few percentage points of much bigger models — and it trains in milliseconds. Reach for it first.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
from sklearn.naive_bayes import MultinomialNB
clf = MultinomialNB().fit(X_train_counts, ytr)  # great for text classification
Try it Yourself »

Discussion

Loading…