Classify message text as spam or ham (not spam) using TF-IDF features and either Multinomial Naive Bayes or a linear-kernel SVM. A Streamlit interface lets you enter text and choose the classifier.
- Lowercase text and remove punctuation and non-alphabetic characters.
- Remove English stopwords and lemmatize words using NLTK.
- Split messages into 80% training and 20% test data with
random_state=42. - Fit the TF-IDF vectorizer on training messages and transform the test messages.
- Apply random undersampling to the training features only.
- Train both classifiers, print their test metrics, and export the model artifacts.
The application operates on message text; it does not analyze email headers or attachments.
git clone https://github.com/mtaimoor1407/Email-Spam-Classifier.git
cd Email-Spam-Classifier
python -m venv .venvActivate with .venv\Scripts\activate on Windows or source .venv/bin/activate on macOS/Linux.
pip install -r requirements.txtThe preprocessing module downloads the NLTK stopwords, wordnet, and omw-1.4 resources. Internet access is required if they are not already installed.
The training script expects data/email.csv, read with Latin-1 encoding, with these columns:
| Column | Expected value |
|---|---|
Category |
ham or spam |
Message |
Message text |
Run from the repository root so imports and relative paths resolve correctly:
python -m src.classifierThis prints accuracy and a classification report for both models, then creates:
models/nb_model.pklmodels/svm_model.pklmodels/tfidf_vectorizer.pkl
streamlit run app.pyChoose Naive Bayes or SVM in the sidebar, enter a message, and select Classify. If the saved models are missing, run the training command first.
The training script reports held-out accuracy, precision, recall, and F1 through scikit-learn's classification report. Numerical results are not added here without a preserved run output.
For a stronger comparison, preserve the dataset version and class counts, check duplicates across splits, use a stratified split, and report spam-class precision/recall/F1 alongside a confusion matrix. A false positive marks a legitimate message as spam; a false negative lets spam through.
| Path | Purpose |
|---|---|
app.py |
Streamlit interface and model selection |
src/preprocess.py |
Text cleaning, stopword removal, lemmatization |
src/classifier.py |
Training, evaluation, undersampling, model export |
data/ |
Input and cleaned CSV files |
models/ |
Saved classifiers and TF-IDF vectorizer |
Email Spam Classifier Report.docx |
Project report |
English stopwords and the alphabetic-only cleaning rule limit the system's usefulness for multilingual text, code, URLs, and other meaningful symbols. Performance can change when message sources differ from the training data. This is a learning project rather than a complete email-filtering service.
Tools: Python, pandas, scikit-learn, imbalanced-learn, NLTK, joblib, Streamlit.