SMS Spam Detection Pipeline

SMS Spam Detection Pipeline

SMS Spam Detection Pipeline

A step-by-step guide to building a machine learning model to classify SMS messages as spam or not-spam using a Naive Bayes classifier.

Libraries Used

This pipeline uses a few key Python libraries. Here is a quick overview of their roles:

  • pandas: Used for data manipulation and analysis.
  • scikit-learn (sklearn): The core machine learning library. It provides all the necessary tools for loading data, splitting, preprocessing text, building the model, and evaluating performance.

1. Load the Dataset

  • What the code does: It uses pandas to read a CSV file. This dataset is a classic for spam detection tasks. The `encoding='latin-1'` is specified to handle potential character issues in the dataset.
  • Behind the scenes: The dataset contains two columns: `v1` (label, 'ham' or 'spam') and `v2` (the text of the message). We will rename these for clarity.
  • Abstraction: The `pd.read_csv()` function abstracts the process of reading a structured text file from a URL.
import pandas as pd

url = "https://raw.githubusercontent.com/uciml/sms-spam-collection/master/SMSSpamCollection"
try:
    df = pd.read_csv(url, sep='\t', names=["label", "text"], encoding='latin-1')
except Exception as e:
    print(f"Error loading dataset: {e}")
    exit()

2. Split Target and Features

  • What the code does: It assigns the `label` column to the variable `y` (the labels to predict) and the `text` column to the feature variable `X`.
  • Behind the scenes: The `y` variable is binary (ham or spam). The `X` variable is text data that needs to be converted into numerical features.
  • Abstraction: The pandas methods `df[...]` and `df.drop(...)` abstract the low-level data selection and column removal.
y = df["label"]
X = df["text"]

3. Split into Training and Testing Sets

  • What the code does: The train_test_split() function from sklearn divides the `X` and `y` data into four parts: `X_train`, `X_test`, `y_train`, and `y_test`.
  • Behind the scenes: The `stratify=y` parameter is used to ensure the ratio of spam to ham messages is consistent across both the training and testing sets. This is important as spam is often a minority class. The `random_state=42` ensures a reproducible split.
  • Abstraction: This function abstracts the manual, randomized, and stratified splitting of data, a critical step for reliable model evaluation.
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, stratify=y, random_state=42
)

4. Build the Pipeline with Text Vectorizer

  • What the code does: It combines a `CountVectorizer` and a MultinomialNB classifier into a `Pipeline`.
  • Behind the scenes: The `CountVectorizer` converts the text messages into a sparse matrix of token counts. Each column represents a unique word, and each row is a message with a count of how many times that word appeared. The MultinomialNB classifier is well-suited for this type of count data. It works by calculating the probability of each word appearing in a message given the message's class (spam or ham).
  • Abstraction: The `Pipeline` class abstracts the process of vectorizing text and then training a classifier on the resulting features.
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

text_clf = Pipeline([
    ("vect", CountVectorizer()),
    ("clf", MultinomialNB())
])

5. & 6. Train and Predict

  • What the code does: The `text_clf.fit()` method trains the entire pipeline on the training data. The `text_clf.predict()` method then generates predictions on the test data.
  • Behind the scenes: When you call `fit()`, the pipeline first fits the `CountVectorizer` to `X_train` (learning the vocabulary) and transforms the data. This transformed count matrix is then used to train the MultinomialNB classifier. When you call `predict()`, the pipeline automatically vectorizes `X_test` with the same vocabulary before the trained classifier makes a prediction.
  • Abstraction: The `Pipeline` handles all these steps internally, ensuring data leakage is prevented and the workflow is smooth and consistent.
text_clf.fit(X_train, y_train)
y_pred = text_clf.predict(X_test)

7. Evaluate the Model

  • What the code does: The accuracy_score() and classification_report() functions from sklearn.metrics are used to calculate the model's performance on the test set.
  • Behind the scenes: The `classification_report` provides a detailed breakdown of precision, recall, and F1-score for each class (`ham` and `spam`), which is more informative than just accuracy alone, especially for a class-imbalanced problem.
  • Abstraction: These functions abstract the complex calculations required to compute these statistical metrics, providing a single score that summarizes model performance.
from sklearn.metrics import accuracy_score, classification_report

accuracy = accuracy_score(y_test, y_pred)

print(f"Accuracy: {accuracy:.4f}")
print("\nClassification Report:")
print(classification_report(y_test, y_pred))

Comments

Popular posts from this blog

Elevator Q-Learning Pipeline

MountainCar Q-Learning Pipeline