🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101

Spam message classifier

Classify SMS or emails as spam or not using TF-IDF and Naive Bayes, with attention to precision and recall.

Beginner⏱ 6–8 hoursđŸĒœ 7 stepsPythonscikit-learnNLP basics

What you'll learn

  • ◆Text vectorisation with TF-IDF
  • ◆Naive Bayes and logistic regression
  • ◆Precision, recall and the confusion matrix

What you'll build

  • ✓Cleans text
  • ✓Two models compared
  • ✓Confusion matrix and classification report
  • ✓Try-it box for any message

Step-by-step guide

  1. 1

    Dataset

    The UCI "SMS Spam Collection" dataset (about 5,500 labelled messages).

  2. 2

    Look at the balance

    Only ~13% are spam, so accuracy alone is misleading; plan to use precision and recall.

  3. 3

    Vectorise

    TfidfVectorizer(lowercase=True, stop_words="english", ngram_range=(1, 2)) inside a Pipeline.

  4. 4

    Train

    MultinomialNB and LogisticRegression(class_weight="balanced"); stratified train/test split.

  5. 5

    Evaluate

    classification_report and ConfusionMatrixDisplay; discuss which error is worse (real message in spam folder).

  6. 6

    Threshold

    Use predict_proba and pick a threshold that keeps precision for spam above 98%.

  7. 7

    Demo

    A Streamlit text box that shows the prediction and probability.

🚀 Make it stand out

  • +Explain predictions with top weighted words
  • +Try a small transformer model
  • +Hindi/Hinglish messages