TECHNOLOGIESTieInn Technologies

Measuring a classifier: accuracy, precision, recall, confusion matrix

ML Foundations · 10 questions

Short notes

Key idea

Accuracy can lie when classes are unbalanced. The confusion matrix counts each kind of right and wrong answer; precision and recall show the trade-off between false alarms and misses.

Syntax

  • TP: predicted yes, was yes. FP: predicted yes, was no. FN: predicted no, was yes. TN: predicted no, was no.
  • Precision = TP / (TP + FP): of the flagged, how many were real.
  • Recall = TP / (TP + FN): of the real ones, how many were caught.
  • F1 = 2PR / (P + R).
from sklearn.metrics import confusion_matrix, classification_report
print(confusion_matrix(y_test, pred))
print(classification_report(y_test, pred))

Example

1,000 transactions, 10 frauds. A model that says "not fraud" every time is 99% accurate and catches nothing: recall 0. A model that flags 30 and catches 8 frauds has precision 8/30 ≈ 27% and recall 8/10 = 80%.

Traps

  • Choose the metric from the cost of mistakes: missing cancer (recall) vs annoying customers with false fraud alerts (precision).
  • The threshold changes the trade-off; 0.5 isn't sacred.
  • Report per-class numbers, not only the average.
  • Accuracy on unbalanced data is almost meaningless.

Want the full lesson with step-by-step examples, a project and a certificate? ML Foundations course, ₹180 →

Questions

Open in Code Lab5 free questions
5 questions

    Measuring a classifier: accuracy, precision, recall, confusion matrix

  1. Question 1easy

    The model says a visit will buy, but it didn't. What kind of result is that?

  2. Measuring a classifier: accuracy, precision, recall, confusion matrix

  3. Question 2medium

    The model flagged 20 visits; 15 really bought. What is its precision?

    tp, fp = 15, 5
    print(tp / (tp + fp))
  4. Measuring a classifier: accuracy, precision, recall, confusion matrix

  5. Question 3medium

    There were 40 real buyers; the model found 30. What is its recall?

    tp, fn = 30, 10
    print(tp / (tp + fn))
  6. Measuring a classifier: accuracy, precision, recall, confusion matrix

  7. Question 4medium

    You raise the decision threshold from 0.5 to 0.9. What usually happens?

  8. Measuring a classifier: accuracy, precision, recall, confusion matrix

  9. Question 5medium

    A system flags possible prompt-injection tickets for a person to check. Which should you favour?

5 more questions on this topic, with hints, and tests

Sign up free for 21 days of every question and test.