Built a spam classifier using natural language processing to automatically distinguish spam from legitimate email, achieving 98.6% accuracy on a labeled dataset of over 5,500 messages and reducing the manual effort of sorting unwanted email to near zero.
Spam filtering is one of the clearest examples of machine learning solving a real, everyday problem at scale. The core challenge is that spam and legitimate email look superficially similar at first glance. Both are plain text, both have subject lines, both come from real email addresses. The goal was to build a model that could reliably separate the two classes by finding patterns in the language itself rather than relying on simple keyword rules.
The dataset used for this project contained over 5,500 labeled messages, with legitimate emails referred to as "ham" following a standard convention in spam detection research. Class imbalance was a key challenge, since spam messages made up only about 13% of the data.
I began with exploratory data analysis to understand the structural differences between spam and legitimate messages. Spam tended to use high urgency language, unusual punctuation density, and phrases designed to provoke immediate action, while ham messages were more conversational and contextually grounded. I used TF-IDF vectorization to convert the raw message text into numerical features that captured the relative importance of words across the dataset, then trained a Naive Bayes classifier, which is well suited to text classification tasks because of how it handles the conditional probability of word occurrence.
To address the class imbalance, I evaluated the model using precision and recall rather than accuracy alone, ensuring it did not simply learn to predict the majority class.
The model achieved 98.6% accuracy on the test set, with a precision of 98% and recall of 94% on the spam class. In practice this means fewer than 2 in 100 spam messages would slip through to an inbox, and fewer than 1 in 100 legitimate messages would be incorrectly filtered. The model reduces the time a user spends sorting through unwanted email to near zero and demonstrates how a well tuned text classifier can match or exceed human performance on pattern recognition tasks at scale.