Lion Cipher Lab

Build a spam filter with Naive Bayes

Why it matters

Spam and phishing filtering is the oldest, most deployed application of machine learning in security, and it is the cleanest place to learn the whole craft because you can hold every moving part in your head. Email does not arrive as a neat table of numbers, so the first real skill is turning language into features a model can count, and bag-of-words is the move that has powered text classification for thirty years. Naive Bayes then classifies that text using nothing more mysterious than word probabilities, which means you can actually explain, not just invoke, why it decides a message is…

What you'll learn

Steps in this lab

  1. What a spam filter actually is
  2. Install scikit-learn, offline after this
  3. Build your own labeled ham/spam set
  4. Turn words into features: bag-of-words
  5. Why Naive Bayes works for text
  6. Train the filter and predict on new messages
  7. Evaluate, and why recall matters most for spam
  8. The honest limits: lost context, drift, and small data
  9. What you can now do that most people can't

Open the full interactive lab →