Interactive Python · Classification

Naive Bayes, the game

The spam filter’s favourite algorithm. Build a spam filter word by word, learn the three flavours of Naive Bayes, and fit one in scikit-learn.

~12 minBeginner friendlyPython · scikit-learn5 missions270 XP
Mission 1+25 XP✓ Complete

Bayes, but naive

P(class | features) ∝ P(class) × Π P(featureᵢ | class)posterior ∝ prior × product of likelihoods

The naive part: it assumes every feature is independent of the others given the class. That is rarely exactly true, but the classifier still works surprisingly well and trains extremely fast.

🧠Quick check
+25 XP

What makes Naive Bayes “naive”?

Mission 2+80 XP✓ Complete

Build a spam filter

Each word you add multiplies in its evidence: words common in spam push the probability up, words common in normal mail push it down.

The word probabilities are example numbers, as a spam filter would learn them from labelled emails.

📧Spam calculator
+80 XP

Tap the words that appear in the email:

–P(spam | words)
–words in email
–Verdict
Word likelihoods used (from a training set of labelled emails)
wordP(word | spam)P(word | not spam)
free38%4%
win25%2%
click30%6%
offer28%8%
invoice5%12%
meeting3%22%
project2%18%

Prior: 40% of training emails were spam.

  • ✓Write a 95%+ spam email+25 XP
  • ✓Write a clearly safe email (spam ≤ 5%)+25 XP
  • ✓Mix spammy and normal words until the filter is unsure (40–60%)+30 XP
Mission 3+50 XP✓ Complete

Three flavours

VariantFeaturesTypical use
GaussianContinuous numbers (assumed normally distributed per class)Sensor readings, measurements
MultinomialCounts / frequenciesWord counts in documents
BernoulliBinary 0/1 (present or absent)Whether each word appears
⚡Pick the variant
1 / 5
correct on the first try
Mission 4+25 XP✓ Complete

Code it in Python

The article predicts whether a bank client subscribes to a term deposit (y) from the bank-marketing data with GaussianNB.

Step 1Prepare the data

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score

data = pd.read_csv("bank_marketing.csv")
X = pd.get_dummies(data.drop(columns="y"), drop_first=True, dtype=int)
y = data["y"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

One-hot encoding avoids treating category codes as meaningful numbers.

Step 2Train and evaluate

gnb = GaussianNB()
y_pred = gnb.fit(X_train, y_train).predict(X_test)
print("Mislabeled:", (y_test != y_pred).sum(), "of", len(y_test))
print("Accuracy:", accuracy_score(y_test, y_pred))
🧠Quick check
+25 XP

Which scikit-learn class suits word-count features best?

Final boss+90 XP✓ Defeated

Defeat the Bayes Boss 🐲

Six questions, three hearts.

Question 1 / 6
❤️❤️❤️

💥 The boss winsReview the missions above, then try again.

You’re a Bayes Classifier Master!

Share on LinkedIn
What’s next

Keep levelling up

You learned how Naive Bayes applies Bayes’ theorem with an independence assumption, how evidence from each feature multiplies together, when to use the Gaussian, Multinomial and Bernoulli variants, and how to train one.

Lv 1 Rookie0 / 270 XP