Spam Detection with Machine Learning
Detecting spam alerts in emails and messages is one of the main applications that every big tech
company tries to improve for its customers. Apple’s official messaging app and Google’s Gmail are
great examples of such applications where spam detection works well to protect users from spam
alerts. So, if you are looking to build a spam detection system, this article is for you. In this article, I
will walk you through the task of Spam Detection with Machine Learning using Python.
Spam Detection
Whenever you submit details about your email or contact number on any platform, it has become
easy for those platforms to market their products by advertising them by sending emails or by
sending messages directly to your contact number. This results in lots of spam alerts and
notifications in your inbox. This is where the task of spam detection comes in.
Spam detection means detecting spam messages or emails by understanding text content so that
you can only receive notifications about messages or emails that are very important to you. If spam
messages are found, they are automatically transferred to a spam folder and you are never notified
of such alerts. This helps to improve the user experience, as many spam alerts can bother many
users.
Spam Detection using Python
Hope you now understand what spam detection is, now let’s see how to train a machine learning
model for detecting spam alerts using Python. I’ll start this task by importing the necessary Python
libraries and the dataset you need for this task:
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
data =
pd.read_csv("https://raw.githubusercontent.com/amankharwal/SMS-Spam-
Detection/master/spam.csv", encoding= 'latin-1')
data.head()
From this dataset, class and message are the only features we need to train a machine learning
model for spam detection, so let’s select these two columns as the new dataset:
data = data[["class", "message"]]
Now let’s split this dataset into training and test sets and train the model to detect spam messages:
x = np.array(data["message"])
y = np.array(data["class"])
cv = CountVectorizer()
X = cv.fit_transform(x) # Fit the Data
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.33, random_state=42)
clf = MultinomialNB()
clf.fit(X_train,y_train)
Now let’s test this model by taking a user input as a message to detect whether it is spam or not:
sample = input('Enter a message:')
data = cv.transform([sample]).toarray()
print(clf.predict(data))
Enter a message:You won $40 cash price
['spam']
Summary
So this is how you can train a machine learning model for the task of detecting whether an email or a
message is spam or not. A Spam detector detects spam messages or emails by understanding text
content so that you can only receive notifications about messages or emails that are very important
to you. I hope you liked this article on the task of detecting spam alerts with machine learning using
Python. Feel free to ask your valuable questions in the comments section below.