ENع
←Evaluation
Lesson 09 · Classic machine learning

Evaluating models

14 min11 steps5 questionsIntermediate
About this lesson

A classifier is only as good as the way you measure it. Take ten held-out emails, each scored by a spam filter, and turn the scores into answers with a threshold. Sort them into the four boxes of the confusion matrix, compute accuracy, precision, recall and F1 by hand, then slide the threshold and watch precision and recall trade while accuracy does not move.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 9 · Interactive
01 / 11

Ten emails, ten scores

A spam filter scored ten test emails it never trained on: near 0 means surely fine, near 1 surely spam. We also know the truth: four are spam (rose, front lane) and six are not (mint, back lane).

held-out test emails (never used in training)
truth   score
fine    0.04
fine    0.13
fine    0.22
fine    0.31
spam    0.38
fine    0.42
spam    0.64
fine    0.70
spam    0.82
spam    0.93
Loading figure…
or ← →
Read the lesson step by step
  1. Ten emails, ten scores

    A spam filter scored ten test emails it never trained on: near 0 means surely fine, near 1 surely spam. We also know the truth: four are spam (rose, front lane) and six are not (mint, back lane).

    held-out test emails (never used in training)
    truth   score
    fine    0.04
    fine    0.13
    fine    0.22
    fine    0.31
    spam    0.38
    fine    0.42
    spam    0.64
    fine    0.70
    spam    0.82
    spam    0.93
  2. Draw a line at 0.5

    A score is not an answer yet. Pick a threshold: a score of 0.5 or more means predict spam. Emails right of the line are flagged as spam; emails left of it go to the inbox.

    t = 0.5
    pred = [s >= t for s in p]
      -> flagged: 4 emails, inbox: 6
    
    the model gives scores; the threshold
    turns each score into a yes / no answer
  3. Four boxes

    Sort each email by truth and prediction. Spam caught: true positive (3). Good mail flagged: false positive (1). Spam missed: false negative (1). Good mail passed: true negative (5). That 2 × 2 table is the confusion matrix.

                     predicted     predicted
                     not spam      spam
    really not spam  TN 5          FP 1
    really spam      FN 1          TP 3
    
    sklearn.metrics.confusion_matrix(y, pred)
      -> [[5, 1], [1, 3]]
  4. Accuracy: right out of all

    Accuracy counts every right answer, true positives plus true negatives, out of all emails: (3 + 5) / 10 = 0.80. Easy to read, but it treats both kinds of mistake as the same.

    acc = (tp + tn) / n
        = (3 + 5) / 10 = 0.80
    
    right answers: the TP and TN boxes
    wrong answers: the FP and FN boxes
  5. Precision: trust a flag

    Precision asks: when the filter says spam, how often is it right? TP / (TP + FP) = 3 / (3 + 1) = 0.75. Each false positive, a good email in the spam folder, pulls it down.

    prec = tp / (tp + fp)
         = 3 / (3 + 1) = 0.75
    
    looks only at the right-hand column:
    the emails the filter flagged
  6. Recall: spam caught

    Recall asks: of all the real spam, how much did the filter catch? TP / (TP + FN) = 3 / (3 + 1) = 0.75. Each false negative, spam left in the inbox, pulls it down.

    rec = tp / (tp + fn)
        = 3 / (3 + 1) = 0.75
    
    looks only at the front row:
    the emails that really are spam
    (also called sensitivity or true positive rate)
  7. F1: one number for both

    F1 joins precision and recall with the harmonic mean: 2 × P × R / (P + R) = 0.75. Unlike a plain average, it stays low when either one is low, so a filter cannot hide a bad recall behind a good precision.

    f1 = 2*prec*rec/(prec+rec)
       = 2 * 0.75 * 0.75 / (0.75 + 0.75) = 0.75
    
    precision 1.00 with recall 0.10:
      plain average 0.55, F1 0.18
  8. Raise the threshold

    Move the threshold up to 0.75: only very sure emails are flagged. No good email is flagged, so precision is 1.00, but 2 spams slip through and recall drops to 0.50. Accuracy is still 0.80.

    t = 0.75   TN 6   FP 0   FN 2   TP 2
    
                t = 0.50   t = 0.75
    accuracy    0.80       0.80
    precision   0.75       1.00
    recall      0.75       0.50
    F1          0.75       0.67
  9. Lower the threshold

    Move it down to 0.35. Every spam is caught: recall 1.00. But 2 good emails land in the spam folder, so precision falls to 0.67. Accuracy stays 0.80, blind to the trade.

    t = 0.35   TN 4   FP 2   FN 0   TP 4
    
                t = 0.75   t = 0.50   t = 0.35
    precision   1.00       0.75       0.67
    recall      0.50       0.75       1.00
    
    lower t: more caught, more false alarms
  10. Accuracy can lie

    A filter that never says spam (threshold 1.00, above every score) is still 0.60 accurate here, with recall 0. If only 1 email in 100 were spam, this useless filter would score 0.99 accuracy.

    t = 1.00   TN 6   FP 0   FN 4   TP 0
    accuracy 0.60   recall 0.00   F1 0.00
    precision = 0 / 0: no answer (shown as –)
    
    1 spam in 100, never say spam:
      accuracy = 99 / 100 = 0.99, recall = 0
    rare class: read precision and recall
  11. Choose for the cost

    Which mistake costs more? Losing a real email hurts, so a spam filter favours precision: here, 0.75. A disease screen favours recall. Pick the threshold on validation data, then measure the test set once.

    spam filter     a false positive hides real mail
                    -> favour precision, higher t
    disease screen  a false negative misses a patient
                    -> favour recall, lower t
    
    every threshold at once: the ROC curve
    (true positive rate vs false positive rate)

Quiz Unlocks after the last step.