CS 212, AI Programming 1

Example for Lab 4: Measuring and Improving a Classifier

Example for Lab 4

This example builds on the Spam Detection example. It takes the results of the spam classifier from that example and uses scikit-learn to calculate its performance metrics: accuracy (the percent correct), precision, recall and F1. It also prints a classification report and a confusion matrix. A second example, adapted from the four-newsgroups example in the scikit-learn text tutorial (Week 3), shows what the report looks like when there are more than two classes. For each example, a second program, tune.py, uses a grid search to tune the classifier with a performance metric as the score.

The explanations of what the numbers mean and how they are calculated are in the lecture notes, Classifier Metrics. Read them first, then run the code.

Contents

[TOC]

The Code

There are two examples, the spam classifier and the newsgroups classifier, and two versions of the code for each: a Python project and a Jupyter notebook. To get them all, download them as a zip file and unzip it. The unzipped folder has three subfolders:

Folder What it is
SpamMetricsProgram A Python project that measures and tunes the spam classifier. You run it from the command line with uv
NewsgroupsMetricsProgram A Python project that measures and tunes the newsgroups classifier, which has four classes
ClassifierMetricsNotebook Two Jupyter notebooks that do the same steps as the two projects, with an explanation of each one

Running the Python Projects

You need uv. It reads pyproject.toml and installs scikit-learn the first time you run something. Both projects have these files:

File What it does
main.py Trains the classifier and measures it with scikit-learn’s metrics functions
tune.py Uses GridSearchCV to tune the classifier, comparing two scores: accuracy and an F1 score
pyproject.toml The project settings and the packages it needs
uv.lock The exact package versions that uv installed when this was written
README.md A short description of the project

SpamMetricsProgram also has a data/ folder with the SMS Spam Collection data file and its readme.

To run either project, open a terminal in its folder and run these commands:

uv run main.py
uv run tune.py

The spam programs have to run from inside the SpamMetricsProgram folder, because they open data/sms_spam.csv using a relative path, and Python starts looking in the folder where the program is running. The first run of the newsgroups programs downloads the newsgroups data, so it needs an internet connection. The newsgroups tune.py takes about a minute.

Running the Jupyter Notebooks

The ClassifierMetricsNotebook folder has two notebooks: SpamMetrics.ipynb for the spam classifier and NewsgroupsMetrics.ipynb for the newsgroups classifier. Start with the spam notebook. Both download their data, so they need an internet connection.

Experimenting

These lab materials were drafted by Claude Code and revised by Brian Bird. 10/21/2026


Creative Commons License AI Programming lab instructions by Brian Bird, written in are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.