Project 4: Document Classification Using Machine Learning
Part 1 - Approach
1 Introduction
Email spam is a common problem because it may contain unwanted advertisements, misleading messages, and harmful content. Identifying spam automatically can help users manage their emails and avoid unwanted messages.
This project will explore how machine learning can classify emails as spam (unwanted email) or ham (legitimate email). Using a labeled email dataset, the project will examine text patterns and compare two classification models: Naive Bayes and Logistic Regression.
This Approach describes the planned data acquisition, preparation, classification methods, and evaluation. The implementation and results will be presented in the Code Base.
2 Project Objectives
The main objective is to develop and evaluate a machine learning system for email classification.
The specific objectives are:
- Data Acquisition: Obtain labeled email messages directly from an online source.
- Data Preparation: Clean and organize email text for analysis.
- Text Analysis: Explore word patterns in spam and legitimate emails.
- Document Classification: Apply Naive Bayes and Logistic Regression.
- Model Evaluation: Compare model performance and identify classification errors.
3 Research Questions
This project will answer four research questions:
Text Patterns: What differences in word usage can be found between spam and legitimate emails?
Document Classification: How accurately can machine learning classify emails as spam or ham?
Model Comparison: How do Naive Bayes and Logistic Regression compare using accuracy, precision, recall, and F1-score?
Classification Errors: How often do the models incorrectly classify spam or legitimate emails?
4 Data Source and Acquisition Plan
4.1 Data Source
The project will use the Apache SpamAssassin Public Corpus, a collection of labeled email messages used for spam filtering research.
Two datasets will be selected:
| Dataset | File Name | Description |
|---|---|---|
| Easy Ham | 20030228_easy_ham.tar.bz2 |
Legitimate email messages |
| Spam | 20030228_spam.tar.bz2 |
Unwanted email messages |
4.2 Automated Data Acquisition
The data will be retrieved directly from the SpamAssassin website through an automated HTTP connection in R.
The download.file() function will retrieve the compressed files, and untar() will extract the email messages. This process does not require manual downloads through a web browser.
After extraction, the project will:
- Read the email messages into R.
- Label each email as spam or ham.
- Combine both groups into one dataset.
- Check document counts, duplicates, and missing values.
This method will make the data acquisition process reproducible.
4.3 Planned Dataset Variables
The email messages will be organized using the following variables:
| Variable | Description | Data Type |
|---|---|---|
| Email ID | Unique identifier for each email | Character |
| Email Text | Extracted email content | Character |
| Class | Spam or Ham | Categorical |
| Text Length | Number of characters | Numeric |
| Word Count | Number of words | Numeric |
These variables will support text analysis and document classification.
5 Proposed Methodology
The project will use supervised machine learning to classify email messages.
5.1 Text Preprocessing
Before training the models, the email text will be prepared using these steps:
- Text Extraction: Extract the email body and remove unnecessary headers.
- Text Cleaning: Remove HTML tags, extra spaces, and unwanted characters.
- Text Normalization: Convert text to lowercase.
- Tokenization: Separate the text into individual words.
- Feature Extraction: Convert words into numerical features using word frequencies or TF-IDF.
The process will avoid using information that directly reveals the original spam or ham labels.
5.2 Training and Testing
The dataset will be divided into training and testing groups.
| Dataset | Percentage | Purpose |
|---|---|---|
| Training | 80% | Train the classification models |
| Testing | 20% | Evaluate predictions on unseen emails |
A fixed random seed (1234) will be used for reproducibility.
The split will maintain similar proportions of spam and ham in both groups. Text features will be prepared using the training data to prevent data leakage.
5.3 Classification Models
Two models will be compared:
Naive Bayes: A probability-based model that uses the words in an email to predict whether it is spam or ham.
Logistic Regression: A model that estimates the probability that an email is spam based on its text features. Regularization will be considered to manage the large number of words.
Both models will use the same training and testing split to support a fair comparison.
6 Model Evaluation Plan
The models will be evaluated using a confusion matrix and four classification metrics.
| Metric | Description |
|---|---|
| Accuracy | Percentage of emails classified correctly |
| Precision | Percentage of predicted spam emails that are actually spam |
| Recall | Percentage of actual spam emails correctly identified |
| F1-Score | Measure that combines precision and recall |
Spam will be treated as the positive class.
The confusion matrix will help identify two important errors:
- False Positive: A legitimate email is incorrectly classified as spam.
- False Negative: A spam email is incorrectly classified as legitimate.
Special attention will be given to false positives because important emails could be filtered incorrectly.
The results will be used to compare both models and identify areas for improvement.
7 Expected Challenges and Limitations
The project may face several challenges:
- Text Quality: Some emails contain HTML, special characters, and unnecessary information.
- Class Imbalance: Different numbers of spam and ham messages may affect performance.
- Classification Errors: Some legitimate emails may contain words commonly found in spam.
- Dataset Age: The SpamAssassin messages are older and may not represent current email patterns.
Consistent text preprocessing, balanced data splitting, and multiple evaluation metrics will help address these challenges.
8 Expected Outcomes and References
The expected outcome is a machine learning system that can classify email messages as spam or ham.
The project will compare Naive Bayes and Logistic Regression, explore common word patterns, and evaluate classification errors.
The Code Base will include data preparation, text analysis, visualizations, model predictions, and evaluation results.
This project will help explain how machine learning can support automatic email classification and identify possible limitations.
8.1 References
Apache Software Foundation. (n.d.). SpamAssassin public corpus.
https://spamassassin.apache.org/old/publiccorpus/
Apache Software Foundation. (n.d.). Apache SpamAssassin.
https://spamassassin.apache.org/
9 AI Use
ChatGPT was used to help organize the project approach, explain the assignment requirements, improve the English writing, and provide guidance on data acquisition and machine learning methods.
I reviewed the proposed methods, selected the dataset, and ran the data acquisition code in R. ChatGPT also helped improve the document structure and table formatting.
The project decisions and final content were reviewed by me. The model implementation, analysis, and results will be completed and verified in the Code Base.