3.10 Using Common Classification Metrics
Key Takeaways
- Precision is $TP/(TP+FP)$ — the cost of false alarms; recall is $TP/(TP+FN)$ — the cost of misses; F1 is their harmonic mean.
- $F_\beta$ tilts the balance: $F_2$ weights recall twice as heavily as precision, $F_{0.5}$ does the reverse.
- Log loss scores calibrated probabilities rather than thresholded labels and punishes confident wrong predictions severely.
- ROC-AUC measures ranking quality across all thresholds; PR-AUC does the same while ignoring true negatives, which is why it is preferred for rare events.
- Spark ML computes these through `BinaryClassificationEvaluator` (`areaUnderROC`, `areaUnderPR`) and `MulticlassClassificationEvaluator` (`f1`, `accuracy`, `weightedPrecision`, `weightedRecall`, `logLoss`).
3.10 Using Common Classification Metrics
Model evaluation is the objective mathematical discipline of assessing how effectively a trained algorithm generalizes to unseen data, meets business operational requirements, and balances structural errors. Selecting inappropriate metrics or misinterpreting learning curve diagnostics can lead to deploying poorly performing models into mission-critical production systems. This section details the complete catalog of classification and regression evaluation metrics, the nuances of evaluating models with transformed targets, and systematic protocols for diagnosing and remediating bias and variance.
Classification Evaluation Metric Framework
Binary and multiclass classification models output discrete class assignments or continuous probability vectors that must be evaluated against ground-truth labels using a confusion matrix:
+-----------------------------------------------------------------------------+
| CONFUSION MATRIX TAXONOMY |
| |
| ACTUAL POSITIVE (1) ACTUAL NEGATIVE (0) |
| PREDICTED POSITIVE (1) True Positive (TP) False Positive (FP) |
| Correct Detection Type I Error (Alarm) |
| |
| PREDICTED NEGATIVE (0) False Negative (FN) True Negative (TN) |
| Type II Error (Missed) Correct Rejection |
+-----------------------------------------------------------------------------+
Mathematical Metric Formulations
-
Accuracy: Measures overall proportion of correct classifications:
- Limitation: Highly misleading under class imbalance.
-
Precision (Positive Predictive Value): Measures the accuracy of positive predictions:
- Optimization Goal: Maximize when False Positives are costly (e.g., spam filtering, VIP transaction freezing).
-
Recall / Sensitivity (True Positive Rate): Measures the proportion of actual positives successfully identified:
- Optimization Goal: Maximize when False Negatives are costly (e.g., medical pathology diagnosis, critical equipment failure).
-
Specificity (True Negative Rate): Measures the proportion of actual negatives correctly rejected:
-
F1-Score: Harmonic mean of precision and recall, balancing both dimensions:
-
$F_\beta$-Score: Weighted harmonic mean that assigns $\beta$ times as much importance to recall as to precision:
- $F_2$ Score ($\beta=2$): Weights recall twice as heavily as precision (ideal for fraud and medical detection).
- $F_{0.5}$ Score ($\beta=0.5$): Weights precision twice as heavily as recall (ideal for automated trading or spam filters).
-
Log Loss / Cross-Entropy: Evaluates calibrated probability outputs rather than hard thresholded labels:
- Property: Heavily penalizes confident incorrect predictions (e.g., predicting $\hat{p}=0.99$ when actual $y=0$).
Threshold Metrics vs. Ranking Metrics
Classification metrics fall into two families, and mixing them up is a frequent error.
| Family | Depends on a threshold? | Members | Answers |
|---|---|---|---|
| Threshold metrics | Yes — computed from hard labels | Accuracy, precision, recall, specificity, F1, $F_\beta$ | "How good are the decisions at this cutoff?" |
| Ranking metrics | No — computed across all cutoffs | ROC-AUC, PR-AUC (average precision) | "How well does the model order positives above negatives?" |
| Probability metrics | No — computed from the probabilities themselves | Log loss, Brier score | "How well calibrated are the probabilities?" |
Two practical consequences:
- A model can have excellent ROC-AUC and terrible F1 simply because the default 0.5 threshold is wrong for its calibration. The remedy is threshold tuning (Section 3.2), not retraining.
- Reporting accuracy on an imbalanced problem is meaningless: predicting the majority class for everything already achieves the prevalence rate.
Computing Them on Databricks
from pyspark.ml.evaluation import BinaryClassificationEvaluator, MulticlassClassificationEvaluator
roc = BinaryClassificationEvaluator(labelCol="label", rawPredictionCol="rawPrediction",
metricName="areaUnderROC").evaluate(preds)
pr = BinaryClassificationEvaluator(labelCol="label", rawPredictionCol="rawPrediction",
metricName="areaUnderPR").evaluate(preds)
f1 = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction",
metricName="f1").evaluate(preds)
Note which column each evaluator consumes. BinaryClassificationEvaluator needs
rawPredictionCol (scores or probabilities) because it sweeps thresholds;
MulticlassClassificationEvaluator needs predictionCol (hard labels) because its
metrics are threshold metrics. Passing the wrong column is a classic snippet bug.
Multiclass averaging
For more than two classes, precision and recall must be averaged across classes:
- Macro — unweighted mean over classes; every class counts equally, so rare classes matter as much as common ones.
- Weighted — mean weighted by class support; dominated by the common classes. This
is what Spark's
weightedPrecisionandweightedRecallcompute. - Micro — pooled counts across classes; for single-label multiclass this equals accuracy.
Worked Implementation: Classification Metrics in scikit-learn
import numpy as np
from sklearn.metrics import (
confusion_matrix,
precision_score,
recall_score,
f1_score,
fbeta_score,
roc_auc_score,
average_precision_score
)
y_true = np.array([0, 1, 0, 0, 1, 1, 0, 1, 0, 1])
y_pred = np.array([0, 1, 0, 0, 0, 1, 0, 1, 1, 1])
y_prob = np.array([0.1, 0.9, 0.2, 0.3, 0.45, 0.85, 0.15, 0.78, 0.65, 0.92])
cm = confusion_matrix(y_true, y_pred)
prec = precision_score(y_true, y_pred)
rec = recall_score(y_true, y_pred)
f1 = f1_score(y_true, y_pred)
f2 = fbeta_score(y_true, y_pred, beta=2.0) # Recall weighted 2x over precision
roc_auc = roc_auc_score(y_true, y_prob)
pr_auc = average_precision_score(y_true, y_prob)
print("Confusion Matrix:\n", cm)
print(f"Precision: {prec:.3f}, Recall: {rec:.3f}, F1: {f1:.3f}, F2: {f2:.3f}")
print(f"ROC-AUC: {roc_auc:.3f}, PR-AUC: {pr_auc:.3f}")
An ML engineer is building a medical diagnostic screening model where failing to detect a diseased patient (False Negative) has fatal consequences, whereas a false alarm (False Positive) merely requires a harmless follow-up blood test. Which evaluation metric should the team prioritize and maximize?
A Spark ML pipeline produces a predictions DataFrame with rawPrediction, probability, and prediction columns. Which evaluator and column combination correctly computes area under the precision-recall curve?
A model achieves ROC-AUC of 0.94 but an F1 score of 0.21 on the same validation set. What is the most likely explanation?