ContentsChapter 5 · Classification3 of 3

5.3

Evaluation metrics

In this chapter
  1. 5.1Logistic regression
  2. 5.2Naive Bayes
  3. 5.3Evaluation metrics
Before you readOnly one email in a hundred is spam. A filter never flags anything and is 99% accurate. What did accuracy miss?
Watch: ROC and AUC, Clearly Explained! · StatQuest with Josh Starmer

Test on labelled examples the model did not train on. Call spam the positive class. For a chosen threshold, the confusion matrix counts predictions against actual labels. A false positive (FPFP is a good message incorrectly flagged as spam. A false negative (FNFN is spam that gets through. The correct predictions are true positives (TPTP and true negatives (TNTN

020406080100Predicted labelSpamNot spamActual labelSpamNot spam3TP1FN1FP7TN
Accuracy: 0.83Precision: 0.75Recall: 0.75False positive rate: 0.13
Filled dots are spam; hollow dots are good messages. Scores on the line or to its right are flagged. Lower the threshold and watch the counts move from the right column to the left.

Accuracy counts all correct predictions: (TP+TN)/N(TP+TN)/N where NN is the total number of examples. If spam is rare, accuracy can look high even when the filter catches no spam.

If you want one number that is high only when both precision and recall are high, use F1=2TP2TP+FP+FNF_1=\frac{2TP}{2TP+FP+FN} their harmonic mean when both are defined. A rate with a zero denominator is undefined.

Lowering the threshold flags more messages (or leaves the predictions unchanged). Recall cannot go down, but neither can the false positive rate. An ROC curve plots recall against false positive rate as the threshold changes. Its area (ROC AUC) measures how well the scores rank positives above negatives; it does not tell you which threshold to use. Pick the threshold on validation data according to which mistakes matter, then measure performance on a separate test set.

Recall
What is the difference between the denominators of precision and recall?
Recall
Why can lowering the threshold not reduce recall?
Exercise
A filter flags eight messages. Six really are spam, and it misses four other spam messages. Find precision and recall, in that order.
Exercise
A filter never flags a message. The test set contains one spam message and 99 good messages. Give accuracy and recall as proportions, in that order.