For classification we count how often each kind of right and wrong decision occurs. The confusion matrix records these counts and every common measure is computed from it.

The Confusion Matrix

Take a binary problem with a positive class (the class of interest, such as disease) and a negative class. Each prediction falls in one of four cells.

  1. True Positive (TP): positive instance predicted positive.
  2. False Negative (FN): positive instance predicted negative (a miss).
  3. False Positive (FP): negative instance predicted positive (a false alarm).
  4. True Negative (TN): negative instance predicted negative.

Accuracy

Accuracy = (TP + TN) / (TP + TN + FP + FN) is the fraction of all predictions that are correct. It is simple but misleading when classes are imbalanced. If only 5 percent of patients are ill, a useless model that always says "healthy" is 95 percent accurate while finding no patient.

Precision, Recall and F1 Score

Precision = TP / (TP + FP) answers: of the instances predicted positive, how many really are? It matters when false alarms are costly, as in spam filtering. Recall = TP / (TP + FN) (also sensitivity or true positive rate) answers: of the real positives, how many were found? It matters when misses are costly, as in cancer screening. The two trade off against each other. The F1 score combines them as their harmonic mean: F1 = 2 × Precision × Recall / (Precision + Recall). It is high only when both are high.

Definition: Confusion matrix. A table that counts, for each actual class, how many instances were predicted in each class; for two classes it holds TP, FN, FP and TN.

Table 2.3: Binary confusion matrix

Actual / Predicted Predicted positive Predicted negative
Actual positive TP FN
Actual negative FP TN
Totals TP + FP FN + TN

Example 2.3: Metrics from a confusion matrix

Problem: A classifier gives TP = 40, FN = 10, FP = 5 and TN = 45 on 100 instances. Find accuracy, precision, recall and F1.

Solution:

  1. Accuracy = (40 + 45) / 100 = 0.85.
  2. Precision = 40 / (40 + 5) = 40 / 45 = 0.889.
  3. Recall = 40 / (40 + 10) = 40 / 50 = 0.80.
  4. F1 = 2 × 0.889 × 0.80 / (0.889 + 0.80) = 1.4222 / 1.6889 = 0.842.

Result: Accuracy 0.85, precision 0.889, recall 0.80 and F1 0.842.

from sklearn.metrics import (confusion_matrix, accuracy_score,
                             precision_score, recall_score, f1_score)
y_true = [1] * 50 + [0] * 50
y_pred = [1] * 40 + [0] * 10 + [1] * 5 + [0] * 45
print(confusion_matrix(y_true, y_pred, labels=[1, 0]))
print("accuracy ", accuracy_score(y_true, y_pred))
print("precision", round(precision_score(y_true, y_pred), 3))
print("recall   ", recall_score(y_true, y_pred))
print("F1       ", round(f1_score(y_true, y_pred), 3))
lazy_true = [1] * 5 + [0] * 95
lazy_pred = [0] * 100
print("always-negative accuracy:",
      accuracy_score(lazy_true, lazy_pred),
      " recall:", recall_score(lazy_true, lazy_pred))

Output

[[40 10]
 [ 5 45]]
accuracy  0.85
precision 0.889
recall    0.8
F1        0.842
always-negative accuracy: 0.95  recall: 0.0

Common mistakes

  • Trusting accuracy on imbalanced data.
  • Swapping precision and recall; precision divides by predicted positives, recall by actual positives.
  • Averaging precision and recall with the ordinary mean instead of the harmonic mean.
  • Reading the matrix with rows and columns reversed.

Key takeaways

  • Accuracy = (TP + TN) / total.
  • Precision = TP / (TP + FP); Recall = TP / (TP + FN).
  • F1 = 2PR / (P + R), the harmonic mean of precision and recall.
  • Prefer recall when misses are costly, precision when false alarms are costly.

Practice questions

  1. What is a false negative?
  2. When is accuracy misleading?
  3. Why is F1 a harmonic mean?

Answers to the practice questions

  1. A positive instance predicted as negative.
  2. When classes are imbalanced.
  3. So that it is high only when both precision and recall are high.

Sources

  • Official BCA syllabus Introduction to Machine Learning, course code 26BCA404DSE21, Unit II