Lion Cipher Lab
Build a spam filter with Naive Bayes
- AI Security
- Beginner
- ~45 min
- Free · no account required
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
- turning raw text into features with bag-of-words
- using CountVectorizer to build a vocabulary and count words
- understanding why Naive Bayes works for text (word probabilities)
- training a MultinomialNB spam/ham classifier end-to-end
- predicting on new, unseen messages
- reading accuracy, a confusion matrix, and per-class precision and recall
- explaining why recall is the metric that matters most for spam
- naming the real limits: lost word order, concept drift, and small-data noise
Steps in this lab
- What a spam filter actually is
- Install scikit-learn, offline after this
- Build your own labeled ham/spam set
- Turn words into features: bag-of-words
- Why Naive Bayes works for text
- Train the filter and predict on new messages
- Evaluate, and why recall matters most for spam
- The honest limits: lost context, drift, and small data
- What you can now do that most people can't