🔍 Introduction

When building a machine learning model, especially for classification problems, it’s not enough to just check accuracy. Sometimes, your model may look accurate but fail in real-world applications. That’s where evaluation metrics like ROC (Receiver Operating Characteristic) curve and AUC (Area Under the Curve) come into play.

They help you understand how well your model separates classes, even if the dataset is imbalanced.

📈 What is the ROC Curve?

👉 The ROC curve is created by plotting TPR vs FPR at different threshold values.

Formula

🎯 Why Do We Need the ROC Curve?

🧮 Example Scenario

Imagine you’re building a spam email classifier:

By plotting TPR against FPR at different thresholds, the ROC curve shows how well your classifier separates spam from normal emails.

📐 What is the AUC Score?

📊 Interpreting AUC Score

🖥️ Python Example (Using scikit-learn)

from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt

# Generate sample data
X, y = make_classification(n_samples=1000, n_features=20, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Train a model
model = LogisticRegression()
model.fit(X_train, y_train)

# Predict probabilities
y_prob = model.predict_proba(X_test)[:, 1]

# ROC curve
fpr, tpr, thresholds = roc_curve(y_test, y_prob)
roc_auc = auc(fpr, tpr)

# Plot
plt.plot(fpr, tpr, label=f"ROC curve (AUC = {roc_auc:.2f})")
plt.plot([0,1], [0,1], "r--")  # Random guess line
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")
plt.title("ROC Curve")
plt.legend(loc="lower right")
plt.show()

✅ This code trains a logistic regression model, computes ROC and AUC, and plots the curve.

⚖️ ROC vs. Precision-Recall Curve

🚀 Key Takeaways

👉 In short, ROC and AUC are powerful tools to evaluate your model’s true predictive power, especially when accuracy alone doesn’t tell the full story.