Lesson Introduction

Welcome! Today, we are diving into a fascinating metric in machine learning called AUC-ROC.

Our goal for this lesson is to understand what AUC (Area Under the Curve) and ROC (Receiver Operating Characteristic) are, how to calculate and interpret the AUC-ROC metric, and how to visualize the ROC curve using Python. Ready to explore? Let's get started!

Understanding ROC
Plotting the ROC Curve

Visualizing the ROC curve helps understand model performance at different thresholds. Let's look at a Python code snippet to see these concepts in action. We'll manually calculate the ROC data and then plot it using matplotlib.

Python
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import confusion_matrix

# Sample binary classification dataset
y_true = np.array([0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1])
y_scores = np.array([0.0, 0.4, 0.5, 0.8, 0.4, 0.8, 0.5, 0.8, 0.7, 0.5, 1])

# Get unique thresholds
thresholds = np.sort(np.unique(y_scores))

# Initialize lists to hold TPR and FPR values
tpr = []
fpr = []

# Calculate TPR and FPR for each threshold
for thresh in thresholds:
    y_pred = (y_scores >= thresh).astype(int)
    tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
    
    tpr.append(tp / (tp + fn))  # True Positive Rate
    fpr.append(fp / (fp + tn))  # False Positive Rate

# Plotting ROC curve
plt.plot(fpr, tpr, marker='.')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC Curve')
plt.show()

In the example above, y_true represents the true labels, and y_scores is an array with the predicted probabilities.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal