Confusion matrix metrics
Quick answer
A binary confusion matrix counts true positives, false positives, true negatives and false negatives at a chosen decision threshold. Metrics describe different kinds of mistakes; define the positive class before computing any ratio. Static examples tested locally with Python 3.13.5, scikit-learn 1.8.0 SciPy 1.17.0 and NumPy 2.3.5 where imported. ByteMind package execution is unverified.
Compute metrics from counts
from sklearn.metrics import confusion_matrix,precision_score,recall_score,f1_score,balanced_accuracy_scorey=[0,0,0,0,1,1];p=[0,0,0,1,0,1]print(confusion_matrix(y,p,labels=[0,1]).tolist())print(f"{precision_score(y,p):.3f} {recall_score(y,p):.3f} {f1_score(y,p):.3f} {balanced_accuracy_score(y,p):.3f}")Observed local output:
[[3, 1], [1, 1]]0.500 0.500 0.500 0.625Specificity is 3/4, so balanced accuracy is (0.5+0.75)/2.
How it works
With positive label 1, use rows=true and columns=predicted, ordered [0,1], giving [[TN,FP],[FN,TP]]. Accuracy=(TP+TN)/n; precision=TP/(TP+FP); recall=TP/(TP+FN); specificity=TN/(TN+FP); F1=2TP/(2TP+FP+FN); balanced accuracy=(recall+specificity)/2 when both classes exist. Set a denominator-zero policy: here undefined ratios are reported as null manually; scikit-learn precision/recall/F1 can instead set zero_division=0 explicitly. Binary balanced accuracy assumes both classes present; absent classes need an explicit reporting policy. In multiclass tasks macro averages per-class scores equally, weighted averages by support and micro pools counts before computing the ratio. Micro F1 equals accuracy for ordinary single-label multiclass classification across all classes. Accuracy can be high under imbalance even when recall is zero. Changing the positive label or threshold changes the interpretation. Report counts alongside ratios and estimate uncertainty with an interval appropriate to the sampling design.
Compare averaging policies
from sklearn.metrics import f1_scorey=[0,0,0,0,1,2];p=[0,0,0,0,0,0]for a in ["micro","macro","weighted"]: print(a,f"{f1_score(y,p,average=a,zero_division=0):.3f}")Observed local output:
micro 0.667macro 0.267weighted 0.533Macro scores each of the three classes equally, including the two missed classes.
Common mistakes
Do not swap FP with FN, omit label order, equate macro with micro, or silently report undefined precision as excellent performance.
Practice and next steps
Given TN=90, FP=5, FN=4, TP=1, compute accuracy, recall and precision; explain why accuracy alone is misleading. Complete the knowledge check and staged exercise. Next: Log loss and ROC AUC.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Confusion matrix metrics — knowledge check
Apply confusion matrix metrics to new scenarios.
What is specificity?
What is precision when no positive predictions exist?
Which average weights each class equally?
Which error is a missed positive?
What is balanced accuracy for recall .2 and specificity .8?