1. True Positive Rate (TPR) or Sensitivity or Recall:

    • TPR measures the proportion of actual positives that are correctly identified by the model.
    • Formula: $$ \text{TPR} = \frac{\text{True Positives (TP)}}{\text{True Positives (TP)} + \text{False Negatives (FN)}} $$
  2. False Positive Rate (FPR):

    • FPR measures the proportion of actual negatives that are incorrectly identified as positives by the model.
    • Formula: $$ \text{FPR} = \frac{\text{False Positives (FP)}}{\text{False Positives (FP)} + \text{True Negatives (TN)}} $$
  3. Threshold:

    • The classification threshold determines the point at which the model's predicted probability is converted into a binary decision (positive or negative).
    • By varying this threshold, you can generate different TPR and FPR values, which are then plotted to form the ROC curve.

How to Construct an ROC Curve:

  1. Calculate TPR and FPR:

    • For a series of threshold values (typically from 0 to 1), calculate the TPR and FPR.
  2. Plot the Points:

    • Plot the TPR against the FPR for each threshold value.
  3. Connect the Points:

    • Connect these points to form the ROC curve.

Interpreting the ROC Curve:

Area Under the ROC Curve (AUC - ROC):

Python Example:

Here’s how you can plot an ROC curve using the sklearn library:

import numpy as np
import matplotlib.pyplot as plt
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

# Generate a binary classification dataset
X, y = make_classification(n_samples=1000, n_features=20, n_classes=2, random_state=42)

# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Create a logistic regression model
model = LogisticRegression()

# Train the model
model.fit(X_train, y_train)

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

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

# Plot ROC curve
plt.figure()
plt.plot(fpr, tpr, color='blue', lw=2, label=f'ROC curve (area = {roc_auc:.2f})')
plt.plot([0, 1], [0, 1], color='red', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic (ROC) Curve')
plt.legend(loc='lower right')
plt.show()

Interpreting the ROC Curve and AUC

  1. ROC Curve Shape:

    • The curve starts at (0, 0) and ends at (1, 1).
    • A curve that bows towards the top-left corner indicates good performance. The model achieves high TPR while maintaining low FPR.
  2. Diagonal Line (Random Classifier):

    • The dotted line from (0, 0) to (1, 1) represents a random classifier with no skill. Points above this line indicate better-than-random performance.
  3. Area Under the Curve (AUC):

    • The AUC value (0.5 to 1.0) provides a single measure of overall model performance. An AUC of 0.93, for example, indicates excellent model performance.

Practical Tips