CS 212, AI Programming 1
This page describes the problem that the spam detection example solves and the strategy it uses. It is not a tutorial. The step-by-step explanations are in the notebook, and the instructions for running each version are in its readme file (see Two Versions of the Solution).
[TOC]
Most text messages are ones you want to read. Some are not: unwanted ads and scams, such as “Free entry in a weekly competition to win FA Cup final tickets”. In this example the unwanted messages are called spam, and the normal messages are called ham.
The goal is a program that reads a message and decides whether it is spam or ham. In machine learning this is a classification problem with two classes.
It is hard to solve this by writing rules. Spammers change their wording all the time, and plenty of normal messages mention prizes or phone numbers too. Instead, the program learns from examples: it is shown many messages that people have already labeled, and it works out for itself which words are signs of spam. Learning from labeled examples is called supervised learning.
The data is the SMS Spam Collection from the UCI Machine Learning Repository. It has 5,574 real text messages, and a person labeled each one ham or spam. Only 747 of the messages (about 13%) are spam, so the two classes are not the same size. The data is a CSV file with two columns, label and message.
The data was collected by Tiago A. Almeida and José María Gómez Hidalgo, who hold its copyright. Each version of the example has a copy of the data and of the original readme, which includes the license. The collection is described on the UCI Machine Learning Repository web page.
The steps follow the scikit-learn Working With Text Data tutorial, with a last step that looks inside the classifier:
On the test messages the classifier’s accuracy is about 98.6%.
There are two versions of the solution. They do the same steps.
| Version | What it is | Folder |
|---|---|---|
| Notebook | A Jupyter notebook with an explanation of each step. It runs in Google Colab or in VS Code. | SpamDetectionNotebook |
| Program | A single Python script, spam_detection.py, that you run from the command line with uv. |
SpamDetectionProgram |
Each folder has a readme file that describes its files and tells you how to run that version.
This overview was drafted by Claude Code and is waiting for review by Brian Bird. 10/21/2026
AI Programming 1 Course Materials by Brian Bird, written in , are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.