Email/SMS Spam Classifier is a machine learning project that detects whether an incoming text message is spam or ham (not spam). The solution demonstrates an end-to-end workflow: dataset cleaning, exploratory data analysis, NLP preprocessing & model training and validation.
- Predicts whether a message is spam or ham in real time
- Text preprocessing with tokenization, stopword removal, and stemming
- Feature extraction using TF-IDF vectorization
- Model built with Multinomial Naive Bayes and compared against multiple classifiers
- Simple Streamlit UI for interactive message classification
- Pickled model and vectorizer for easy deployment
Spam filtering is a practical and high-impact application of natural language processing and classification. This project addresses the challenge of protecting users from unwanted or malicious messages while preserving legitimate communication. It is suitable for personal assistants, email clients, messaging platforms, and cybersecurity proof-of-concept systems.
The dataset is provided in spam.csv with the following structure:
v1: label (hamorspam)v2: raw text message content
This dataset is commonly associated with the SMS Spam Collection dataset, originally sourced from UCI Machine Learning Repository. It includes a natural class imbalance between ham and spam messages.
- Python
- pandas
- numpy
- scikit-learn
- nltk
- Streamlit
- matplotlib
- seaborn
- wordcloud
- xgboost (exploratory model comparison)
The notebook spam_classifier.ipynb follows a structured workflow:
- Data loading and initial inspection
- Data cleaning
- Drop unused columns
- Convert labels to numeric format
- Remove duplicates
- Exploratory Data Analysis (EDA)
- Target distribution
- Text length, word count, sentence count
- Histograms, pair plots, and word clouds
- Text preprocessing
- Lowercasing
- Tokenization
- Removing non-alphanumeric tokens
- Stopword removal
- Stemming using Porter Stemmer
- Feature engineering
- Vectorization using
TfidfVectorizer
- Vectorization using
- Model training and evaluation
- Naive Bayes models: Gaussian, Multinomial, Bernoulli
- Additional classifiers for comparison: SVC, Logistic Regression, Decision Tree, Random Forest, AdaBoost, Bagging, Extra Trees, Gradient Boosting, XGBoost
- Model selection based on performance, with emphasis on precision due to class imbalance
- Serialization of the final vectorizer and model to
vectorizer.pklandmodel.pkl
The final Multinomial Naive Bayes model trained on TF-IDF features achieved strong results in the notebook analysis:
- Accuracy: ~97.10%
- Precision: 1.00
These metrics indicate the model is highly effective at identifying spam while minimizing false positives in the test set.
- Clone the repository:
git clone https://github.com/your-username/email-sms-spam-classifier.git
cd email-sms-spam-classifier- Create and activate a virtual environment:
python -m venv venv
venv\Scripts\activate- Install required packages:
pip install pandas numpy scikit-learn nltk streamlit matplotlib seaborn wordcloud xgboost- Download required NLTK data:
python -c "import nltk; nltk.download('punkt'); nltk.download('stopwords')"- Run the Streamlit app:
streamlit run app.py- Open the Streamlit app in your browser using the local URL displayed in the terminal.
- Enter an email or SMS message into the text area.
- Click the Predict button.
- View the classification result:
SpamorNot Spam.
├── app.py
├── model.pkl
├── spam.csv
├── spam_classifier.ipynb
├── vectorizer.pkl
└── README.md
- Add support for email-specific metadata such as sender, subject, and URLs
- Improve preprocessing with lemmatization instead of stemming
- Use advanced NLP models like BERT, DistilBERT, or transformer-based embeddings
- Tune hyperparameters using cross-validation and grid search
- Add input validation and rich UI feedback in Streamlit
- Extend support to multilingual spam detection
- SMS Spam Collection dataset (UCI Machine Learning Repository)
- Streamlit for fast and easy deployment of Python apps
- scikit-learn and NLTK for machine learning and NLP utilities
This project is released under the MIT License.