Author: Giuseppe Pio La Tosa
Project Date: January 9, 2025
This project aims to develop an intelligent email classification system that distinguishes between Spam and Ham (non-spam) emails. Leveraging Natural Language Processing (NLP) and various Machine Learning (ML) techniques, the system analyzes the textual content of emails to detect unwanted or malicious messages.
- Source: [TREC 2006 Public Spam Corpus]
- Total Emails: 37,822
- Spam: 24,802 (65.9%)
- Ham: 12,910 (34.1%)
label: Integer –1for spam,0for hamorigin: String – raw email content
Comprehensive preprocessing was applied to clean and standardize the text data:
- Renamed
origintoemailsfor clarity - Extracted email body using MIME parsing
- Created
label_text: maps0to “Ham” and1to “Spam” - Removed:
- Punctuation and excessive whitespace
- Stop words
- Short and non-alphanumeric tokens
- Duplicate or conflicting entries
- Emails with empty or malformed content
- Converted all text to lowercase and added a clean
textfield
Key insights derived from EDA:
- WordClouds and Bar Charts illustrated the most frequent terms in spam/ham messages
- Spam emails: short, promotional, with more special characters
- Ham emails: longer, more structured and formal
- Distributions significantly changed after cleaning, revealing that raw data contained substantial noise
Two types of text vectorization methods were used:
- TF-IDF (Term Frequency-Inverse Document Frequency)
- Sparse representation based on word frequency
- Ignores semantic context
- Word2Vec
- Dense embeddings capturing word context and similarity
- Trained using the Skip-Gram model
Implemented and compared the following classification models:
- Support Vector Machine (SVC)
- Random Forest
- Logistic Regression
- Decision Tree
- Accuracy
- Precision
- Recall
- F1-Score
- Confusion Matrix
- ROC Curve & AUC
Tested with both TF-IDF and Word2Vec feature representations using hold-out (70/30) and 10-fold cross-validation.
Used Stratified K-Fold (k=10) to maintain class balance during evaluation.
| Model | Accuracy | Precision | Recall | F1 Score | AUC |
|---|---|---|---|---|---|
| SVC | 0.9857 | 0.9761 | 0.9723 | 0.9742 | 0.9949 |
| Random Forest | 0.9682 | 0.9792 | 0.9050 | 0.9406 | 0.9940 |
| Logistic Reg. | 0.9798 | 0.9730 | 0.9538 | 0.9633 | 0.9947 |
| Decision Tree | 0.9470 | 0.8992 | 0.9123 | 0.9057 | 0.9219 |
➡️ Best model: SVC – high F1-score and lowest error rate
Performed pairwise comparisons using the Wilcoxon test on F1-scores:
- Null hypothesis rejected (p-value < 0.05) → performance differences are statistically significant
- SVC showed the lowest error rate, confirming its superiority
Built a simple web interface for real-time predictions using Flask:
- 📌 Frontend: Form to input email text
- 🤖 Backend:
- Loads pretrained SVC model (
joblib) andTfidfVectorizer - Endpoint
/classifyreceives input, vectorizes it, and predicts spam/ham
- Loads pretrained SVC model (
- 🟢 Instant classification results returned to the user
- Hybrid Feature Extraction: Combine TF-IDF + Word2Vec
- Deep Learning Models: Use Transformers like BERT
- Data Augmentation: Expand dataset with synthetic samples
- Imbalanced Learning: Implement SMOTE or class-weight tuning
- Python 3.11
- Pandas, NumPy, Matplotlib, Seaborn
- Scikit-learn
- Gensim (Word2Vec)
- NLTK / SpaCy
- Flask
- Joblib
- Classification of Phishing Emails with Word Embedding
- Spam Detection with Deep Learning – Elsevier
- TF-IDF, Word2Vec, BERT – Comparative Study
- Machine Learning vs Deep Learning for Spam
Note: Although some references focus on phishing, the methodologies are largely transferable to spam detection.
Thank you for your attention!
If you found this project interesting or useful, feel free to ⭐ the repository or open an issue for discussion or collaboration.