ROC Curves
Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 25 of 58.
Every threshold gives its own recall and its own false alarms. The ROC curve shows them all at once:
Across: the false positive rate, the share of healthy patients flagged. Up: recall. Lower thresholds climb toward (1, 1).
from sklearn.metrics import roc_curve, roc_auc_score
proba = model.predict_proba(X_test)[:, 1]
fpr, tpr, thresholds = roc_curve(y_test, proba) # tpr is recall
ax.plot(fpr, tpr)
print(f"{roc_auc_score(y_test, proba):.3f}")This prints 0.914, the AUC: the area under the curve. 1 is perfect, 0.5 a coin toss. A higher curve catches more for the same false alarms.
Challenge
MediumA cherry farm lights frost fires on nights that may freeze. The starter trains two frost models, logistic and knn, and sets up the chart.
- Write
draw_roc(model, name): it draws the model's ROC curve onax, labelledname, and prints its AUC as below. - Call it for both models, add a dashed diagonal labelled
coin tossand a legend, then show.
For landslide models:
logistic AUC: 0.869
k-NN AUC: 0.924Then Coddy describes your chart. Theirs looks like this:
Try it yourself
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import roc_curve, roc_auc_score
# One row per spring night on a cherry farm: 1 in frost means frost formed before dawn
nights = pd.read_csv("frost.csv")
features = ["temp_6pm_c", "humidity_pct", "wind_kmh", "cloud_pct"]
X_train, X_test, y_train, y_test = train_test_split(
nights[features], nights["frost"], test_size=0.25, random_state=0, stratify=nights["frost"])
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Two frost models
logistic = LogisticRegression(max_iter=1000)
logistic.fit(X_train, y_train)
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train, y_train)
fig, ax = plt.subplots()
ax.set_title("Frost Warnings")
ax.set_xlabel("False positive rate")
ax.set_ylabel("Recall")
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Supervised Learning
1Your First Model
Learning from ExamplesTrain and Test SetsFit and PredictScoring a ModelRecap: Seed Sorter2Nearest Neighbors
How k-NN DecidesChoosing kDecision BoundariesScaling for NeighborsRecap: Wine Neighbors5Measuring Classifiers
The Confusion MatrixPrecision and RecallThe F1 ScoreImbalanced ClassesROC CurvesRecap: Screening Test11Project: Penguin Species
Meet the PenguinsPrepare the DataChoose a ModelTune the WinnerShip the ModelPractice on your own: Online Python compiler