Skip to content
BIASED

Classification metrics

Block 1 of 8~3 min

Two kinds of error

The topic Logistic regression ended with probabilities p(x)=σ(⟨w,x⟩+b)p(x) = \sigma(\langle w, x \rangle + b), and the reliability diagram showed how to check that they can be read literally. The classifiers of Linear classifiers and surrogate losses give scores without that promise. Classes here are {0,1}\{0, 1\}, and 1 is the positive class, the one being looked for: the sick patient, the fraud, the customer about to leave.

Two questions remain. Where should the threshold go that turns a probability into a decision? And how good is the classifier? Both need a language for errors, because the two kinds of error rarely cost the same.

Write s(x)s(x) for any score that is larger for "more positive" objects: a probability, a logit, a margin. A threshold tt turns the score into a decision:

y^=[ s(x)≥t ].\hat y = [\, s(x) \ge t \,].

At a fixed threshold every test object falls into one of four cells of the confusion matrix:

y^=1\hat y = 1 y^=0\hat y = 0
y=1y = 1 true positive (TP) false negative (FN)
y=0y = 0 false positive (FP) true negative (TN)

Take the screening test from Conditional probability and Bayes' formula: base rate 0.010.01, sensitivity 0.90.9, false-positive rate 0.090.09. Among 10,00010{,}000 people, 100 are sick, so TP=90\mathrm{TP} = 90 and FN=10\mathrm{FN} = 10. Of the 9,9009{,}900 healthy, FP=891\mathrm{FP} = 891 and TN=9,009\mathrm{TN} = 9{,}009. The row sums are fixed by the data, so only two of the four counts are free. One threshold needs two numbers, one per kind of error.

The obvious single number is accuracy, the share of correct answers, (TP+TN)/n(\mathrm{TP} + \mathrm{TN})/n. Let n+n_+ and n−n_- be the numbers of positives and negatives, and π=n+/n\pi = n_+/n the prevalence. Splitting the correct answers by true class shows what accuracy weighs:

accuracy=π⋅TPn++(1−π)⋅TNn−.\text{accuracy} = \pi \cdot \frac{\mathrm{TP}}{n_+} + (1 - \pi) \cdot \frac{\mathrm{TN}}{n_-}.

It averages the success rates on the two classes with the prevalence as weights. At π=0.01\pi = 0.01 the positives contribute one hundredth of the number. The screening test is right about 91.0%91.0\% of people. A "test" that declares everyone healthy is right about 99%99\%. So any accuracy must be read against the majority baseline max⁡(π,1−π)\max(\pi, 1 - \pi), the accuracy of always predicting the larger class.

Answers are saved on the server and tied to this browser until you sign in.

Go deeper — optional

Code and labs do not count toward the main percentage or the certificate.

  • Precision, recall and F1 from a confusion matrixoptional

    Compute TP/FP/FN → P/R/F1; match expected numbers. Self-check in the notebook.

    ~15 min · easier on a computer · Classification metrics topic read.

    Self-check in the notebook — not part of mastery or the certificate.