SwadhyAI Notes

All Python notes

AUC - ROC Curve 📉

Evaluating Binary Classifiers

The ROC (Receiver Operating Characteristic) curve is a graphical representation of a classifier's performance. AUC (Area Under the Curve) measures how good that classifier is.

✨ Understanding ROC

The ROC curve plots:

  • True Positive Rate (TPR) on the Y-axis: TP / (TP + FN): Also called Sensitivity or Recall
  • False Positive Rate (FPR) on the X-axis: FP / (FP + TN)
python
import numpy as np
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt

# True labels and predicted probabilities
y = np.array([0, 0, 1, 1])
scores = np.array([0.1, 0.4, 0.35, 0.8])

fpr, tpr, thresholds = roc_curve(y, scores, pos_label=1)

plt.plot(fpr, tpr)
plt.plot([0, 1], [0, 1], 'r--')  # Diagonal line (random classifier)
plt.title("ROC Curve")
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")
plt.show()

📊 AUC Score

AUC measures the area under the ROC curve. Higher is better!

  • AUC = 1.0: Perfect classifier.
  • AUC = 0.5: Random guessing (as good as a coin flip).
  • AUC < 0.5: Worse than random!
python
roc_auc = auc(fpr, tpr)
print("AUC:", roc_auc)

💡 When to Use

ROC-AUC is best for balanced datasets. For imbalanced datasets, consider using Precision-Recall curves instead.