Sub topic 12
ROC AUC for more than two classes
This chapter assumes you can build a binary ROC curve, read AUC as a ranking probability, and call roc_auc_score in scikit-learn. A ROC curve needs one positive class and one negative class. With three or more classes, you have to reduce the problem to binary ones first.
One-vs-rest: one curve per class
One-vs-rest (OvR) treats each class in turn as the positive class. All other classes together become the negative class. For a model with classes A, B and C, you get three binary problems:
- A versus (B or C)
- B versus (A or C)
- C versus (A or B)
For each problem, the score is that class's predicted probability, which is one column of predict_proba. Each problem gives its own ROC curve and its own AUC.
Computing it in scikit-learn
The code below computes the per-class AUCs by hand, then compares the average with the built-in option.
import numpy as np
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score
from sklearn.preprocessing import label_binarize
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=0, stratify=y
)
model = LogisticRegression(max_iter=1000).fit(X_train, y_train)
proba = model.predict_proba(X_test)
# One indicator column per class: 1 if the example is in that class
Y = label_binarize(y_test, classes=model.classes_)
per_class = [roc_auc_score(Y[:, k], proba[:, k]) for k in range(proba.shape[1])]
print(per_class)
print(np.mean(per_class))
print(roc_auc_score(y_test, proba, multi_class="ovr"))
print(roc_auc_score(y_test, proba, multi_class="ovo"))The second and third printed values match, because multi_class="ovr" uses average="macro" by default. That is the unweighted mean of the per-class AUCs. The column order of proba follows model.classes_, which is sorted label order.
Averaging choices
| Option | What it does | When to use it |
|---|---|---|
| `average="macro"` | Plain mean of per-class AUCs | Every class matters equally, including rare ones |
| `average="weighted"` | Mean weighted by class frequency | You care most about typical examples |
| `multi_class="ovo"` | Averages AUC over every pair of classes | Class imbalance should not change the result |
One-vs-one (OvO) compares two classes at a time and ignores the rest. Its pairwise AUCs do not depend on how many examples the other classes have. OvR is easier to explain, but each "rest" group changes size with the class mix, so OvR results are sensitive to class frequencies.
Common errors
- Passing hard labels.
roc_auc_scoreneeds probabilities or scores. Predicted labels frompredictcollapse each curve to a single point. - Probabilities that do not sum to 1. With
multi_classset, scikit-learn requires each row ofy_scoreto sum to 1. Raw decision scores from some models fail this check. Use calibrated probabilities or compute per-class binary AUCs yourself. - Missing classes in the test set. If a class is absent from
y_test, its binary AUC is undefined and the call raises an error. Use stratified splits. - Hiding a weak class. A high macro average can mask one poorly separated class. Always print the per-class values as well.
Exercise
A three-class model has per-class OvR AUCs of 0.98, 0.97 and 0.62. What is the macro average, and why is it a poor sole summary?
Show answer
The macro average is (0.98 + 0.97 + 0.62) / 3 = 0.857. It looks respectable, but it hides the third class, which the model separates only slightly better than chance (0.5). Report the per-class AUCs alongside the average.