AI · 06 Artificial Intelligence

Is Your Model Any Good? Metrics That Matter

Is Your Model Any Good? Metrics That Matter

A cancer test that's 99% accurate can catch zero cancers. So how do you know if a model is any good? We measure episode four's real spam filter properly: the confusion matrix, precision, recall and F1, thresholds, PR and ROC curves (and what AUC means), class weights, MAE vs RMSE, Goodhart's law, and why offline wins need an A/B test. Every number on screen is real output from code you can run.

Chapters

Cheat sheet

Code

Try it yourself (pip install scikit-learn)
Data: unzip the SMS Spam Collection from https://archive.ics.uci.edu/dataset/228/sms+spam+collection

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import average_precision_score, classification_report, confusion_matrix, roc_auc_score
from sklearn.model_selection import train_test_split

labels, texts = [], []
for line in open("SMSSpamCollection", encoding="utf-8"):
label, text = line.rstrip("\n").split("\t", 1)
labels.append(1 if label == "spam" else 0)
texts.append(text)

X_train, X_test, y_train, y_test = train_test_split(texts, labels, test_size=0.2, random_state=0, stratify=labels)
vec = TfidfVectorizer()
X_train, X_test = vec.fit_transform(X_train), vec.transform(X_test)

for weights in (None, "balanced"): # episode 4's filter, then with class weights
model = LogisticRegression(class_weight=weights).fit(X_train, y_train)
pred = model.predict(X_test) # yes/no at the default threshold, 0.5
score = model.predict_proba(X_test)[:, 1] # the probability, used to rank messages
print(f"--- class_weight={weights}")
print(confusion_matrix(y_test, pred)) # rows: the truth, columns: the prediction
print(classification_report(y_test, pred, target_names=["not spam", "spam"], digits=3))
print(f"ROC-AUC: {roc_auc_score(y_test, score):.3f}, average precision: {average_precision_score(y_test, score):.3f}")

What we got: 114 spam caught, 35 missed, 1 good message flagged; precision 0.991, recall 0.765, F1 0.864, ROC-AUC 0.985, average precision 0.957. With balanced weights: recall 0.906, precision 0.925, same AUC.

Sources

Transcript

Last episode, we learned to keep a test set the model never sees. But what should we measure on it? Here's a cancer test that's ninety-nine percent accurate, and catches zero cancers. It's a made-up example: picture ten thousand people, where one in a hundred has the disease, and a test that simply tells everyone they're healthy. It's right for nine thousand nine hundred of them.

And it finds none of the hundred who are sick. Our spam filter from episode four scored ninety-six point eight percent. Is that good? It's a classic interview question: your model is ninety-nine percent accurate. Is it any good?

Let's measure properly.

This is Just Known. From unknown, to known. Episode six: is your model any good? Metrics that matter. Let's go back to episode four's spam filter, and exactly the same test set: one thousand one hundred fifteen messages it never saw in training.

One hundred forty-nine of them are spam. A lazy model that always says not spam is right eighty-six point six percent of the time. Ours scores ninety-six point eight. Better, but how much better? Accuracy squeezes everything into one number.

When one class is rare, a useless model can still score high. That's called the accuracy paradox. And it hides two very different kinds of mistake. Instead of one number, count four. Sort every test message by two questions: is it really spam, and did the model say spam?

That gives a two-by-two table called the confusion matrix. Spam the model caught: one hundred fourteen. Those are true positives. Positive just means the class we're hunting, not that it's good news. Spam it missed: thirty-five, the false negatives.

Good messages it wrongly flagged: just one, a false positive. And nine hundred sixty-five good messages correctly let through: true negatives. In scikit-learn, rows are the truth, columns are the prediction, and not spam comes first. From those four cells come the two metrics that matter most. Precision reads down the spam column: of everything the model flagged, how much was really spam?

One hundred fourteen out of one hundred fifteen, or ninety-nine point one percent. When our filter says spam, it's almost always right. Recall reads across the spam row: of all the real spam, how much did it catch? One hundred fourteen out of one hundred forty-nine, or seventy-six point five percent. So almost a quarter of the spam still gets through.

In medicine, recall is called sensitivity. And the lazy model's recall? Zero. Often you want one number that balances both. That's the F one score, the harmonic mean of precision and recall.

Ours is eighty-six point four percent. Why not the plain average? Take a model that flags every message as spam. Its recall is a perfect one hundred percent, and its precision just thirteen point four. The plain average says fifty-seven.

F one says twenty-four. It stays low unless both are high. The idea comes from Keith van Rijsbergen's 1979 book on information retrieval. And if one kind of mistake costs more, a variant called F-beta weights recall more heavily, or less. Remember the threshold from episode four?

Precision and recall are its two sides. Lower it, and the filter flags more. At point three, it catches one hundred thirty spam and flags seven good messages. At point one, it catches one hundred forty-four, but flags ninety-one. Recall climbs to almost ninety-seven percent, while precision falls to sixty-one.

That's the seesaw: push one up, and the other usually comes down. So which threshold? Choose it on validation data, as in the last episode, never on the test set. Tuned that way, ours lands at about point two two: one hundred thirty-five caught, and fourteen flagged. Now sweep every threshold at once, and plot precision against recall.

Each point on this curve is one threshold. That's the precision-recall curve. The top right corner is perfect: catch all the spam, flag no good messages. Our curve hugs the top until it has caught about eighty-seven percent of the spam, and then precision falls off a cliff. A model that guesses at random sits flat at the spam share, thirteen point four percent.

To sum up the whole curve in one number, we use average precision, roughly the area under it. Ours is point nine five seven. The other famous curve is the ROC curve. It plots the true positive rate, which is just recall, against the false positive rate: the share of good messages wrongly flagged. Ours shoots straight up the left side.

At the default threshold, it catches seventy-six percent of the spam while flagging a tenth of a percent of the good messages. The diagonal is a coin flip. The name, receiver operating characteristic, comes from World War Two era radar: telling real signals from noise. It later spread to medicine, and to machine learning. Its summary is the area under the curve, the AUC, and it has a neat meaning.

Pick a random spam message and a random good one. The AUC is the chance that the spam gets the higher score. We checked every pair, almost one hundred forty-four thousand. Spam won ninety-eight and a half percent of them: exactly our AUC, point nine eight five. Which curve should you trust?

Try this: pretend good messages were ten times more common, by counting each one ten times. The ROC curve doesn't move at all, because each of its rates is measured within one class. But precision collapses. At a threshold of point three, seven false alarms become seventy, and precision falls from ninety-five percent to sixty-five. So when positives are rare, the precision-recall curve shows what ROC hides: how many of your alarms are false.

A 2015 study made that case. But it's a nuance, not a law: a 2024 paper argued it isn't automatically the better way to compare models. Now, episode four's fix. With balanced class weights, the filter catches one hundred thirty-five spam and flags eleven good messages. Recall jumps from seventy-six to ninety-one percent, precision dips from ninety-nine to ninety-two and a half, and F one rises from eighty-six to ninety-one and a half.

But the AUC hasn't changed: point nine eight five for both. The weights barely changed which messages look spammier. Mostly, they moved the threshold. At their best thresholds on this test set, both models land on exactly the same point. So far, every answer was yes or no.

What about predicting a number, like our delivery times from episode two? Mean absolute error, or MAE, averages the size of the misses: our line is off by point nine five minutes on average. Root mean squared error, RMSE, squares the misses, averages them, then takes the square root: one point one seven minutes. Both are in minutes, so they're easy to explain. Now add episode three's outlier, two kilometers in sixty minutes, and keep the same line.

MAE rises about five times, to five point one five. RMSE jumps twelve times, to fourteen point three, because that one squared miss is over ninety-nine percent of the total. RMSE punishes big misses. Which brings us to what can go wrong: optimizing the wrong metric. Every single metric has a cheat.

Always saying not spam scores eighty-six point six percent accuracy. Always saying spam gets perfect recall. And flagging only the one message you're surest about gets perfect precision, while catching under one percent of the spam. This is Goodhart's law, named after the economist Charles Goodhart. In the anthropologist Marilyn Strathern's words: when a measure becomes a target, it ceases to be a good measure.

So pick your metric from the real cost of each mistake, and always watch at least two that pull against each other, like precision and recall. One more trap. Everything we measured today is offline: on saved data, before anyone uses the model. Offline metrics are fast and cheap, but they're stand-ins for what you actually care about. So before a big launch, many teams run an A-B test.

Real users are split at random: half keep the old filter, half get the new one. Then you compare what matters, like how often people rescue real messages from their spam folder. Random assignment is what lets you say the new model caused the difference. And offline wins don't always survive it. You can run all of this yourself, in about twenty lines of scikit-learn.

The code is in the description. It rebuilds episode four's filter with the same split, then prints the confusion matrix and the classification report: precision, recall, and F one for each class, plus support, the number of real examples in each. Then the ROC AUC and average precision. The loop runs it twice: once as in episode four, and once with balanced class weights. Watch recall climb, precision dip, and the AUC stay put.

Interview corner. Your model is ninety-nine percent accurate, but only one percent of cases are positive. Is it any good? And what would you report instead? Pause here, and try answering in thirty seconds.

Here's a strong answer. Not necessarily: a model that always predicts the majority class would also score ninety-nine percent. So look at the positive class, in a confusion matrix. Precision asks: of the cases we flagged, how many were right? Recall asks: of all the real positives, how many did we catch?

F one combines them, and the threshold trades one against the other. To compare models across every threshold, use the ROC AUC, which measures ranking. When positives are rare, also report average precision, whose baseline is the positive rate. And pick the threshold on validation data. To stand out, say the metric should come from the cost of each mistake, a missed cancer versus a false alarm, and that offline wins need an A-B test.

Let's recap. With imbalanced classes, accuracy alone can lie. The confusion matrix splits the mistakes, precision and recall measure them, and the threshold trades one for the other. ROC and precision-recall curves show every threshold at once. For numbers, MAE is the typical miss, and RMSE punishes the big ones.

And no single metric is the goal. So far, every model has drawn a straight line, or a straight cut through the data. Next: a model that asks a series of yes-or-no questions, and then hundreds of them voting together. That's episode seven: decision trees, random forests, and boosting. This is Just Known.

From unknown, to known.

Note: the spam numbers are real (text messages, not emails); the cancer test is a stated hypothetical; the deliveries are made-up data from a stated rule. Pictures marked "illustration" are not data.
Narration voice generated with Google Gemini text-to-speech.