Lecture 5 · Interactive lab

Real Data Lab

Explore all 5,572 real text messages: look inside, test word clues, compare groups and find duplicates

Introduction to Data Science and Machine Learning
Md Mahbub E Noor · University of Barishal
All labs

Look inside: rows, columns and single messages

The full SMS Spam Collection is built into this page: 5,572 real messages. Look before you model.

df.shape
matching rows

Read one message: df.loc[row, "message"]

The table

What to notice

  • The numbers on the left are the index: row labels, not positions.
  • After drop_duplicates() the index has gaps, so df.loc[7] may no longer exist.
  • Long messages are cut with ... on screen only; the full text is still there.

Try this

Word clues: is this word a good sign of spam?

For any word, see what share of spam and of normal (ham) messages contain it, exactly as str.contains and groupby compute it.

Share of messages containing it

Your comparison (best spam clue first)

Examples that contain it

What to notice

  • A good clue is common in spam and rare in ham. Being common in spam is not enough.
  • str.contains finds text inside words too: "free" also matches "freedom".
  • By default the text is a pattern (a regular expression): "." means any character, "(" is an error. Tick regex=False to search literally.

Try this

Group and compare: spam messages are longer

groupby("label") splits the table into a spam group and a ham group, then summarises each. Then try the simplest possible model: a single length rule.

Message length by label

A one-number model: can message length alone find spam?

Accuracy of the rule
Always say "ham"
Spam caught
Ham wrongly flagged

What to notice

  • The two groups overlap: many ham messages are long, some spam is short.
  • The mean is pulled up by a few very long ham messages; the median is less affected.
  • A clear difference in averages is not enough to classify: no "longer than" rule beats always saying ham. Lecture 6 uses every word instead.

Try this

Duplicates: the same text, sent many times

Spammers send one text to many people, and some everyday replies repeat too. See what repeats, and why duplicates make a test unfair.

Most repeated messages

Why it matters: a split with duplicates

Split the messages at random: 80% to study (train), 20% for the exam (test). Count how many exam messages also appear, word for word, in the study set.

Test messages
Already seen in training

What to notice

  • duplicated() marks the 2nd, 3rd, … copy; the first copy is kept.
  • With duplicates, some exam questions were already in the study notes: the score looks better than it really is.
  • Dropping duplicates makes the test honest. Lecture 12 calls this problem leakage.

Try this

Works offline: no internet needed. The whole dataset is built into this page. Every number is calculated the same way as pandas and matplotlib and was checked against the real libraries, so it matches what you will see in Jupyter or Colab.