Sub topic 3
Measuring Performance: Accuracy, Precision, Recall, and ROC AUC
Assume you know binary classification, train/test splits, and basic probability. A classifier produces a score or probability. A threshold turns that value into a class label.
Core metrics
For a positive class, the confusion matrix counts true positives (TP), false positives (FP), true negatives (TN), and false negatives (FN).
| Metric | Formula | Meaning |
|---|---|---|
| Accuracy | `(TP + TN) / total` | Fraction of all predictions that are correct |
| Precision | `TP / (TP + FP)` | Fraction of predicted positives that are truly positive |
| Recall | `TP / (TP + FN)` | Fraction of actual positives that are found |
| ROC AUC | Area under the ROC curve | Ranking quality across all thresholds |
Accuracy can hide poor minority-class performance. Precision matters when false alarms are costly. Recall matters when missed positives are costly. ROC AUC measures ranking, not the final thresholded decisions.
Choosing a threshold for your costs
The default threshold is 0.5, but it is not automatically correct. Lowering it usually increases recall and decreases precision. Choose it on validation data using the cost of false positives and false negatives, then report test metrics once.
A scikit-learn workflow
Scale features inside a pipeline so that test information does not leak into training.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, roc_auc_score
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, stratify=y, random_state=0
)
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
model.fit(X_train, y_train)
p = model.predict_proba(X_test)[:, 1]
print(roc_auc_score(y_test, p))
print(classification_report(y_test, p >= 0.3))Imbalance and probability quality
With imbalanced classes, use stratified splits, inspect precision and recall, and consider class_weight="balanced". A precision-recall curve can be more informative than ROC AUC when positives are rare.
Calibration asks whether predicted probabilities match observed frequencies. Among examples predicted near 0.8, roughly 80% should be positive. Use a reliability diagram or calibration curve. A high ROC AUC does not guarantee calibrated probabilities.
Multiple classes and limits
For multiple classes, one-vs-rest fits one binary model per class. Softmax regression fits class scores whose exponentiated, normalized values sum to one.
Logistic regression assumes a linear decision boundary in the features. Perfect separation can make coefficients grow without bound. Strongly correlated or unscaled features can also make coefficients unstable. Try regularization, scaling, and feature inspection. Move to a nonlinear model when validation results show that a linear boundary is inadequate.
Exercise
Why might a model with 99% accuracy still be unsuitable for detecting a rare disease?
Show answer
If only 1% of cases are positive, predicting negative every time gives 99% accuracy but zero recall.