Skip to content
Laviyx Learn

Try

Final-year project

SMS Spam Detection with Machine Learning

A Python machine learning project that classifies SMS messages as spam or not spam with TF-IDF and a linear model, evaluated properly on imbalanced data and served through a small web app.

Intermediate 6–10 weeks 2–3 studentsUpdated

Pythonpandasscikit-learnTF-IDFFastAPI or Flask

The problem

Spam messages waste time and many try to trick people into sharing bank details or OTPs. A spam filter reads a message and predicts whether it is spam, so it can be moved away before the user sees it.

A public dataset to start with is the SMS Spam Collection from the UCI Machine Learning Repository: about 5,500 English messages labelled spam or ham (not spam), of which only around 13% are spam. That imbalance is the most important thing to handle well.

Features to build

  • Load and explore the dataset: class balance, message length and common words
  • Clean text: lowercase, remove extra spaces, keep numbers and links as useful signals
  • Turn text into features with TF-IDF
  • Train and compare at least two models, for example Multinomial Naive Bayes and Logistic Regression
  • Evaluate with precision, recall, F1 and a confusion matrix on a held-out test set, not just accuracy
  • Save the trained pipeline and load it in a small web app or API that classifies a typed message
  • Show which words most push a message towards spam

Build plan

  1. 1

    Understand the data

    Load the file with pandas, check for duplicates and missing values, and look at the spam percentage and examples of each class.

  2. 2

    Split first

    Create a stratified train and test split before any fitting, so the test set keeps the same spam ratio and stays unseen.

  3. 3

    Build a pipeline

    Put TfidfVectorizer and the classifier in one scikit-learn Pipeline, so the vectorizer is fitted only on training data.

  4. 4

    Compare models

    Use cross-validation on the training set with F1 as the score, and tune a few settings such as n-gram range and regularisation strength.

  5. 5

    Evaluate honestly

    Report precision, recall and F1 for the spam class and the confusion matrix on the test set. Explain the cost of each mistake.

  6. 6

    Serve the model

    Save the pipeline with joblib and load it in a FastAPI or Flask route that returns the label and probability.

  7. 7

    Report

    Include the data analysis, the comparison table, error analysis of wrong predictions, and limitations.

Learn these first

Each part of the project is taught in one of these free lessons.

Make it stand out

  • Collect and label your own Indian SMS samples (with consent) and test on them
  • Handle Hinglish and Telugu-English mixed messages
  • Compare with a sentence-embedding model
  • Let users report wrong predictions and use them for retraining

Viva and interview questions

Try to answer each one aloud before you open it.

  • Why is accuracy not a good metric here?

    Only about 13% of messages are spam. A model that predicts 'not spam' for everything gets about 87% accuracy and catches no spam at all. Precision, recall and F1 for the spam class show how well it really works.

  • What is TF-IDF?

    Term frequency–inverse document frequency. Each word's weight grows with how often it appears in a message and shrinks if it appears in many messages, so common words like 'the' matter little and distinctive words like 'prize' matter more.

  • Which mistake is worse, a false positive or a false negative?

    A false positive hides a real message, which can be costly, for example a bank OTP. A false negative lets spam through. For an SMS filter, false positives are usually worse, so we favour high precision for spam.

  • Why do you fit TF-IDF only on the training data?

    Fitting it on all data lets information from the test set, like its vocabulary and word counts, leak into training. Then the test score is too optimistic. A Pipeline keeps the fitting inside the training data.

  • Why did Naive Bayes work well even though its assumption is wrong?

    It assumes words are independent given the class, which is not true. But for classification it only needs the right class to score highest, and with many word features that usually still happens. It is also fast and works well with little data.

What to put in your project report

  1. Abstract: the problem, your solution and the result in one paragraph.
  2. Introduction and problem statement: who has the problem and why it matters.
  3. Requirements: the features (functional) and qualities such as speed and security (non-functional).
  4. Design: architecture diagram, database tables or data flow, and screen layouts.
  5. Implementation: the main modules and the most interesting code, explained.
  6. Testing: the test cases you ran, with inputs, expected and actual results.
  7. Results: screenshots or numbers that show the project working.
  8. Conclusion and future scope: what you achieved and what you would add next.

Your college may give its own format. Follow that first, and use this list to check nothing is missing.

All project ideas →