PeaBox Lab › PeaLang › Learn › Chapter 19 › Lesson 3

Part IV — Expert Production Engineering · Chapter 19 · Lesson 3 of 7 · 6 min read

Lesson 3: Regression and Classification Metrics

Lesson level: Advanced

Purpose: Measure model quality with metrics that reflect how errors affect the business, compare models against meaningful baselines, and report results honestly.

Learning objectives

By the end of this lesson you will be able to:

3.1 Regression metrics

Chapter 18 introduced MAE, RMSE, and scaled errors for forecasts. They apply equally to regression models. One additional metric is common:

R² (coefficient of determination) is 1 − (sum of squared errors of the model) / (sum of squared errors of predicting the mean). An R² of 0.6 means the model removes 60% of the squared error that a constant mean prediction would leave. R² can be negative on test data when the model is worse than predicting the training mean. It says nothing about whether errors are small enough for the business; always report it alongside MAE or RMSE in real units.

3.2 The confusion matrix

For a binary classifier with a chosen threshold, every prediction falls into one of four cells:

Predicted positivePredicted negative
Actual positiveTrue positive (TP)False negative (FN)
Actual negativeFalse positive (FP)True negative (TN)

Every classification metric is built from these four counts. Before choosing a metric, ask what a false positive and a false negative each cost. Flagging a loyal customer as a churn risk may cost a small discount; missing a real churner may cost a year of revenue. Missing a fraudulent payment may cost the full amount; blocking a legitimate one costs a frustrated customer and a support call.

3.3 Threshold metrics

MetricFormulaQuestion it answers
Accuracy(TP + TN) / allWhat share of predictions are correct?
PrecisionTP / (TP + FP)When the model says positive, how often is it right?
Recall (sensitivity)TP / (TP + FN)What share of actual positives does the model find?
SpecificityTN / (TN + FP)What share of actual negatives does the model correctly clear?
F12 × precision × recall / (precision + recall)Balance of precision and recall in one number

Precision and recall trade off against each other as the threshold moves: lowering the threshold flags more cases, raising recall and usually lowering precision. F1 is a convenient single number when both matter roughly equally, but it ignores true negatives and hides the trade-off. When costs are known, prefer reporting precision and recall at the chosen threshold, plus the expected cost (Lesson 5).

Illustrative listing.

from pealang.ml.metrics import confusion_matrix, precision, recall, f1

cm = confusion_matrix(y_test, label)
print(cm)                 # {"tp": .., "fp": .., "fn": .., "tn": ..}
print("precision", round(precision(y_test, label), 3))
print("recall",    round(recall(y_test, label), 3))
print("f1",        round(f1(y_test, label), 3))

3.4 Worked example: why accuracy misleads

A test set holds 1,000 customers, of whom 80 churned. At a chosen threshold the model produces TP = 56, FN = 24, FP = 64, and TN = 856.

A trivial model that predicts "no churn" for everyone scores accuracy 0.92 — higher than the real model — while finding no churners at all. Accuracy rewards the majority class. For rare-event problems, report precision, recall, and the metrics in the next section, and always show the trivial baseline beside the model.

3.5 Ranking metrics: ROC AUC and PR AUC

Threshold metrics depend on one cut-off. Ranking metrics evaluate the probabilities across all thresholds.

With strong imbalance, ROC AUC can look excellent because the huge number of true negatives keeps the false positive rate small. PR AUC shows how hard the problem really is. Report both for imbalanced problems, and always state the positive rate so readers can interpret PR AUC.

3.6 Probability metrics

When predicted probabilities are used directly — for expected-value calculations, pricing, or prioritization — their accuracy matters, not just their ranking.

Both should be compared with the score of a model that always predicts the base rate. Lesson 5 adds calibration plots, which show where probabilities are too high or too low.

3.7 Reporting results honestly

Common mistakes

Guided practice

Hands-on lab: A metrics report

Review questions

Applied challenge

A hospital operations team wants to predict missed appointments (about 12% of bookings) to schedule reminder calls. Each call costs a small amount; each missed appointment wastes a clinician slot. Choose the primary and secondary metrics, define the baseline, design the segment analysis (clinic, age band, booking lead time), and write the evaluation section of a model proposal.

Lesson summary

Metrics are statements about which errors matter. Use errors in real units and R² for regression; the confusion matrix, precision, and recall at a stated threshold for decisions; ROC AUC and PR AUC for ranking; and log loss or the Brier score for probabilities. Compare every model with a trivial baseline, report uncertainty and segment results, and never let accuracy alone describe an imbalanced problem.

Transition to Lesson 4

One train/test split gives one noisy estimate and invites over-tuning. Lesson 4 uses cross-validation to compare models reliably, regularization to control complexity, and structured search to choose settings.

Lesson status: Ready for review.

← Previous Next →