Spam Filter
You'll turn text into numbers, train a model that sorts junk messages from real ones, and pick a threshold you'd trust with your own inbox.
About
Text is where most people first meet a model that feels like it understands something, and it doesn't: it counts words. A bag-of-words model with naive Bayes or logistic regression gets over 95 percent on the free SMS spam collection in an afternoon, and the interesting part is reading what it learned and deciding how cautious it should be. What counts is a filter trained on a public dataset, a look at the words it weighs most, and a threshold chosen on purpose, with a test on messages you wrote yourself. A pretrained language model doesn't count here; the point is to see the mechanism.
At a glance
- Per requirement
- 25 XP
- Completion bonus
- 150 XP
- Requirements
- 5
- Takes
- One weekend
- Skill level
- Intermediate (months of practice)
- Cost
- Free
- Supplies
- Nothing special
- Where
- At home
- Other people
- On your own
Requirements
-
1
Load the SMS spam dataset, split it, and compute the majority baseline. It's about 5,500 messages, 13 percent spam, free from the UCI repository. Split off a fifth for testing before you do anything else. The majority baseline is about 87 percent, so anything below that has learned nothing. Take a screenshot of the counts and the baseline. Photo
-
2
Turn each message into a vector of word counts and look at the vocabulary. Lowercase, split on spaces and punctuation, and count. Keep words that appear in at least two messages, which usually leaves a few thousand. Print the vocabulary size and the ten most common words in spam and in ham. 'Free', 'win' and 'txt' will be near the top, and that's the model's whole secret. Take a screenshot. Photo
-
3
Train naive Bayes or logistic regression and report precision and recall for spam. Either works; naive Bayes is a few lines if you want to write it yourself. Print the confusion matrix on the test set. Then list the 20 words with the highest spam weight and the 20 with the highest ham weight. Take a screenshot of the matrix and the two word lists. Photo
-
4
Move the threshold and plot precision against recall. The model gives a probability, and 0.5 is just a default. Sweep the threshold from 0.1 to 0.9 and plot precision and recall for spam at each value. Decide which mistake is worse for you: a spam message getting through, or a real message from a friend going to the spam folder. Pick a threshold and say why. Take a screenshot of the plot with your threshold marked. Photo
-
5
Test it on ten messages you wrote, including three meant to fool it. Write five normal messages, two obvious spam, and three tricky ones, like a real note from a friend about a free ticket. Run them through and show the probability for each. Expect it to get at least one wrong, and say which word tipped it. Take a screenshot of the ten messages with their scores. Photo