Many classifiers output a score or probability, and a threshold turns the score into a class. Changing the threshold changes the confusion matrix. The ROC curve shows all these trade-offs at once.

Threshold and Its Effect

If the rule is "predict positive when score >= t", then a low threshold labels many instances positive, which raises recall but also false alarms, while a high threshold is strict, which lowers false alarms but also recall. The default of 0.5 is not always the best choice.

The ROC Curve

The Receiver Operating Characteristic (ROC) curve plots the True Positive Rate (TPR = TP / (TP + FN)), which is recall, on the vertical axis against the False Positive Rate (FPR = FP / (FP + TN)) on the horizontal axis, for every threshold. The curve starts at (0, 0), where nothing is predicted positive, and ends at (1, 1), where everything is. A perfect classifier passes through (0, 1), the top-left corner. A random guesser lies on the diagonal from (0, 0) to (1, 1).

Area Under the Curve

The AUC is the area under the ROC curve. It lies between 0 and 1. AUC = 1 means perfect ranking, AUC = 0.5 is no better than chance and AUC below 0.5 means the scores are reversed. It has a useful meaning: AUC is the probability that a randomly chosen positive instance gets a higher score than a randomly chosen negative one. It does not depend on any one threshold, so it is helpful for comparing models, and it is less sensitive to class imbalance than accuracy.

Definition: AUC. The area under the ROC curve; the probability that a randomly chosen positive instance is scored higher than a randomly chosen negative instance.

Table 2.4: Reading the AUC value

AUC Meaning Comment
1.0 Perfect Every positive is ranked above every negative
0.9 to 1.0 Excellent Very strong separation
0.7 to 0.9 Good Useful in practice
0.5 No skill Same as random guessing
Below 0.5 Worse than chance Scores are inverted

Example 2.4: Computing the AUC by pairs

Problem: Four instances have actual classes 0, 0, 1, 1 and scores 0.1, 0.4, 0.35, 0.8. Find the AUC.

Solution:

  1. The positives have scores 0.35 and 0.8; the negatives have 0.1 and 0.4. There are 2 × 2 = 4 positive-negative pairs.
  2. 0.35 > 0.1 (correct), 0.35 > 0.4 (wrong), 0.8 > 0.1 (correct), 0.8 > 0.4 (correct).
  3. Three of the four pairs are ranked correctly, so AUC = 3 / 4 = 0.75.
  4. The ROC points (FPR, TPR) are (0, 0), (0, 0.5), (0.5, 0.5), (0.5, 1) and (1, 1).

Result: AUC = 0.75.

from sklearn.metrics import roc_auc_score, roc_curve
y = [0, 0, 1, 1]
score = [0.1, 0.4, 0.35, 0.8]
fpr, tpr, _ = roc_curve(y, score)
print("FPR:", [float(v) for v in fpr])
print("TPR:", [float(v) for v in tpr])
print("AUC:", roc_auc_score(y, score))

Output

FPR: [0.0, 0.0, 0.5, 0.5, 1.0]
TPR: [0.0, 0.5, 0.5, 1.0, 1.0]
AUC: 0.75

Common mistakes

  • Believing an AUC of 0.5 is acceptable; it equals random guessing.
  • Fixing the threshold at 0.5 without checking the cost of errors.
  • Confusing FPR with precision.
  • Using AUC alone when the probability calibration also matters.

Key takeaways

  • TPR = recall = TP / (TP + FN); FPR = FP / (FP + TN).
  • The ROC curve plots TPR against FPR over all thresholds.
  • AUC = 1 perfect, 0.5 chance.
  • AUC is the chance a random positive is scored above a random negative.

Practice questions

  1. What are the axes of the ROC curve?
  2. What does AUC = 0.5 mean?
  3. Which corner does a perfect classifier reach?

Answers to the practice questions

  1. TPR on the vertical axis and FPR on the horizontal axis.
  2. The model is no better than random guessing.
  3. The top-left corner (0, 1).

Sources

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