A Machine Learning model that automatically detects whether an email is spam or normal using Natural Language Processing and Naive Bayes — built with Python and Scikit-Learn.
Spam emails are one of the biggest problems in digital communication. This project builds an intelligent system that reads an email and automatically decides if it is spam or a normal email — using Natural Language Processing (NLP) and a Naive Bayes classifier.
I built this to learn how machines can understand and classify human language — one of the most exciting areas of Artificial Intelligence.
Given:
→ The text content of an email
Predict:
→ Is this email SPAM or NORMAL?
I created a balanced dataset of 30 emails — 15 spam and 15 normal.
| Label | Count | Examples |
|---|---|---|
| Spam (1) | 15 | Prize offers, fake warnings, money schemes |
| Normal (0) | 15 | Meeting requests, project updates, greetings |
Create Email Dataset
↓
Convert Text to Numbers (CountVectorizer)
↓
Visualize Spam vs Normal distribution
↓
Split Train and Test (80/20)
↓
Train Naive Bayes Classifier
↓
Evaluate with Accuracy and Confusion Matrix
↓
Test with Custom Email Input
| Metric | Result |
|---|---|
| Algorithm | Multinomial Naive Bayes |
| Accuracy | ~100% on test set |
| Train/Test Split | 80% / 20% |
| Text Processing | CountVectorizer (Bag of Words) |
- 📊 Spam vs Normal bar chart
- 🥧 Spam ratio pie chart
- 🔥 Confusion Matrix heatmap
predict_email("Congratulations! You won a free iPhone. Click now!")
# Spam Probability : 98.7%
# Result : 🚨 SPAM
predict_email("Hi, please review the attached report before Friday.")
# Spam Probability : 1.2%
# Result : ✅ NORMAL
predict_email("Make money fast! Earn $500 daily working from home!")
# Spam Probability : 96.4%
# Result : 🚨 SPAM1. Email text comes in
↓
2. CountVectorizer converts words into numbers
"free prize click" → [0, 1, 0, 1, 1, 0 ...]
↓
3. Naive Bayes calculates probability
P(spam | words) vs P(normal | words)
↓
4. Higher probability wins → Final decision
| Tool | Purpose |
|---|---|
| Python | Programming language |
| Pandas | Data handling |
| NumPy | Numerical computation |
| Matplotlib | Charts |
| Seaborn | Heatmap visualization |
| Scikit-Learn | NLP and ML model |
| CountVectorizer | Convert text to numbers |
| Jupyter Notebook | Development environment |
On Google Colab — no installation needed
- Open the
.ipynbfile in this repository - Click Open in Colab
- Click Runtime → Run all
On local machine
pip install numpy pandas matplotlib seaborn scikit-learn jupyter
jupyter notebook Spam_Email_Detector.ipynbSpam-Email-Detector/
├── Spam_Email_Detector.ipynb # main notebook
└── README.md # project documentation
- Use a real dataset — SMS Spam Collection from Kaggle (5000+ emails)
- Try TF-IDF vectorizer instead of CountVectorizer
- Compare with Support Vector Machine (SVM) model
- Add a simple web interface where user types email and gets result
- Deploy online using Streamlit
This project introduced me to Natural Language Processing — teaching a machine to understand text. The most interesting part was learning how CountVectorizer converts words into numbers so the ML model can process them. I also learned why Naive Bayes works so well for text classification — it is fast, simple and surprisingly accurate even with a small dataset.
- 📂 ML-Practice — ML learning and practice notebooks
- 📂 Student-Performance-Predictor — Logistic Regression project
- 📂 House-Price-Prediction — Linear Regression project
- 📂 Python-Documentation — 30-class Python training course
🎓 Rayhan Uddin · Computer Science Student · Bangladesh
"Teaching a machine to read and understand human language is one of the most fascinating challenges in Computer Science."