This project implements an end-to-end email classification system using Recurrent Neural Networks (RNN) and Long Short-Term Memory (LSTM) networks. The system classifies emails as ham (legitimate) or spam using deep learning.
The project includes:
- Dataset loading & cleaning
- Exploratory Data Analysis (EDA)
- Text preprocessing
- RNN and LSTM model training
- Evaluation (accuracy, confusion matrix, precision, recall, F1)
- Custom email prediction pipeline
- Raw email text preprocessing (tokenization, stemming, stopword removal, n-grams)
- Train/validation/test split with stratification
- Deep learning-based text classification
- RNN baseline model
- LSTM enhanced model
- Model checkpointing + early stopping
- Confusion matrix & metrics visualization
- Custom email prediction function
- Ready for deployment/inference
Email_Classification_NNFL/
│
├── data/
│ └── Emailscam.csv
│
├── notebooks/
│ └── email_classification.ipynb
│
├── models/
│ ├── best_rnn.weights.h5
│ └── best_lstm.weights.h5
│
├── results/
│ ├── confusion_matrix.png
│ ├── training_curves.png
│ └── classification_report.txt
│
├── README.md
└── requirements.txt
- Source: Mendeley Data
- Classes:
- ham: legitimate messages
- spam: unsolicited / fraudulent messages
Load dataset:
df = pd.read_csv(
"https://raw.githubusercontent.com/ayeshatofa/Email-Classification-using-RNN/main/Emailscam.csv",
encoding="latin1"
)
The notebook includes visualizations for:
- Class distribution
- Word count analysis
- Punctuation frequency
- Stopword count
- URL frequency
- Frequent spam vs ham words
Preprocessing steps include:
- Lowercasing
- Emoji removal
- Punctuation removal
- Stopword removal
- Stemming (PorterStemmer)
- Bigram generation
- Tokenization
- Sequence padding
processed_sentence = preprocessing(text)
processed_sentence = stopwordRemoval(processed_sentence)
processed_sentence = stem_text(processed_sentence)
processed_sentence = ' '.join(generate_ngrams(processed_sentence, n=2))
- Embedding layer
- SimpleRNN(64)
- Dense layers with dropout
- Embedding layer
- LSTM(64) with dropout
- Dense classifier
- Loss: binary_crossentropy
- Optimizer: adam
- Batch size: 32
- Epochs: 20
- EarlyStopping
- ModelCheckpoint
| Metric | Score |
|---|---|
| Accuracy | 0.9731 |
| Loss | 0.0949 |
| Precision (weighted) | 0.7492 |
| Recall (weighted) | 0.8656 |
| F1 Score | 0.8032 |
| Metric | Score |
|---|---|
| Accuracy | 0.9821 |
| Loss | 0.0705 |
| Precision (weighted) | 0.7492 |
| Recall (weighted) | 0.8656 |
| F1 Score | 0.8032 |
[[483 0] [ 75 0]]
⚠ Note: Both models predicted all instances as ham due to dataset imbalance and heavy preprocessing, causing 0% recall on spam.
pred = model.predict(padded_seq)
pred_class = label_encoder.classes_[int(pred[0][0] > 0.5)]
print("Predicted Label:", pred_class)
- Dataset imbalance → poor spam detection
- Heavy preprocessing removed useful patterns
- No class weighting used
- Simple RNN/LSTM architecture; lacks attention/transformers
- Use class weights (e.g., class_weight='balanced')
- Apply SMOTE or oversampling for spam
- Include metadata features (sender, domain)
- Switch to BERT, DistilBERT, or Transformer models
- Refine preprocessing (keep URLs, moderate stopword removal)
- Clone the repo:
git clone https://github.com/ayeshatofa/Email-Classification-using-RNN.git
cd Email-Classification-using-RNN
- Install dependencies:
pip install -r requirements.txt
- Open the notebook:
code CODE_2257_1002_1026.ipynb
- Run all cells to train models / test predictions.