LSMA

LONDON SCHOOL OF MULTIOMICS & AI

Free course

Free course

0 of 73 lessons

Lesson 5 of 12

Module 8 · AI for biology

Measuring a model honestly

Measuring a model honestly

Measuring a model honestly

Accuracy hides the questions that matter. Read a confusion matrix, choose between recall and precision, use ROC and precision-recall curves, handle rare outcomes, and tune a model without fooling yourself.

⏱ 13 min readModule 8 · AI for biologyFree
Lesson at a glance6 steps · about 13 min
  1. Why accuracy is not enough
  2. The confusion matrix
  3. Scores, thresholds and the ROC curve
  4. Rare outcomes: when accuracy lies
  5. Tuning without fooling yourself
  6. Reporting a model honestly

Why accuracy is not enough

In Machine learning on an expression table: from patients to marker genes two models scored the same in cross-validation and then differed on new patients: 33 of 34 correct against 29 of 34. "Correct" is one number, and it hides the questions a clinician would ask first. Which patients were wrong? Were AML patients missed, or ALL patients wrongly flagged as AML? Would a different cut-off fix it? And what happens when the disease is rare, so that almost every patient is healthy?

This lesson gives you the standard tools for those questions: the confusion matrix, precision and recall, the ROC curve and its area, and the precision–recall view used for rare outcomes. It ends with tuning a model without fooling yourself. The first cell rebuilds the data and the two models from Machine learning on an expression table: from patients to marker genes.

Python
# Same data and models as Machine learning on an expression table: from patients to marker genes, in one cell
import numpy as np, pandas as pd
from sklearn.datasets import fetch_openml
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.feature_selection import VarianceThreshold, SelectKBest, f_classif
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier

d = fetch_openml("leukemia", version=1, as_frame=True, parser="auto")
X = np.log10(d.data.clip(lower=100, upper=16000)); y = (d.target == "AML").astype(int)
X_train, X_test, y_train, y_test = X.iloc[:38], X.iloc[38:], y.iloc[:38], y.iloc[38:]
spread = X_train.max() - X_train.min(); keep = spread[spread > np.log10(5)].index
X_train, X_test = X_train[keep], X_test[keep]

lr = make_pipeline(VarianceThreshold(), StandardScaler(), SelectKBest(f_classif, k=50),
                   LogisticRegression(max_iter=5000)).fit(X_train, y_train)
rf = RandomForestClassifier(n_estimators=500, random_state=0).fit(X_train, y_train)

The confusion matrix

For a yes/no question, every prediction falls into one of four boxes. Call AML the positive class, the one we are trying to detect.

  • True positive (TP): an AML patient predicted as AML.
  • False negative (FN): an AML patient predicted as ALL, a missed case.
  • False positive (FP): an ALL patient predicted as AML, a false alarm.
  • True negative (TN): an ALL patient predicted as ALL.

Laid out as a 2 × 2 table this is the confusion matrix. Two scores come straight from it. Recall, also called sensitivity, is TP ÷ (TP + FN): of the patients who really have AML, the share the model finds. Precision is TP ÷ (TP + FP): of the patients the model calls AML, the share who really have it.

Two more scores from the same table are worth knowing. Specificity is TN ÷ (TN + FP): of the patients who do not have AML, the share the model correctly clears. The false-alarm rate is its opposite, FP ÷ (FP + TN), or 1 − specificity; it is the second axis of the ROC curve below. Note too that the scores depend on which class you call positive. Had ALL been the positive class, the same predictions would give different precision and recall, so always say which class is which.

Python
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score

for name, m in [("logistic regression", lr), ("random forest", rf)]:
    p = m.predict(X_test)
    tn, fp, fn, tp = confusion_matrix(y_test, p).ravel()
    print(f"{name}: TP={tp} FN={fn} FP={fp} TN={tn} | accuracy {accuracy_score(y_test, p):.2f} "
          f"precision {precision_score(y_test, p):.2f} recall {recall_score(y_test, p):.2f}")
logistic regression: TP=13 FN=1 FP=0 TN=20 | accuracy 0.97 precision 1.00 recall 0.93 random forest: TP=9 FN=5 FP=0 TN=20 | accuracy 0.85 precision 1.00 recall 0.64
Measuring a model honestly: confusion matrix, ROC, precision, recall and imbalance · LSMA free course Two 2 by 2 grids for the 34 test patients, 14 with AML and 20 with ALL. Logistic regression: 13 true positives, 1 false negative, 0 false positives, 20 true negatives. Random forest: 9 true positives, 5 false negatives, 0 false positives, 20 true negatives. Both make no false alarms; the forest misses five AML patients. 1 · CONFUSION MATRICES, 34 TEST PATIENTS Logistic regressionpredicted AMLpredicted ALLreally AMLreally ALLTP13AML called AMLFN1AML missedFP0ALL called AMLTN20ALL called ALLrecall 13 ÷ 14 = 0.93 · precision 13 ÷ 13 = 1.00 Random forestpredicted AMLpredicted ALLreally AMLreally ALLTP9AML called AMLFN5AML missedFP0ALL called AMLTN20ALL called ALLrecall 9 ÷ 14 = 0.64 · precision 9 ÷ 9 = 1.00
Figure 1. Rows are the truth, columns the prediction; AML is the "positive" class. Both models raise no false alarms, but the random forest misses 5 of the 14 AML patients, which a single accuracy figure of 85% does not show.

Reading the output

Neither model raises a false alarm: every patient called AML really has AML, so precision is 1.00 for both. The difference is entirely in missed cases. The logistic regression misses one AML patient out of 14 (recall 0.93); the random forest misses five (recall 0.64). For a disease where a missed case means the wrong treatment, that is the number that matters, and it is invisible in "85% accuracy".

Keep the size of the test set in mind as well. With 14 AML patients, each one is 1 ÷ 14, about 7 percentage points of recall. The difference between 0.93 and 0.64 is four patients: real, and worth explaining, but a single extra mistake would move either number noticeably. Small test sets give rough scores, and reporting the counts alongside the percentages lets a reader see that for themselves.

Scores, thresholds and the ROC curve

Both models do more than say yes or no. Underneath, each gives a probability of AML, and predict simply calls everything above 0.5 AML. That cut-off, the threshold, is a choice. Lower it and the model catches more AML patients but raises more false alarms; raise it and the opposite happens.

A ROC curve (receiver operating characteristic, a name from radar engineering) shows that trade-off for every possible threshold at once: recall on one axis, the false-alarm rate on the other. The area under the curve (ROC AUC) sums it up in one number: 1.0 means some threshold separates the two groups perfectly, and 0.5 means the scores are no better than coin tossing. Another way to read it: the AUC is the chance that a randomly chosen AML patient gets a higher score than a randomly chosen ALL patient.

Python
from sklearn.metrics import roc_auc_score

for name, m in [("logistic regression", lr), ("random forest", rf)]:
    prob = m.predict_proba(X_test)[:, 1]                 # probability of AML
    print(f"{name}: ROC AUC {roc_auc_score(y_test, prob):.3f}")

prob = rf.predict_proba(X_test)[:, 1]
for t in [0.5, 0.4, 0.3]:
    p = (prob >= t).astype(int)
    print(f"random forest, threshold {t}: recall {recall_score(y_test, p):.2f}, "
          f"precision {precision_score(y_test, p):.2f}")
logistic regression: ROC AUC 0.996 random forest: ROC AUC 0.996 random forest, threshold 0.5: recall 0.64, precision 1.00 random forest, threshold 0.4: recall 0.93, precision 1.00 random forest, threshold 0.3: recall 0.93, precision 0.93

Reading the output

Both models have a ROC AUC of 0.996. The random forest ranks the patients almost perfectly: nearly every AML patient gets a higher score than every ALL patient. Its problem is the cut-off. Its AML scores are lower than the logistic regression's, so at 0.5 five of them fall below the line. At 0.4 it finds 13 of 14 with no false alarms; at 0.3 it still finds 13 but raises one false alarm.

One honest warning about this demonstration. The thresholds were compared on the test patients, to show the idea. In a real project you must choose the threshold using the training data only, for example from cross-validated probabilities, and then apply that fixed threshold to the test set once. Choosing it on the test set is another form of the leakage in Machine learning on an expression table: from patients to marker genes.

The random forest result also shows the difference between ranking and calibration. A model is well calibrated when its probabilities mean what they say: of all the patients given a probability of 0.3, about 30% really have AML. The forest ranks almost perfectly, but its scores for AML patients are pulled towards the middle, as forest probabilities often are, because each score is the share of trees voting AML and the trees rarely all agree. The ROC AUC cannot see this, since it only looks at the order of the scores. If the probabilities themselves will be used, for example to tell a patient their risk, calibration has to be checked separately, by comparing predicted probabilities with observed outcomes in groups of patients.

Rare outcomes: when accuracy lies

The leukaemia data are roughly balanced: about two ALL patients for every AML patient. Many real questions are not. A screening test might look for a disease in 3 people in every 100. Then a "model" that always says healthy is 97% accurate, and completely useless. This situation is called class imbalance. To show it clearly, the code below uses simulated data rather than a real cohort: 2,000 made-up people described by 20 measurements, about 3% of them cases.

Python
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.dummy import DummyClassifier
from sklearn.metrics import average_precision_score, balanced_accuracy_score

# Simulated screening data: about 3 cases in every 100 people
Xs, ys = make_classification(n_samples=2000, n_features=20, n_informative=5,
                             weights=[0.97], flip_y=0.01, random_state=3)
print("cases:", ys.sum(), "of", len(ys))
Xa, Xb, ya, yb = train_test_split(Xs, ys, test_size=0.5, stratify=ys, random_state=0)

for name, m in [("always 'healthy'", DummyClassifier(strategy="most_frequent")),
                ("logistic regression", LogisticRegression(max_iter=2000)),
                ("balanced weights", LogisticRegression(max_iter=2000, class_weight="balanced"))]:
    m.fit(Xa, ya); p = m.predict(Xb); pr = m.predict_proba(Xb)[:, 1]
    print(f"{name:20s} accuracy {accuracy_score(yb, p):.3f}  balanced {balanced_accuracy_score(yb, p):.3f}  "
          f"recall {recall_score(yb, p, zero_division=0):.2f}  precision {precision_score(yb, p, zero_division=0):.2f}  "
          f"PR AUC {average_precision_score(yb, pr):.2f}")
cases: 66 of 2000 always 'healthy' accuracy 0.967 balanced 0.500 recall 0.00 precision 0.00 PR AUC 0.03 logistic regression accuracy 0.968 balanced 0.530 recall 0.06 precision 0.67 PR AUC 0.22 balanced weights accuracy 0.754 balanced 0.712 recall 0.67 precision 0.09 PR AUC 0.15

What this does

  • make_classification invents a dataset; weights=[0.97] makes 97% of people belong to the first class, and flip_y=0.01 adds a little label noise, as real data have.
  • stratify=ys keeps the same 3% share of cases in both halves of the split.
  • DummyClassifier(strategy="most_frequent") always predicts the commonest class: the "always healthy" baseline every model should beat.
  • class_weight="balanced" tells the logistic regression to treat each missed case as many times worse than a false alarm, in proportion to how rare cases are.
  • Balanced accuracy is the average of the recall for each class, so the rare class counts as much as the common one; 0.5 is no better than guessing. PR AUC (here computed as average precision) is the area under the precision–recall curve; its no-skill baseline is the share of cases, about 0.03.

Reading the output

The "always healthy" baseline reaches 96.7% accuracy and finds no cases at all; its balanced accuracy of 0.500 gives it away. The plain logistic regression is barely better: 96.8% accuracy, but it finds only 6% of cases. With balanced class weights, accuracy drops to 75%, yet the model now finds two-thirds of the cases, and balanced accuracy rises to 0.71. The price is precision: only 9% of people it flags are real cases, so most alarms are false. Whether that is acceptable depends on what happens next. A cheap, safe follow-up test can absorb many false alarms; an invasive one cannot.

Class weights are one remedy for imbalance. Another is resampling: repeating the rare cases, or dropping some of the common ones, so that the training data are closer to balanced. Either way, the same rule as in Machine learning on an expression table: from patients to marker genes applies. Resample only the training data, inside each fold, and leave the test data as they are. A test set that has been rebalanced no longer reflects the people the model will meet, and its precision will look far better than it will be in practice.

Go deeper: why precision collapses when cases are rare

Imagine a test that catches 90% of cases (recall) and clears 95% of healthy people correctly. That sounds good. Now apply it to 10,000 people of whom 1% are ill: 100 are ill and 9,900 are not. The test finds 90 of the 100. It also wrongly flags 5% of the 9,900 healthy people, which is 495.

So it flags 90 + 495 = 585 people, of whom only 90 are ill: a precision of about 15%. Nothing about the test changed; only the rarity of the disease. This is why the same model can look excellent in a balanced study and disappointing in a screening programme, and why precision should always be reported together with how common the outcome was.

Tuning without fooling yourself

Most models have settings that are not learned from the data but chosen beforehand, called hyperparameters: how many genes to keep, or how strongly to restrain the logistic regression's weights (the setting C, where a small C means strong restraint, also called regularisation). Grid search tries every combination from a list and keeps the one with the best cross-validation score. Done inside the training set, it cannot see the test patients.

Python
from sklearn.model_selection import GridSearchCV, StratifiedKFold

pipe = make_pipeline(VarianceThreshold(), StandardScaler(), SelectKBest(f_classif),
                     LogisticRegression(max_iter=5000))
grid = {"selectkbest__k": [10, 50, 200], "logisticregression__C": [0.01, 1, 100]}
search = GridSearchCV(pipe, grid, cv=StratifiedKFold(5, shuffle=True, random_state=0),
                      scoring="roc_auc").fit(X_train, y_train)

print("best settings:", search.best_params_, "| CV ROC AUC", round(search.best_score_, 3))
print("test ROC AUC:", round(roc_auc_score(y_test, search.predict_proba(X_test)[:, 1]), 3),
      "| test correct:", (search.predict(X_test) == y_test).sum(), "of", len(y_test))
best settings: {'logisticregression__C': 0.01, 'selectkbest__k': 10} | CV ROC AUC 1.0 test ROC AUC: 0.989 | test correct: 27 of 34

Reading the output

The search reports a perfect cross-validation ROC AUC of 1.0 for keeping 10 genes with strong restraint (C = 0.01). On the test patients, the ROC AUC is still excellent at 0.989, but only 27 of 34 predictions are correct, fewer than the 33 of the untuned model in Machine learning on an expression table: from patients to marker genes.

Two lessons hide in that result. First, with only 38 training patients and an easy problem, several settings score a perfect 1.0, and the search simply takes the first of the tie; "best" here means "one of many equally perfect", not "truly better". Second, strong restraint squeezes the predicted probabilities towards the middle, so the ranking stays good while many AML patients fall just below the 0.5 cut-off: the threshold problem again. Tuning is worth doing, but a perfect score on a small training set is a warning, not a triumph.

Go deeper: nested cross-validation

Once a grid search has picked the best of nine settings using cross-validation, its best cross-validation score is slightly too optimistic: it is the maximum of nine noisy scores, and maxima of noisy numbers run high. With a separate test set, as here, that does not matter, because the test set gives the honest estimate.

When there are too few samples to keep a test set, the fix is nested cross-validation: an outer loop splits the data into folds for scoring, and inside each outer training part, a complete grid search with its own inner cross-validation chooses the settings. In scikit-learn this is simply cross_val_score(search, X, y, cv=outer_cv), where search is the GridSearchCV object. It takes longer to run, but every outer score comes from data the tuning never saw.

Reporting a model honestly

Put together, a trustworthy report of a classifier answers these questions:

  • How many samples, how many in each class, and where they came from.
  • How the data were split, and whether every learned step, including gene selection and tuning, happened inside the training data.
  • The confusion matrix on independent test data, not just accuracy, together with recall and precision for the class that matters.
  • ROC AUC, and PR AUC when the outcome is rare, with the no-skill baseline for comparison.
  • How the threshold was chosen, and on which data.
  • A simple baseline: "always the common class", or a single well-known marker gene. A complex model that cannot beat it has not earned its complexity.

For imbalanced data, Saito and Rehmsmeier (2015, PLOS ONE) showed that precision–recall plots are more informative than ROC curves, because a ROC curve can look excellent while most positive calls are false. Varma and Simon (2006, BMC Bioinformatics) showed that reporting the cross-validation score of the setting chosen by that same cross-validation gives biased error estimates, and recommended nested cross-validation. For models meant for patients, the TRIPOD statement (Collins et al. 2015), now extended as TRIPOD+AI (2024), lists what a prediction-model paper should report; it makes a good checklist for your own write-ups too.

Key point

Look at the confusion matrix before any single score. Report recall and precision for the class you care about, use ROC AUC for ranking and PR AUC when that class is rare, choose thresholds and settings on training data only, and always compare with the simplest possible baseline.

Further reading · Module 8scikit-learn: Metrics and scoring: every score used here, with definitions. · The TRIPOD statement: how to report a prediction model. · All reading for this course

Check your understanding

1. A test finds 9 of 14 AML patients and raises no false alarms. What are its recall and precision?

2. In a screening dataset 3% of people are cases. A model that always says 'healthy' scores about 97% accuracy. Which score exposes it?

3. A grid search reports a perfect cross-validation score on 38 patients. What is the right reaction?

Up next

Lesson 6

Deep learning on sequences: what the model actually learns

Ask BioTutor

Create a free website with Framer, the website builder loved by startups, designers and agencies.