Spam Classifier Using Naive Bayes
Spam and Normal Emails
In the previous lesson, we learned about the Naive Bayes classifier. In this lesson, we use it to build a spam classifier.
Let's first look at a few emails:
- "Congratulations, you have won a free gift card." The email says you have been selected for a $500 gift card, and that you should hurry because the offer expires in 24 hours. This is definitely spam.
- "Urgent: your account will be suspended." The email says your bank account needs immediate verification, or it may be terminated. This also looks like spam.
- "Meeting agenda for 15th October." The email shares the agenda for tomorrow's meeting with the team. This is a normal email.
- "Your order has been shipped." The email gives an order number and a link to track the package. This is also a normal email.
Just by looking at these emails, we can tell which ones are spam. But how can a machine learning model do this?
The presence or absence of certain words can influence whether an email is normal or spam. For the emails above, words like free, won, gift, and urgent make spam more likely, while words like meeting, order, shipped, and agenda make a normal email more likely.
Different datasets will have different words, since a corporate inbox and an e-commerce inbox don't look alike. But the idea holds for any dataset: different words influence whether an email is normal or spam.
Bag of Words
Naive Bayes treats an email or document as a bag of words, which is just a collection of words. It also assumes that the presence of each word in the document is independent of the others. This is why it is called naive.
This assumption isn't entirely true, because words in a document can be related. Consider this sentence:
Click the link to claim your prize.
Here, click and link are related, because the link is what you click. Claim and prize are related too, and so are your and prize.