Build a Simple Spam Classifier in Google Colab with Python: Step-by-Step

Spam classification is a simple way to see how machine learning can classify text.

In this practical example, we will use Google Colab, Python, scikit-learn, CountVectorizer, and Multinomial Naive Bayes to build a small model that predicts whether a message is spam or not spam.

This is a beginner example rather than a production spam-detection system.

What We Will Build

The workflow is simple:

Messages → Convert Text to Numbers → Train Model → Test New Messages → Prediction

Step 1: Open Google Colab

Open Google Colab and create a new notebook.

No local Python installation is required for this example.

Screenshot 1: New Google Colab notebook ready for the project.

Step 2: Import the Libraries

Run this code:

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import make_pipeline

CountVectorizer converts the text into word-count features, while MultinomialNB performs the classification.

Step 3: Create a Small Training Dataset

For this beginner example, we will create the data directly inside the notebook:

messages = [
    "Win a free iPhone now",
    "Congratulations you won a cash prize",
    "Claim your free voucher today",
    "Urgent claim your lottery prize now",
    "Free entry in a weekly competition",
    "Click here to get your reward",
    "Earn money fast from home",
    "Limited offer claim your prize",
    "Meeting moved to 3 PM tomorrow",
    "Can you send me the report",
    "Happy birthday have a great day",
    "Please review the attached document",
    "Let's have lunch tomorrow",
    "Your appointment is confirmed",
    "Thanks for your help today",
    "See you at the office Monday"
]

labels = [
    "spam", "spam", "spam", "spam",
    "spam", "spam", "spam", "spam",
    "not spam", "not spam", "not spam", "not spam",
    "not spam", "not spam", "not spam", "not spam"
]

The first eight messages represent spam examples, while the remaining messages represent normal messages.

Step 4: Train the Spam Classifier

Create and train the model:

model = make_pipeline(
    CountVectorizer(),
    MultinomialNB()
)

model.fit(messages, labels)

print("Model trained successfully.")

Expected result:

Model trained successfully.

Screenshot 2: Training code and the successful output in Google Colab.

Step 5: Test New Messages

Now test messages that were not included exactly in the training data:

test_messages = [
    "Congratulations! Claim your free prize now",
    "Please send the meeting notes tomorrow"
]

predictions = model.predict(test_messages)

for message, prediction in zip(test_messages, predictions):
    print(message, "->", prediction)

In this example, the result is:

Congratulations! Claim your free prize now -> spam
Please send the meeting notes tomorrow -> not spam

Screenshot 3: The prediction results displayed in Google Colab.

What Happened?

The model learned patterns from the example messages.

Words such as free, prize, claim, and reward appeared frequently in our spam examples, while ordinary work and personal messages were associated with the not spam category.

The model then used those learned patterns to classify the two new messages.

Troubleshooting

If you see:

ModuleNotFoundError: No module named 'sklearn'

run:

!pip install scikit-learn

Then run the notebook cells again.

If the model gives an incorrect prediction, remember that this example uses a very small training dataset. Adding more realistic examples usually gives the model more information to learn from.

My Observation

What I found useful in this example is how little code is required to create a complete text-classification workflow.

However, the small dataset also clearly shows an important machine-learning lesson: the quality and amount of training data matter. A real spam classifier would require a much larger and more representative dataset and proper evaluation.

Screenshot 4: The completed notebook showing the training and prediction cells together.

Final Thoughts

This simple project demonstrates a complete machine-learning task in Google Colab:

Prepare Data → Convert Text → Train Model → Predict → Review Results

It is a useful starting point for understanding text classification before moving to larger datasets and more advanced Natural Language Processing models.

Wise up to AI. Learn it. Use it. Grow with it.

Written by Yousef Ismail AbuAlShawareb

References and Further Reading

Google (n.d.) Google Colaboratory – Frequently Asked Questions. Available at: https://research.google.com/colaboratory/faq.html (Accessed: 5 October 2026).

Scikit-learn Developers (n.d.) MultinomialNB. Scikit-learn Documentation. Available at: https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.MultinomialNB.html (Accessed: 5 October 2026).

Scikit-learn Developers (n.d.) Naive Bayes. Scikit-learn Documentation. Available at: https://scikit-learn.org/stable/modules/naive_bayes.html (Accessed: 5 October 2026). 

Comments