This project focuses on building a machine learning system that classifies SMS messages into two categories:
- Ham (legitimate messages)
- Spam (undisered messages)
- The dataset contains thousands of SMS messages labeled as Ham or Spam.
- The distribution is imbalanced (Ham >> Spam)( [Ham / Spam ] = 6, which means that the ham messages are 6 times more than spam ones.
- To make the data usable for machine learning, the text was cleaned and transformed into numerical features using TF-IDF Vectorization.
- Words were lowercased, special characters were removed, and stopwords were filtered out.
To ensure reproducibility and a clean workflow, a Pipeline was used that combines:
- TF-IDF Vectorizer → transforms raw text into numerical features.
- Model (Naive Bayes or XGBoost) → learns to classify the messages.
This way, preprocessing and model training are linked together and can be reused consistently.
- Hyperparameter tuning was done using GridSearchCV.
- For each model (Naive Bayes and XGBoost), a grid of parameters was tested.
- GridSearch ensures that the best parameters are selected based on cross-validation performance, avoiding overfitting.
- Precision (Spam): 1.0 ✅
- Recall (Spam): slightly lower than XGBoost (27 Spam messages were missed).
- Confusion Matrix:
- No Ham misclassified as Spam (0 false positives).
- Safe model, user never loses important messages.
- Precision (Spam): slightly lower than 1.0 (introduced 8 false positives).
- Recall (Spam): higher than NB (detected more Spam, only 23 missed).
- Confusion Matrix:
- Caught more Spam, but at the cost of flagging some Ham messages as Spam.
For this project, Naive Bayes was chosen because:
- It achieved perfect precision (1.0) on Spam.
- Even though XGBoost had better recall, NB ensures no false alarms.
- In practice, it is more important not to lose legitimate messages than to catch every single Spam.
- Clone the repository:
git clone https://github.com/yourusername/sms-spam-detector.git cd sms-spam-detector - Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate - Install dependencies:
pip install -r requirements.txt
- Open Jupyter Notebook:
jupyter notebook spam-detection.ipynb
- Run the UI interface:
streamlit run app.py
- Try your own SMS and see the results.