SMS Spam Detection using TensorFlow in Python

In today's digital era, where text messaging has become an integral part of our lives, dealing with SMS spam has become an ongoing challenge. The relentless influx of unwanted and unsolicited messages disrupts our daily routines and poses risks to our privacy and security. To address this issue, machine learning techniques have proven to be effective tools. Among them, TensorFlow, a widely adopted open−source library for deep learning, offers a robust framework for developing advanced models. In this article, we will explore the realm of SMS spam detection and discover how TensorFlow, in conjunction with the versatile programming language Python, can empower us to construct a robust and accurate SMS spam detection system. By following the step−by−step process, encompassing dataset preparation, preprocessing, model training, and evaluation, readers will gain the knowledge needed to establish a more secure and uninterrupted mobile messaging experience.

Understanding SMS Spam Detection

Building a model to automatically categorize incoming text messages as spam or legitimate requires detecting SMS spam. To do this, we require a dataset made up of a sizable number of SMS messages that have been classified as either spam or not spam. The basis for training our TensorFlow model will be this dataset.

Building the SMS Spam Detection Model

Step 1: Preparing the Dataset

Finding a good dataset to train our model is the first step. The publicly accessible UCI SMS Spam Collection is a well−liked dataset for SMS spam detection. The dataset is available for download at the following URL:

Once the dataset is downloaded, we can load it into our Python environment using the pandas library:

import pandas as pd

# Load the dataset
data = pd.read_csv('path/to/dataset.csv', encoding='latin-1')

Step 2: Data Preprocessing

Any machine−learning activity must begin with data preprocessing. This entails transforming the original text messages into a numerical representation that our model can comprehend for SMS spam detection. To normalize the text, this procedure frequently entails stages like tokenization, stop−word removal, and the use of stemming or lemmatization algorithms.

Here's an example of how to preprocess the text data using the NLTK library:

import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize'stopwords')'wordnet')'punkt')

# Preprocess the text
def preprocess_text(text):
    # Tokenization
    tokens = word_tokenize(text.lower())
    # Remove stopwords
    stop_words = set(stopwords.words('english'))
    tokens = [word for word in tokens if word not in stop_words]
    # Lemmatization
    lemmatizer = WordNetLemmatizer()
    tokens = [lemmatizer.lemmatize(word) for word in tokens]
    return ' '.join(tokens)

# Apply preprocessing to the dataset
data['processed_text'] = data['text'].apply(preprocess_text)

Step 3: Feature Extraction

To capture the essence of the SMS messages after text preprocessing, it is important to select significant features. One widely used method for feature extraction is the Bag−of−Words model. This approach represents each text as a vector of word frequencies or presence indicators. However, more advanced techniques like TF−IDF or word embeddings can also enhance the feature representation by considering the importance of words within the entire dataset.

Let's take a closer look at how to extract features using the CountVectorizer from scikit−learn:

from sklearn.feature_extraction.text import CountVectorizer

# Initialize the CountVectorizer
vectorizer = CountVectorizer()

# Extract features from the processed text
features = vectorizer.fit_transform(data['processed_text'])

# Convert the features to a dense matrix
features = features.toarray()

In the above example, we import the CountVectorizer class from scikit−learn. We initialize an instance of CountVectorizer, which will convert the processed text into a matrix representation. The fit_transform() method applies the transformation to the preprocessed text data, generating the feature matrix. Finally, we convert the sparse matrix into a dense matrix using the toarray() method for further analysis and model training.

By extracting meaningful features from the preprocessed SMS text using techniques like CountVectorizer, we enable our model to learn and make accurate predictions in the SMS spam detection task.

Step 4: Model Training

The next step is to train our TensorFlow model after the dataset has been cleaned and the features have been extracted. The high−level API for TensorFlow, Keras, makes it easier to create and train deep learning models. With layers like Dense and Dropout, we can build a sequential model in Keras and define the proper activation functions. Selecting the appropriate loss function, such as binary cross−entropy, is crucial for binary classification. When training the model, many optimization approaches are used to iteratively change the model's parameters and reduce the loss, such as stochastic gradient descent (SGD) or Adam. The adaptability of TensorFlow enables us to quickly train and improve our SMS spam detection model, assuring its efficacy in precisely identifying incoming texts.

Here's an example of how to build and train the model using TensorFlow and Keras:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout

# Define the model architecture
model = Sequential()
model.add(Dense(128, activation='relu', input_shape=(len(vectorizer.get_feature_names()),)))
model.add(Dense(64, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Train the model, data['label'], epochs=10, batch_size=32)

Step 5: Model Evaluation

Evaluation of the model's performance after training is crucial. The accuracy, precision, recall, and F1 score of the model can be evaluated using the test set, which is a piece of the dataset that was not used for training. These indicators enable us to evaluate the generalizability of our model to brand−new, untested SMS messages.

Here's an example of how to evaluate the model using the test set:

# Divide the dataset into test and training sets.
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(features, data['label'], test_size=0.2)

# Evaluate the model on the test set
loss, accuracy = model.evaluate(X_test, y_test)
print('Test Loss:', loss)
print('Test Accuracy:', accuracy)

Step 6: Model Deployment

Once the model has been tested and trained, it may be used to predict the type of incoming SMS messages with great success. We can create a user−friendly interface where users may submit their messages to put this into practice. The model will quickly categorize these messages as spam or authentic in real time, giving immediate feedback on their nature. We can effectively protect ourselves against the infiltration of unwanted SMS spam and maintain a flawless and secure texting experience by putting such a system in place.


In conclusion, SMS spam detection using TensorFlow in Python offers a powerful solution to combat the growing problem of unwanted and unsolicited text messages. By leveraging machine learning techniques and the flexibility of TensorFlow, we can build an efficient and accurate SMS spam detection system. Through the steps of preparing the dataset, preprocessing the text, extracting meaningful features, training the model, and evaluating its performance, we can develop a robust model capable of accurately classifying incoming messages as either spam or legitimate. With the ability to deploy this model in real−time, we can provide users with a reliable defense against SMS spam, enhancing mobile communication security and improving the overall user experience.

Updated on: 26-Jul-2023


Kickstart Your Career

Get certified by completing the course

Get Started