An intelligent SMS spam detection system that uses machine learning to classify messages as spam or legitimate (ham). The system combines TF-IDF text vectorization, dimensionality reduction, handcrafted linguistic features, and XGBoost classification to achieve high accuracy.
This project addresses the growing problem of SMS spam by building an automated, data-driven detection system. Traditional rule-based filters struggle with evolving spam patterns, so we use machine learning to learn distinguishing patterns automatically.
- High Accuracy: Achieves 98.5% accuracy on test data
- Low False Positives: Maintains precision of 97.8%
- Robust Performance: AUC score of 99.1%
- Real-time Classification: Lightweight model suitable for production deployment
- Comprehensive Feature Engineering: Combines text and statistical features
- Saif Mohamed
- Muhab Abdelraouf
- Adham Tamer
- Ahmed Sharif
- Ahmed Ramadan
- Amr Hamoda
| Metric | Score |
|---|---|
| Accuracy | 98.5% |
| Precision | 97.8% |
| Recall | 90.6% |
| AUC | 99.1% |
Confusion Matrix:
[[963 3]
[ 14 135]]
- Python 3.x
- scikit-learn: Feature extraction and preprocessing
- XGBoost: Gradient boosting classifier
- NLTK: Text tokenization and stopword removal
- Pandas & NumPy: Data manipulation
- TQDM: Progress tracking
sms-spam-detection/
โโโ data/
โ โโโ spam.csv # Dataset (not included, see below)
โโโ notebooks/
โ โโโ sms_spam_detection.ipynb # Full implementation notebook
โโโ src/
โ โโโ __init__.py
โ โโโ preprocessing.py # Text cleaning and feature engineering
โ โโโ model.py # Model training and prediction
โ โโโ utils.py # Helper functions
โโโ models/
โ โโโ trained_model.pkl # Saved model (generated after training)
โโโ requirements.txt # Project dependencies
โโโ README.md # This file
โโโ LICENSE # Project license
- Python 3.7 or higher
- pip package manager
- Clone the repository:
git clone https://github.com/yourusername/sms-spam-detection.git
cd sms-spam-detection- Install dependencies:
pip install -r requirements.txt- Download NLTK data:
python -c "import nltk; nltk.download('punkt'); nltk.download('stopwords')"This project uses the SMS Spam Collection Dataset.
Download the dataset:
- Download
spam.csvfrom the link above - Place it in the
data/directory
from src.model import SMSSpamDetector
# Initialize and train
detector = SMSSpamDetector()
detector.load_data('data/spam.csv')
detector.train()
# Evaluate
detector.evaluate()# Predict single message
message = "Congratulations! You've won a free iPhone. Click here to claim."
result = detector.predict(message)
print(f"Prediction: {result['label']}")
print(f"Spam Probability: {result['probability']:.4f}")Open and run notebooks/sms_spam_detection.ipynb for the complete implementation with visualizations and detailed explanations.
- Text cleaning (URL removal, special characters, lowercase conversion)
- Tokenization and stopword removal
Text Features:
- TF-IDF vectorization (top 3,000 features)
- Dimensionality reduction using Truncated SVD (200 components)
Handcrafted Features:
- Message length (character count)
- Number of words
- Number of sentences
- Average word length
- Stopword ratio
- Algorithm: XGBoost Classifier
- Hyperparameters:
- n_estimators: 300
- max_depth: 8
- learning_rate: 0.15
- Custom threshold: 0.3 for spam classification (optimized for recall)
Multiple metrics ensure comprehensive performance assessment:
- Accuracy, Precision, Recall
- ROC-AUC score
- Confusion matrix analysis
This system can be integrated into:
- Mobile network operator messaging pipelines
- Smartphone messaging applications
- Email filtering systems
- Customer support platforms
The lightweight architecture enables real-time processing with minimal latency.
- Deep learning models (LSTM, BERT)
- Multilingual spam detection
- Active learning for continuous improvement
- Web API for easy integration
- Mobile app demonstration
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
For questions or feedback, please open an issue in this repository.
- SMS Spam Collection Dataset from UCI Machine Learning Repository
- scikit-learn and XGBoost communities
- NLTK project for natural language processing tools