Email Spam Detection Using Machine Learning and Deep Learning (Random Forest, SVM, LSTM and BERT)

Spam emails waste time and carry phishing links and malware. Simple keyword filters are easy to fool, so modern mail systems rely on models that learn from thousands of examples. This project builds an email spam detection system that compares four very different models – Random Forest, Support Vector Machine, an LSTM network and a fine-tuned BERT transformer – on the same data.

Because it covers classic machine learning, deep learning and a modern language model in one study, it makes a strong final year or M.Tech project, and it ends with a deployable web app where anyone can paste an email and get an instant spam or ham verdict.

How the System Works

  1. Prepare the email data – use a public labelled email dataset, clean headers and HTML, and split into training and test sets.
  2. Extract features – TF-IDF vectors for Random Forest and SVM; tokenised sequences for the LSTM; BERT's own tokenizer for the transformer.
  3. Train the classic models – Random Forest (an ensemble of decision trees with majority voting) and a linear SVM that separates spam from ham with a maximum-margin boundary.
  4. Train the deep models – an embedding + LSTM network that reads the email word by word, and a pre-trained BERT model fine-tuned for spam classification.
  5. Compare fairly – accuracy, precision, recall and F1-score for all four models on the same test set.
  6. Deploy – a Flask backend with a simple HTML/CSS/JS front end that runs the chosen model on any pasted email.

Tools and Techniques

Python with Pandas, scikit-learn (TF-IDF, Random Forest, SVM), TensorFlow/Keras for the LSTM, the Hugging Face Transformers library for BERT, and Flask for the web app – all free and open source.

Project Preview: Four Models Compared

Here is a preview of how this project is built and how the four models compared on the same test set. The system has four stages:

1. Input
  • User pastes or uploads an email
2. Preprocessing
  • Cleaning
  • TF-IDF features
  • Tokenizer for LSTM and BERT
3. Models
  • Random Forest
  • SVM
  • LSTM
  • Fine-tuned BERT
4. Result
  • Flask backend
  • Web interface
  • Spam / ham verdict
Email spam detection model comparison chart
Preview: accuracy, precision, recall and F1-score of the four models on the test set. The fine-tuned BERT model performed best in this study.

Want the complete method? The full code walkthrough, dataset preparation, model tuning and how to write your own results chapter are taught step by step in our live classes. Message us on WhatsApp to get started.

What You Learn

You learn how the same text problem is solved with feature-based machine learning, recurrent networks and transformers, why fine-tuned language models usually win, what each metric tells you about a spam filter, and how to turn a model into a usable web service.

Real-World Applications

  • Email and webmail spam filters
  • Phishing and fraud email screening
  • Customer support inbox triage
  • Comment and message moderation

Related Projects

Explore more machine learning projects on similar topics:

What Support You Get

  • Coding guidance: how to write the four models, the comparison and the Flask web app in Python – explained line by line so you understand every step
  • Dataset guidance: where to find a suitable public dataset and how to clean and prepare it yourself
  • Report guidance: how to structure your own report – introduction, literature review, methodology, results and conclusion
  • Live class: a step-by-step explanation while you write and run the code on your own computer, so you can present your work and answer viva questions with confidence

Studying abroad? We guide you too – learn Python, machine learning and deep learning through our live classes or recorded video lessons.

Who Is This Project For?

This project is a good learning example for B.Tech and B.E. students in CSE, IT, AI and Data Science, ECE and allied branches, MCA and M.Sc students, and M.Tech students who want a method they can extend in their own thesis. It also suits anyone learning machine learning in Python who wants to work on a real problem instead of a textbook example.

Frequently Asked Questions

Which model performed best?

In our study the fine-tuned BERT model scored highest on accuracy, precision, recall and F1-score, while all four models stayed above 96%.

Is BERT too heavy for a student laptop?

Fine-tuning a base BERT model works on a free cloud GPU notebook; inference in the web app runs on a normal laptop.

How is this different from the SMS spam project?

Emails are longer and noisier, so this project focuses on comparing classic, deep and transformer models, while the SMS project is a lighter introduction to NLP.

Can I learn this project with Engineers R World?

Yes. We teach the method step by step in live classes or recorded videos, and you write and run the Python code yourself on your own computer.

Learn the Email Spam Detection (RF, SVM, LSTM, BERT) Project With Us

Want to learn this project for your final year, mini project or M.Tech work? Message us with your university, branch and deadline, and we will share a learning plan – live classes and step-by-step guidance while you build it yourself.

Enquire about this project on WhatsApp or use the enquiry form on our home page. Browse more machine learning projects or start with our free Python learning section.

Interested in this project? Get the details, files and guidance from our team.

Enquire on WhatsApp

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top