CS 212, AI Programming 1
[TOC]
Last week we briefly looked at classifier with accuracy, the percent of test messages it got right. There are other performance metrics that matter as well precision, recall and F1. We will learn to use tools to make these performance assessments and discuss their importance. We will also learn how to improve the performance of our models.
Lets review by looking at a spam detection example. Last week we looked at similar code in the twenty newsgroups tutorial. In both of these programs, these are the things that are done:
The Spam Detection example loads the data, splits it into training and test messages, counts the words, trains the classifier and tests it. After testing, it prints three things about the classifier: its accuracy, a classification report and a confusion matrix.
This example starts where that program ends. It takes the classifier’s results and explains what the numbers mean, how they are calculated, and how to judge whether they are good.
The data is the SMS Spam Collection, collected by Tiago Almeida and José María Gómez Hidalgo. It has 5,574 real text messages, and people labeled each one as spam or ham. In the file data/sms_spam.csv in the spam Python project folder (the spam notebook reads the same data from GitHub), each row has a label and a message. The file data/SMSSpamCollection-readme.txt describes the collection and its terms of use. Only 747 of the messages (13%) are spam.
To find out how well a classifier works, you compare its answers with the right answers for the same messages. Both are needed:
You measure the classifier on the test messages, which it did not learn from. In this example the true labels and the predicted labels are lists of numbers, where 1 means spam and 0 means ham.
In your lab you will do something similar with a different data set. The true labels come from a data set that your group’s lab instructions describe, and the predicted labels come from a classifier that you train with scikit-learn.
The spam classifier’s accuracy is 98.6%. That sounds excellent, but how good is it really? Here is a “classifier” that never needs any training:
def classify(message):
return "ham"
Most messages are ham: 1,203 of the 1,394 test messages. So this classifier gets 86.3% of them right. It also never finds a single spam message, which makes it useless as a spam filter. The spam classifier’s 98.6% has to be compared with that 86.3%. And if spam were much rarer than 13%, the problem would be worse. A classifier that is 99% accurate when 99% of the messages are ham is no better than the one above.
To see how well a classifier does on the rare class (in this case the spam), you need to count the different kinds of right and wrong clasifications.
Pick one class to be the positive class. Here it is spam (label 1). Every prediction is one of four kinds:
| Predicted spam | Predicted ham | |
|---|---|---|
| Really spam | True positive (TP) | False negative (FN) |
| Really ham | False positive (FP) | True negative (TN) |
A table like this is called a confusion matrix. From the four counts you can calculate these performance metrics:
| Measure | Formula | The question it answers |
|---|---|---|
| Accuracy | (TP + TN) / all messages | What percent of all the messages did it get right? |
| Precision | TP / (TP + FP) | When it says “spam”, how often is it right? |
| Recall | TP / (TP + FN) | Of the real spam messages, how many did it find? |
| F1 | 2 × precision × recall / (precision + recall) | A number that is high only when precision and recall are both high |
Precision and recall pull against each other. A classifier that calls every message spam has perfect recall and terrible precision. One that only says “spam” for only the most obvious spam has high precision and poor recall. Which one you want depends on the cost of each kind of mistake. For a spam filter, a real message lost in the spam folder (a false positive) is usually worse than a spam message that gets through (a false negative).
You don’t need to write code to calculate these numbers. The module sklearn.metrics has a function for each one: accuracy_score, precision_score, recall_score and f1_score. You give each of them the true labels and the predicted labels. The function confusion_matrix gives you the four counts. For two classes, its result is [[TN, FP], [FN, TP]], so .ravel() flattens it into the four numbers in that order.
If a classifier never says “spam”, then TP + FP is 0, and precision (TP / (TP + FP)) would divide by zero. The argument zero_division=0 tells precision_score to return 0.0 in that case instead of printing a warning.
The Lab 4 example has two Python projects, one for each of the two examples below. SpamMetricsProgram measures the spam classifier, which has two classes, and NewsgroupsMetricsProgram measures a newsgroups classifier, which has four. In each project, main.py uses these scikit-learn functions to measure the classifier, and tune.py tunes it (see Using a Metric to Tune a Classifier).
The program main.py in SpamMetricsProgram starts with the steps of the Spam Detection program: the same data, the same 75/25 split, and a naive Bayes classifier on word counts. You can read that example to see how those steps work. Then it prints the four counts and the four performance metrics of the spam classifier, and of the “always ham” classifier for comparison. At the end it prints the classification report and confusion matrix that the Spam Detection program printed:
# Lab 4 example: measuring the spam classifier from the Spam Detection
# example.
# By Brian Bird 10/21/2026 with assistance from Claude Code
#
# Run with: uv run main.py
import csv
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics import (accuracy_score, classification_report,
confusion_matrix, f1_score, precision_score,
recall_score)
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
# ------------------------------------------------ the spam classifier
# These steps are the same as in the Spam Detection example: the same
# data, the same 75/25 split, and a naive Bayes classifier on word
# counts. The only differences are that the data is read with the csv
# module and the labels are numbers: 1 is spam and 0 is ham.
messages = []
labels = []
with open("data/sms_spam.csv", encoding="utf-8", newline="") as file:
for row in csv.DictReader(file):
messages.append(row["message"])
if row["label"] == "spam":
labels.append(1)
else:
labels.append(0)
train_messages, test_messages, train_labels, test_labels = train_test_split(
messages, labels, test_size=0.25, random_state=42)
vectorizer = CountVectorizer()
train_counts = vectorizer.fit_transform(train_messages)
test_counts = vectorizer.transform(test_messages)
classifier = MultinomialNB()
classifier.fit(train_counts, train_labels)
predicted_labels = classifier.predict(test_counts)
print(len(test_labels), "test messages,", sum(test_labels), "of them are spam")
print()
# ------------------------------------------------ measuring it
# For two classes, the confusion matrix has the four counts:
# [[TN, FP], [FN, TP]]. The positive class is 1 (spam).
matrix = confusion_matrix(test_labels, predicted_labels, labels=[0, 1])
tn, fp, fn, tp = matrix.ravel()
print("Spam classifier")
print(" counts: TP =", tp, " FP =", fp, " FN =", fn, " TN =", tn)
accuracy = accuracy_score(test_labels, predicted_labels)
precision = precision_score(test_labels, predicted_labels)
recall = recall_score(test_labels, predicted_labels)
f1 = f1_score(test_labels, predicted_labels)
print(" accuracy ", round(accuracy, 3))
print(" precision", round(precision, 3))
print(" recall ", round(recall, 3))
print(" F1 ", round(f1, 3))
print()
# ------------------------------------------------ always ham
# For comparison, a "classifier" that calls every message ham.
always_ham = [0] * len(test_labels)
matrix = confusion_matrix(test_labels, always_ham, labels=[0, 1])
tn, fp, fn, tp = matrix.ravel()
print("Always ham")
print(" counts: TP =", tp, " FP =", fp, " FN =", fn, " TN =", tn)
accuracy = accuracy_score(test_labels, always_ham)
# This classifier never says "spam", so precision would divide by zero.
# zero_division=0 makes precision_score return 0 instead of a warning.
precision = precision_score(test_labels, always_ham, zero_division=0)
recall = recall_score(test_labels, always_ham)
f1 = f1_score(test_labels, always_ham)
print(" accuracy ", round(accuracy, 3))
print(" precision", round(precision, 3))
print(" recall ", round(recall, 3))
print(" F1 ", round(f1, 3))
# ------------------------------------------------ report and matrix
print()
print(classification_report(test_labels, predicted_labels,
target_names=["ham", "spam"]))
print("Confusion matrix (rows = true, columns = predicted):")
print(["ham", "spam"])
print(confusion_matrix(test_labels, predicted_labels))
Here is the output of uv run main.py:
1394 test messages, 191 of them are spam
Spam classifier
counts: TP = 178 FP = 6 FN = 13 TN = 1197
accuracy 0.986
precision 0.967
recall 0.932
F1 0.949Always ham
counts: TP = 0 FP = 0 FN = 191 TN = 1203
accuracy 0.863
precision 0.0
recall 0.0
F1 0.0
precision recall f1-score support ham 0.99 1.00 0.99 1203 spam 0.97 0.93 0.95 191 accuracy 0.99 1394 macro avg 0.98 0.96 0.97 1394 weighted avg 0.99 0.99 0.99 1394 Confusion matrix (rows = true, columns = predicted):
[‘ham’, ‘spam’]
[[1197 6]
[ 13 178]]
The “always ham” classifier. Its accuracy is 86.3%, but its recall is 0.0: it found none of the 191 spam messages. Its precision is also reported as 0.0, but really it is undefined, because the classifier never found “spam”. When one class is rare, accuracy mostly measures how common the other class is, so it can’t tell a useful classifier from a useless one. Precision and recall look at the rare class itself.
The spam classifier. Its accuracy of 98.6% is far above the 86.3% of “always ham”, so the classifier really is useful. Precision and recall show what kind of mistakes it makes.
The confusion matrix. The confusion matrix at the bottom of the spam results shows the same four counts. Each row is a true class (what the message really is), and each column is a predicted class (what the classifier said it is). scikit-learn puts the classes in order of their labels, so ham (0) comes first and spam (1) second. That is the opposite order from the table in The Four Counts, so be careful when you read it. Here is the matrix [[1197 6] [13 178]] with each cell labeled:
| Predicted ham | Predicted spam | |
|---|---|---|
| Really ham | 1197: true negatives (TN), real messages correctly left in the inbox | 6: false positives (FP), real messages wrongly sent to the spam folder |
| Really spam | 13: false negatives (FN), spam messages that got through | 178: true positives (TP), spam messages correctly caught |
The numbers on the diagonal, from the top left to the bottom right (1197 and 178), are the right answers. The other two numbers (6 and 13) are the mistakes.
Which mistake is worse? That depends on what the classifier is for. For a spam filter, losing a real message (a false positive) is probably worse than letting some spam through (a false negative), so precision matters more. For a classifier that flags emergency messages, it would be the other way around, and recall would matter more. You choose the performance metrics to watch by thinking about what each kind of mistake costs.
The spam classifier chooses between two classes. Of the three lab assignment versions, Group B’s classifier chooses between six classes (six departments), so if you are in Group B, you will read a report like the one below. When there are more than two classes, scikit-learn calculates precision, recall and F1 for each class in turn, treating that class as the positive one.
To show what that looks like, the program main.py in NewsgroupsMetricsProgram uses the newsgroups example from the scikit-learn text tutorial in Week 3: posts from four newsgroups (computer graphics, hockey, medicine and religion), classified with the tf-idf and linear SVM pipeline from the tutorial. Two of the tutorial’s newsgroups (atheism and Christianity) are replaced with hockey and religion. To show what happens when one class is rare, the program throws away most of the religion posts: it keeps only 75 of the 377 for training and 50 of the 251 for testing. The other three newsgroups keep all of their posts, about 600 each for training and 390 each for testing.
Here is main.py:
# Lab 4 example: measuring a classifier with more than two classes,
# using the newsgroups example from the scikit-learn text tutorial
# (Week 3).
# By Brian Bird 10/21/2026 with assistance from Claude Code
#
# Run with: uv run main.py
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.linear_model import SGDClassifier
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.pipeline import Pipeline
# ------------------------------------------------ the data
# Posts from four newsgroups. Two of the tutorial's newsgroups (atheism
# and Christianity) are replaced with hockey and religion. The first run
# downloads the data, which scikit-learn keeps for later runs.
categories = ["talk.religion.misc", "rec.sport.hockey", "comp.graphics",
"sci.med"]
twenty_train = fetch_20newsgroups(subset="train", categories=categories,
shuffle=True, random_state=42)
twenty_test = fetch_20newsgroups(subset="test", categories=categories,
shuffle=True, random_state=42)
group_names = twenty_test.target_names
religion = group_names.index("talk.religion.misc")
# To show what happens when one class is rare, keep only the first 75
# of the 377 religion posts for training. The other newsgroups keep all
# of their posts.
train_posts = []
train_groups = []
religion_count = 0
for i in range(len(twenty_train.data)):
group = twenty_train.target[i]
if group == religion:
religion_count = religion_count + 1
if group != religion or religion_count <= 75:
train_posts.append(twenty_train.data[i])
train_groups.append(group)
# Do the same for testing, keeping only 50 of the 251 religion posts.
test_posts = []
test_groups = []
religion_count = 0
for i in range(len(twenty_test.data)):
group = twenty_test.target[i]
if group == religion:
religion_count = religion_count + 1
if group != religion or religion_count <= 50:
test_posts.append(twenty_test.data[i])
test_groups.append(group)
# ------------------------------------------------ the classifier
# The tf-idf and linear SVM pipeline from the tutorial.
text_clf = Pipeline([
("vect", CountVectorizer()),
("tfidf", TfidfTransformer()),
("clf", SGDClassifier(loss="hinge", penalty="l2", alpha=1e-3,
random_state=42, max_iter=5, tol=None)),
])
text_clf.fit(train_posts, train_groups)
predicted_groups = text_clf.predict(test_posts)
# ------------------------------------------------ measuring it
print("Four newsgroups")
print(classification_report(test_groups, predicted_groups,
target_names=group_names))
print("Confusion matrix (rows = true, columns = predicted):")
print(group_names)
print(confusion_matrix(test_groups, predicted_groups))
Here is the output of uv run main.py:
Four newsgroups
precision recall f1-score support comp.graphics 0.90 0.97 0.93 389 rec.sport.hockey 0.97 0.99 0.98 399 sci.med 0.93 0.90 0.92 396 talk.religion.misc 0.96 0.52 0.68 50 accuracy 0.94 1234 macro avg 0.94 0.84 0.88 1234 weighted avg 0.94 0.94 0.93 1234 Confusion matrix (rows = true, columns = predicted):
[‘comp.graphics’, ‘rec.sport.hockey’, ‘sci.med’, ‘talk.religion.misc’]
[[377 3 9 0]
[ 3 394 2 0]
[ 35 3 357 1]
[ 4 6 14 26]]
Read the report one row at a time. For talk.religion.misc, the recall is 0.52: there were 50 religion posts in the test set and the classifier found only 26 of them. The precision is 0.96: the classifier put 27 posts in talk.religion.misc (0 + 0 + 1 + 26, the last column of the confusion matrix) and 26 of them belonged there. The other classes work the same way, each one taking its turn as the positive class. In the confusion matrix each row is a true newsgroup and each column is a predicted newsgroup, so the numbers on the diagonal are the correct answers.
The rows and columns are in the same order as the list of names printed above the matrix: comp.graphics, rec.sport.hockey, sci.med and talk.religion.misc (shortened to graphics, hockey, medicine and religion below). Here is the matrix with each cell labeled:
| Predicted graphics | Predicted hockey | Predicted medicine | Predicted religion | |
|---|---|---|---|---|
| Really graphics | 377: correct | 3: graphics posts called hockey | 9: graphics posts called medicine | 0: graphics posts called religion |
| Really hockey | 3: hockey posts called graphics | 394: correct | 2: hockey posts called medicine | 0: hockey posts called religion |
| Really medicine | 35: medicine posts called graphics | 3: medicine posts called hockey | 357: correct | 1: medicine post called religion |
| Really religion | 4: religion posts called graphics | 6: religion posts called hockey | 14: religion posts called medicine | 26: correct |
With more than two classes, the words true positive, false positive and false negative only make sense after you pick one class to be the positive one. If religion is the positive class, then 26 is TP, the other three numbers in its row (4, 6 and 14) are FN, and the other three numbers in its column (0, 0 and 1) are FP. All of the other cells are TN for religion. Each class takes its turn as the positive class in the same way.
At the bottom of the report are two kinds of average:
Here the religion class is much smaller than the others: 50 test posts, compared with 389 to 399. In the weighted average it counts for only 50 of the 1234 posts, so the weighted average F1 is 0.93, close to the scores of the three big classes. In the macro average it counts as one class out of four, so its low recall pulls the macro average recall down to 0.84 and the macro average F1 down to 0.88. When the classes are not the same size, look at both averages and at each class’s own row.
The accuracy of 0.94 hides the fact that the classifier misses about half of the religion posts. It is the same problem as the “always ham” classifier: a rare class hardly changes the accuracy, so the accuracy can’t tell you how well that class is classified. The confusion matrix shows where the missed posts went: 14 of the 24 were put in sci.med. With only 75 religion posts to learn from, the classifier rarely predicts that class. When it does, it is almost always right (high precision), but it misses many of the religion posts (low recall). The best fix is to collect more examples of the rare class. When you can’t, you can at least choose a classifier’s settings with a score that counts every class equally, as the next section shows.
Performance metrics are also how you decide whether a change made a classifier better. Most classifiers have hyperparameters, settings that you choose before training, such as the alpha of naive Bayes or whether the word counts include pairs of words. GridSearchCV tries every combination of the settings you give it. For each combination it trains and tests the classifier several times with cross-validation, using only the training messages. Then it keeps the combination with the best score.
That score is another performance metric, and you choose it with the scoring argument. The default for a classifier is accuracy, which has the problem described above: when one class is rare, accuracy can hardly tell a good classifier from a bad one. For a rare class you usually want scoring="f1", which is the F1 of the positive class. For more than two classes, scoring="f1_macro" averages the F1 of every class, the same way the macro average in the report does.
These are the hyperparameters that the two tune.py programs try. In the code, each name starts with the name of a pipeline step and two underscores, so nb__alpha means the alpha setting of the step named nb.
ngram_range (counts__ngram_range for spam, vect__ngram_range for the newsgroups): which word groups CountVectorizer counts. (1, 1) counts single words only. (1, 2) also counts pairs of words that are next to each other, such as “free entry” or “call now”, so the classifier can learn from short phrases as well as single words. Pairs make many more features, so training is slower.alpha of naive Bayes (nb__alpha): how much smoothing naive Bayes does. Before calculating its probabilities, naive Bayes adds alpha to the count of every word in every class. Without smoothing, a word that never appeared in a spam message during training would make the probability of spam zero for any message that contains it. A small alpha (0.1) affects the training counts less, and a large one (1.0) affects them more (smooths them more).alpha of the linear SVM (clf__alpha): how strongly the classifier is kept simple (this is called regularization). A large alpha (0.01) keeps the classifier’s weights small, so it may miss real patterns. A small alpha (0.00001) lets it fit the training posts very closely, so it may learn details that don’t carry over to new posts. This is a different setting from the alpha of naive Bayes, even though it has the same name.class_weight (clf__class_weight): how much each training post counts. With None, every post counts the same, so a small class has little effect on what the classifier learns. With "balanced", each post counts for more if its class is small and less if its class is big, so every class has the same total effect.The program tune.py in SpamMetricsProgram puts the word counts and naive Bayes in a Pipeline, so the grid search can change settings in both. It tries a small grid with each of two scores, accuracy and F1. You can run it with uv run tune.py.
# Lab 4 example: using a performance metric to tune the spam classifier.
#
# Run with: uv run tune.py
import csv
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics import classification_report
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
# The same data and the same 75/25 split as main.py (1 is spam, 0 is ham).
messages = []
labels = []
with open("data/sms_spam.csv", encoding="utf-8", newline="") as file:
for row in csv.DictReader(file):
messages.append(row["message"])
if row["label"] == "spam":
labels.append(1)
else:
labels.append(0)
train_messages, test_messages, train_labels, test_labels = train_test_split(
messages, labels, test_size=0.25, random_state=42)
# The same steps as main.py, as a pipeline. The names "counts" and "nb"
# are used in the settings below, for example "nb__alpha" is the alpha
# setting of "nb".
pipeline = Pipeline([("counts", CountVectorizer()),
("nb", MultinomialNB())])
settings = {
# single words, or single words and word pairs
"counts__ngram_range": [(1, 1), (1, 2)],
# how much smoothing naive Bayes does
"nb__alpha": [0.1, 0.5, 1.0],
}
# Try every combination of settings, using 5-fold cross-validation on
# the training messages only. The score decides which combination is
# "best", so the choice of score matters. We try two: accuracy and F1
# (the F1 of the positive class, spam).
for score_name in ["accuracy", "f1"]:
search = GridSearchCV(pipeline, settings, scoring=score_name, cv=5)
search.fit(train_messages, train_labels)
print("Best settings when scoring by", score_name)
print(" ", search.best_params_)
print(" cross-validation", score_name, "=",
round(search.best_score_, 3))
# Only now use the test messages, once, with the model that was best
# by F1.
print()
predicted_labels = search.predict(test_messages)
print(classification_report(test_labels, predicted_labels,
target_names=["ham", "spam"]))
Here is the output of uv run tune.py:
Best settings when scoring by accuracy
{'counts__ngram_range': (1, 2), 'nb__alpha': 0.1}
cross-validation accuracy = 0.987
Best settings when scoring by f1
{'counts__ngram_range': (1, 2), 'nb__alpha': 0.1}
cross-validation f1 = 0.95
precision recall f1-score support ham 0.99 0.99 0.99 1203 spam 0.96 0.94 0.95 191 accuracy 0.99 1394 macro avg 0.97 0.97 0.97 1394 weighted avg 0.99 0.99 0.99 1394
The program tune.py in NewsgroupsMetricsProgram does the same for the newsgroups classifier, but it compares accuracy with f1_macro, and it tests the classifiers chosen with both scores. You can run it with uv run tune.py. It takes about a minute.
# Lab 4 example: using a performance metric to tune the newsgroups
# classifier, where talk.religion.misc is the rare class.
#
# Run with: uv run tune.py
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.linear_model import SGDClassifier
from sklearn.metrics import classification_report
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
# The same posts as main.py: all of the posts from three newsgroups,
# and only 75 training posts and 50 test posts from talk.religion.misc.
categories = ["talk.religion.misc", "rec.sport.hockey", "comp.graphics",
"sci.med"]
twenty_train = fetch_20newsgroups(subset="train", categories=categories,
shuffle=True, random_state=42)
twenty_test = fetch_20newsgroups(subset="test", categories=categories,
shuffle=True, random_state=42)
group_names = twenty_test.target_names
religion = group_names.index("talk.religion.misc")
train_posts = []
train_groups = []
religion_count = 0
for i in range(len(twenty_train.data)):
group = twenty_train.target[i]
if group == religion:
religion_count = religion_count + 1
if group != religion or religion_count <= 75:
train_posts.append(twenty_train.data[i])
train_groups.append(group)
test_posts = []
test_groups = []
religion_count = 0
for i in range(len(twenty_test.data)):
group = twenty_test.target[i]
if group == religion:
religion_count = religion_count + 1
if group != religion or religion_count <= 50:
test_posts.append(twenty_test.data[i])
test_groups.append(group)
# The same steps as main.py, as a pipeline, but without alpha, because
# the grid search chooses it. The names "vect" and "clf" are used in
# the settings below, for example "clf__alpha" is the alpha setting of
# "clf".
pipeline = Pipeline([
("vect", CountVectorizer()),
("tfidf", TfidfTransformer()),
("clf", SGDClassifier(loss="hinge", penalty="l2", random_state=42,
max_iter=5, tol=None)),
])
settings = {
# single words, or single words and word pairs
"vect__ngram_range": [(1, 1), (1, 2)],
# how strongly the classifier is kept simple
"clf__alpha": [0.01, 0.001, 0.0001, 0.00001],
# "balanced" counts each post of a small class for more
"clf__class_weight": [None, "balanced"],
}
# Try every combination of settings, using 5-fold cross-validation on
# the training posts only. We compare two scores: accuracy and
# f1_macro, the average of the F1 of each newsgroup. To see how the two
# choices differ, we test both tuned models on the test posts. The test
# results are only for comparing them, not for choosing the settings.
for score_name in ["accuracy", "f1_macro"]:
search = GridSearchCV(pipeline, settings, scoring=score_name, cv=5)
search.fit(train_posts, train_groups)
print("Best settings when scoring by", score_name)
print(" ", search.best_params_)
print(" cross-validation", score_name, "=",
round(search.best_score_, 3))
print()
predicted_groups = search.predict(test_posts)
print(classification_report(test_groups, predicted_groups,
target_names=group_names))
Here is the output of uv run tune.py:
Best settings when scoring by accuracy
{'clf__alpha': 0.0001, 'clf__class_weight': 'balanced', 'vect__ngram_range': (1, 2)}
cross-validation accuracy = 0.976
precision recall f1-score support comp.graphics 0.91 0.97 0.94 389 rec.sport.hockey 0.97 0.99 0.98 399 sci.med 0.96 0.90 0.93 396 talk.religion.misc 0.82 0.72 0.77 50 accuracy 0.94 1234 macro avg 0.91 0.89 0.90 1234 weighted avg 0.94 0.94 0.94 1234 Best settings when scoring by f1_macro
{'clf__alpha': 0.0001, 'clf__class_weight': 'balanced', 'vect__ngram_range': (1, 1)}
cross-validation f1_macro = 0.966
precision recall f1-score support comp.graphics 0.91 0.97 0.94 389 rec.sport.hockey 0.98 0.99 0.98 399 sci.med 0.96 0.89 0.93 396 talk.religion.misc 0.82 0.84 0.83 50 accuracy 0.94 1234 macro avg 0.92 0.92 0.92 1234 weighted avg 0.95 0.94 0.94 1234
The tuning results for the newsgroups classifier is different from the spam classifier: its religion class is rare, and the grid has one more setting, class_weight. Normally every training post counts the same, so a class with only 75 posts has little effect on what the classifier learns. The setting class_weight="balanced" makes each post of a small class count for more, in proportion to how rare the class is.
Tuning made a much bigger difference here than it did for spam. Before tuning, in the output of the newsgroups main.py, the classifier found 26 of the 50 religion posts (recall 0.52). Both tuned classifiers use a smaller alpha and class_weight="balanced", and they find many more: 36 with the settings chosen by accuracy (recall 0.72) and 42 with the settings chosen by f1_macro (recall 0.84). The two scores disagree about word pairs. The F1 of the religion class went from 0.68 to 0.77 and 0.83, and the macro average F1 from 0.88 to 0.90 and 0.92. There is a cost: the precision of the religion class went down from 0.96 to 0.82, because the classifier now puts more posts in that class that don’t belong there.
Now look at the accuracy on the test posts: it is 0.94 for both tuned classifiers, the same as before tuning. If accuracy were the only number you looked at, you would think tuning had done nothing. Keep in mind that there are only 50 religion posts in the test set, so the difference between the two tuned classifiers is 6 posts. It is a real difference but not a big one. The lesson is that the score you tune for decides which settings you get, so it should be the score that measures what you care about.
Notice that the test messages and posts were not used to choose the settings. The grid search chose them with cross-validation on the training data, and the test data was used only at the end, to see how well the tuned classifiers work. (For the newsgroups, both tuned classifiers are tested so that you can compare them.) If you chose the settings by looking at the test results, the test results would no longer tell you how well the classifier works on new messages.
These notes were drafted by Claude and revised by Brian Bird. 10/21/2026
AI Programming 1 lecture notes by Brian Bird, written in , are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.