Metrics
Pick metrics that match your problem. Accuracy is misleading on imbalanced data; AUC ignores threshold; F1 trades off precision and recall. Pick one as the “north star” before training.
Classification, regression, ranking
EXAMPLE
from sklearn.metrics import (
accuracy_score, precision_score, recall_score, f1_score,
roc_auc_score, average_precision_score, log_loss, confusion_matrix,
classification_report,
)
import numpy as np
# Classification
y_true = np.array([0, 0, 1, 1, 0, 1])
y_pred = np.array([0, 1, 1, 1, 0, 0]) # hard labels
y_prob = np.array([0.1, 0.6, 0.8, 0.7, 0.3, 0.45]) # probabilities
accuracy_score(y_true, y_pred) # OK on balanced data
precision_score(y_true, y_pred) # of predicted positives, how many right
recall_score(y_true, y_pred) # of actual positives, how many caught
f1_score(y_true, y_pred) # harmonic mean of precision + recall
log_loss(y_true, y_prob) # penalises confident-wrong predictions
roc_auc_score(y_true, y_prob) # ranking quality across thresholds
average_precision_score(y_true, y_prob) # better for imbalanced data than AUC
print(classification_report(y_true, y_pred))
print(confusion_matrix(y_true, y_pred))
# Regression
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
mean_squared_error(y_true, y_pred) # MSE
mean_squared_error(y_true, y_pred, squared=False) # RMSE
mean_absolute_error(y_true, y_pred) # MAE — robust to outliers
r2_score(y_true, y_pred)
# Multiclass — pick averaging
f1_score(y_true_multi, y_pred_multi, average='macro') # unweighted mean
f1_score(y_true_multi, y_pred_multi, average='weighted') # weighted by support
f1_score(y_true_multi, y_pred_multi, average='micro') # global TP/FP/FN
Why it matters
“Macro-F1” for class balance, “weighted F1” for unbalanced production reality, “PR AUC” over ROC AUC when positives are rare. The right pick is invisible to non-ML stakeholders — document it.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
Example
from sklearn.metrics import accuracy_score, f1_score, roc_auc_score print(accuracy_score(yte, pred)) print(f1_score(yte, pred, average='macro')) print(roc_auc_score(yte, prob))Try it Yourself »
Exercise
Macro F1 score.
f1_score(y, p, average='
')
Five letters.
Discussion
Loading…