A machine-learning project that classifies SMS messages as spam or ham (legitimate messages). The project is implemented as a Google Colab notebook and compares several classification algorithms using both TF-IDF text features and handcrafted message features.
The notebook performs the following steps:
- Mounts Google Drive and loads the SMS dataset.
- Explores missing values and the class distribution.
- Creates additional features from each message.
- Splits the data into stratified training and test sets.
- Converts message text into TF-IDF unigram and bigram features.
- Balances the training data with random oversampling.
- Trains and compares ten classification models.
- Saves the best selected pipeline for later predictions.
Each message is represented by:
- Up to 5,000 TF-IDF unigram and bigram features
- Message word count
- Whether the message contains a dollar sign (
$) - Whether the message contains a number
The complete preprocessing and classifier workflow is stored in an imblearn pipeline, which ensures the same transformations are applied during training and prediction.
- Logistic Regression
- Weighted Logistic Regression
- Support Vector Classifier (SVC)
- Random Forest
- Linear SVC
- SGD Logistic Regression
- SGD Linear SVM
- Ridge Classifier
- Passive Aggressive Classifier
- XGBoost
Models are evaluated on a stratified 20% test split using the F1 score for the spam class.
| Model | F1 score |
|---|---|
| Ridge Classifier | 0.9553 |
| Linear SVC | 0.9553 |
| Passive Aggressive Classifier | 0.9550 |
| SVC | 0.9550 |
| SGD Linear SVM | 0.9521 |
| Random Forest | 0.9296 |
| Weighted Logistic Regression | 0.8959 |
| Logistic Regression | 0.8959 |
| SGD Logistic Regression | 0.8903 |
| XGBoost | 0.7802 |
Ridge Classifier and Linear SVC achieved the highest recorded F1 score. The notebook selects Ridge Classifier as the final model and saves the fitted pipeline as SMS_Spam_Detector.
Spam_SMS_Detection/
├── archive/
│ └── spam.csv
├── Spam_SMS_Detection.ipynb
└── README.md
- Python 3
- Google Colab
- pandas
- NumPy
- Matplotlib
- scikit-learn
- imbalanced-learn
- XGBoost
- joblib
Install the required Python packages in Colab or a local environment with:
pip install pandas numpy matplotlib scikit-learn imbalanced-learn xgboost joblib-
Clone this repository into the following Google Drive directory:
MyDrive/Colab Notebooks/Spam_SMS_Detection -
Confirm that the dataset is available at:
Spam_SMS_Detection/archive/spam.csv -
Open
Spam_SMS_Detection.ipynbin Google Colab. -
Run all cells in order and authorize Google Drive access when prompted.
The notebook uses a fixed random seed of 42 for the train/test split, oversampling, and supported classifiers.
The saved pipeline expects a DataFrame containing the original message and three engineered features. The notebook's prepare_message helper creates that input:
from joblib import load
model = load("SMS_Spam_Detector")
message = "Congratulations! You won $500. Reply now to claim your prize."
prediction = model.predict(prepare_message(message))[0]
print("SPAM" if prediction == 1 else "HAM")Because preprocessing is included in the fitted pipeline, no separate TF-IDF transformation is needed when predicting new messages.
The notebook expects spam.csv to contain:
v1: the target label (hamorspam)v2: the SMS message textUnnamed: 2,Unnamed: 3, andUnnamed: 4: unused columns that are removed before training