This project builds a text classification model to detect whether an SMS message is spam or ham (not spam) using Naive Bayes and TF-IDF vectorization. It is a classic NLP use case and demonstrates how probabilistic classifiers can be used to flag unwanted messages.
To classify SMS messages as spam or ham using machine learning, and evaluate model performance through classification metrics and confusion matrix analysis.
- Source: Kaggle - SMS Spam Collection
- Total Samples: 5,572 messages
- Target Variable:
spam: Unwanted messagesham: Legitimate messages
- Format:
label: spam/hammessage: the text content of the SMS
- Checked class distribution (spam vs ham)
- Visualized frequent spam/ham words using word clouds
- Observed that spam messages tend to use promotional terms, numbers, and URLs
- Removed punctuation, stopwords, and converted text to lowercase
- Encoded labels:
ham → 0,spam → 1 - Applied TF-IDF Vectorizer to transform messages into numerical features
- Split data into training and test sets with stratification
- Multinomial Naive Bayes: Ideal for discrete features like word counts or TF-IDF scores
- Trained using scikit-learn’s
MultinomialNB
| Metric | Ham (0) | Spam (1) | Interpretation |
|---|---|---|---|
| Precision | 0.95 | 1.00 | No ham misclassified as spam |
| Recall | 1.00 | 0.62 | 38% of spam messages were missed |
| F1-Score | 0.97 | 0.76 | Spam recall could be improved |
| Accuracy | – | – | 95.16% overall |
Confusion Matrix:
[[903 0]
[ 50 81]]
- ✅ True Ham: 903
- ❌ Missed Spam: 50
- ✅ Correct Spam: 81
predict_message("Congratulations! You've won a free vacation! Reply NOW to claim.")
# Output: 'Spam'The model takes any SMS input and returns a prediction: Spam or Ham.
# 1. Clone or download the repo
# 2. Install dependencies
pip install pandas numpy scikit-learn matplotlib seaborn wordcloud
# 3. Run the Jupyter Notebook
# 4. Use predict_message() function to test your own texts- Improve recall for spam class by tuning alpha or using other classifiers
- Experiment with n-grams or additional features
- Deploy with Streamlit or Flask for interactive UI
- Use deep learning (LSTM, BERT) for more advanced performance
- GitHub: archangel2006