Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

SMS Spam Detection

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.

Project workflow

The notebook performs the following steps:

  1. Mounts Google Drive and loads the SMS dataset.
  2. Explores missing values and the class distribution.
  3. Creates additional features from each message.
  4. Splits the data into stratified training and test sets.
  5. Converts message text into TF-IDF unigram and bigram features.
  6. Balances the training data with random oversampling.
  7. Trains and compares ten classification models.
  8. Saves the best selected pipeline for later predictions.

Features

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.

Models evaluated

  • 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

Results

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.

Repository structure

Spam_SMS_Detection/
├── archive/
│   └── spam.csv
├── Spam_SMS_Detection.ipynb
└── README.md

Requirements

  • 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

Running the notebook

  1. Clone this repository into the following Google Drive directory:

    MyDrive/Colab Notebooks/Spam_SMS_Detection
    
  2. Confirm that the dataset is available at:

    Spam_SMS_Detection/archive/spam.csv
    
  3. Open Spam_SMS_Detection.ipynb in Google Colab.

  4. 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.

Making predictions

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.

Dataset format

The notebook expects spam.csv to contain:

  • v1: the target label (ham or spam)
  • v2: the SMS message text
  • Unnamed: 2, Unnamed: 3, and Unnamed: 4: unused columns that are removed before training