Built to order

SMS Spam Detection using Machine Learning with TF-IDF and Naive Bayes

This project builds an SMS spam detector that classifies messages as spam or ham with TF-IDF vectorization and a Multinomial Naive Bayes model trained on the UCI SMS Spam Collection dataset of 5,574 messages. It ships with a training and evaluation notebook, a precision/recall/F1 report with confusion matrix, and a browser demo where you type any SMS and see the verdict with token explanations. The classical, inspectable pipeline keeps every decision explainable in the viva. Suitable for B.E./B.Tech final-year projects in Computer Science, IT and AI & Machine Learning.

AI & Machine Learning scikit-learnJupyter notebookPython 3NumPy · pandasTF-IDF vectorizerMultinomial Naive BayesHTML/CSS/JS demo appSMS Spam CollectionSoftware
SMS Spam Detection using Machine Learning with TF-IDF and Naive Bayes — project thumbnail preview
More project photos (2)

The problem

Spam SMS still drains attention and enables fraud: prize scams, fake bank alerts and phishing links arrive as ordinary-looking texts, and rule-based filters miss the wording variations spammers constantly invent. Machine learning treats this as a text-classification problem — learn the statistical signature of spam from thousands of labeled examples, then score new messages by how closely they match. Naive Bayes pairs naturally with TF-IDF here: TF-IDF turns each message into a weighted word vector, and Naive Bayes converts those weights into a clean probability for spam vs ham, while remaining fast enough to run in a browser and transparent enough that every decision can be traced back to individual words. Students often train a black-box model they cannot explain; this project does the opposite — a classical, inspectable pipeline with token-level log-odds explanations, so the "why" behind each verdict is visible and viva-defensible.

SYSTEM FLOW

5 components 6 connections

Live flow
Drag to explore
100%
userUser

Types or pastes any SMS into the demo app to test the classifier.

Follow the working sequence

How it works

  1. The 5,574 labeled SMS messages are loaded from the SMS Spam Collection and split into a stratified training and test set so both classes are represented.
  2. Text is preprocessed: lowercasing, tokenization on word boundaries, and stopword removal, keeping digits since phone-number fragments are genuine spam signals.
  3. A TF-IDF vectorizer converts each message into a weighted vector — common words are down-weighted, distinctive words like "prize" or "claim" are up-weighted.
  4. A Multinomial Naive Bayes classifier learns per-class word likelihoods with Laplace smoothing (alpha = 1.0) and class priors from the training vectors.
  5. The held-out test set is scored once: precision, recall, F1 and the confusion matrix are computed and written into the report.
  6. In the demo, a typed message passes through the same vectorizer and model; the app displays the verdict, class probabilities and the top contributing tokens with their log-odds.

Tech stack:

  • Python 3, scikit-learn
  • NumPy, pandas, Matplotlib
  • Jupyter Notebook (training & evaluation)
  • TF-IDF vectorizer · Multinomial Naive Bayes
  • Single-file HTML/CSS/JS demo app
  • SMS Spam Collection (UCI) dataset

Dataset & model details

  • Dataset: SMS Spam Collection — UCI Machine Learning Repository (Almeida & Hidalgo, 2011). 5,574 English SMS messages: 4,827 ham, 747 spam (approximately 13.4% spam). Tab-separated file with label and message-text columns.
  • Task: Binary text classification; input = raw SMS text, output = spam/ham label with class probabilities.
  • Model: TF-IDF vectorization (word tokens, stopword removal) feeding a Multinomial Naive Bayes classifier with Laplace smoothing (alpha = 1.0); prediction in log space.
  • Metrics: Precision, recall, F1-score per class and confusion matrix on a held-out stratified test split. Literature baselines for TF-IDF + Naive Bayes on this dataset are commonly reported in the high-90s for accuracy and high-80s to mid-90s F1, varying with split and preprocessing — reported here as literature context only, not a claim for the delivered build. Final performance depends on the split, preprocessing and configuration used in the buyer's training run.
Parameter Value
Dataset size 5,574 labeled SMS messages (4,827 ham, 747 spam)
Classes 2 (spam / ham)
Model Multinomial Naive Bayes, Laplace smoothing alpha = 1.0
Vectorizer TF-IDF, word tokens, stopword removal (design target: 5,000–10,000 features)
Train/test split 80/20 stratified (design target)
Evaluation Precision, recall, F1-score, confusion matrix (per class)
Inference Under 50 ms per message on a laptop CPU (expected)
Model artifact Approximately 1–3 MB (expected)
Demo Single-file web app, runs offline after download

Project features

  • [TF-IDF + Multinomial Naive Bayes classifier] The core model: TF-IDF vectorization over word tokens with Laplace-smoothed Multinomial Naive Bayes, implemented with scikit-learn and fully documented from preprocessing to prediction.
  • [Live classify-any-SMS web demo] Paste any message and get an instant spam/ham verdict with class probabilities, rendered as a single-file app that runs offline in the browser.
  • [Token-level explanations] Every verdict is broken down into per-token log-odds contributions, so the student can show exactly which words pushed the decision toward spam or ham.
  • [Full training notebook] Dataset loading, stratified train/test split, vectorizer fitting, model training and evaluation in one reproducible Jupyter notebook.
  • [Honest evaluation report] Precision, recall and F1-score per class plus a confusion matrix, with a written interpretation of where the model errs — typically aggressive prize-scam spam caught easily, short ambiguous messages harder.
  • [Imbalance-aware methodology] The dataset is ~13% spam, so the build treats spam recall and the precision/recall trade-off explicitly instead of leaning on misleading raw accuracy.
  • [Viva-ready theory notes] Concise write-ups of TF-IDF, Bayes' theorem, smoothing, log-space computation and the independence assumption, mapped to viva questions.

What is included

  • Complete training & evaluation Jupyter notebook
  • Trained vectorizer and model with preprocessing code
  • Live classify-any-SMS demo app (single-file, offline-capable)
  • Per-class precision/recall/F1 report with confusion matrix and interpretation
  • Project report PDF (background, TF-IDF/NB theory, methodology, results)
  • PPT presentation for final review
  • Viva Q&A preparation document (Bayes theorem, smoothing, TF-IDF, evaluation metrics)

Limitations & prerequisites

  • Trained on English SMS from 2011–2012 — modern slang, regional-language messages and emoji-heavy texts are outside the training distribution.
  • Very short or ambiguous messages (e.g. "call me now") carry weak signals and may misclassify; the token explanation shows exactly when confidence is low.
  • No accuracy figure is promised — the report documents the actual precision/recall/F1 achieved on the buyer's training run, honestly labeled.
  • Naive Bayes assumes token independence, which is a simplification; it trades some modeling power for speed and interpretability.
  • The demo is a teaching build, not a carrier-grade filter — no claim is made about real-world deployment precision.
  • Spam wording drifts over time; a model trained today degrades unless retrained on fresh labeled data (listed as future scope).

Frequently Asked Questions

Which dataset is used?

The SMS Spam Collection from the UCI Machine Learning Repository — 5,574 real labeled SMS messages (4,827 ham, 747 spam), published by Almeida and Hidalgo in 2011. It is the standard public benchmark for SMS spam classification.

Why Naive Bayes instead of a deep-learning model?

Because this project values inspectability: Naive Bayes trains in seconds, runs anywhere, and every verdict decomposes into per-token contributions the student can show in the viva. Deep models are listed as future scope, not the base build.

How does the token-level explanation work?

Each token's TF-IDF weight is multiplied by its log-likelihood ratio between the spam and ham classes. Tokens with positive contributions pushed the verdict toward spam; negative toward ham. The demo lists the strongest ones for every message.

Why not just report accuracy?

Because the dataset is only ~13% spam — a model that always predicts "ham" would score ~87% accuracy while catching zero spam. Precision, recall and F1 on the spam class are the honest metrics, and the report leads with those.

Can it classify messages in other languages?

Not as shipped — it is trained on English SMS. A custom labeled dataset in another language can be plugged into the same notebook pipeline as a customization.

Is this project suitable for a final-year project?

Yes — for Computer Science, IT and AI/ML programs. It demonstrates NLP preprocessing, TF-IDF, probabilistic classification, imbalance-aware evaluation and an interpretable live demo.

Components & software requirements
  • Python 3, scikit-learn
  • NumPy, pandas, Matplotlib
  • Jupyter Notebook (training & evaluation)
  • TF-IDF vectorizer · Multinomial Naive Bayes
  • Single-file HTML/CSS/JS demo app
  • SMS Spam Collection (UCI) dataset

Dataset & model details

  • Dataset: SMS Spam Collection — UCI Machine Learning Repository (Almeida & Hidalgo, 2011). 5,574 English SMS messages: 4,827 ham, 747 spam (approximately 13.4% spam). Tab-separated file with label and message-text columns.
  • Task: Binary text classification; input = raw SMS text, output = spam/ham label with class probabilities.
  • Model: TF-IDF vectorization (word tokens, stopword removal) feeding a Multinomial Naive Bayes classifier with Laplace smoothing (alpha = 1.0); prediction in log space.
  • Metrics: Precision, recall, F1-score per class and confusion matrix on a held-out stratified test split. Literature baselines for TF-IDF + Naive Bayes on this dataset are commonly reported in the high-90s for accuracy and high-80s to mid-90s F1, varying with split and preprocessing — reported here as literature context only, not a claim for the delivered build. Final performance depends on the split, preprocessing and configuration used in the buyer's training run.
Delivery information

Built-to-order project. Delivery timeline is shared after order confirmation based on current queue.

Support terms

Complete documentation, setup guide, and viva preparation included. Support for setup and explanation provided.

Download free synopsis (PDF)

Enter your name and email — the synopsis PDF downloads immediately. We may send occasional project updates; no spam, unsubscribe anytime.

Get a quotation