Tensorflow Precision / Recall / F1 score and Confusion matrix
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Accuracy is often not enough to judge a classifier, especially when one class is much rarer than the others. Precision, recall, F1 score, and the confusion matrix show where the model is making mistakes and whether those mistakes matter more on the false-positive side or the false-negative side.
What These Metrics Mean
For a binary classifier:
- precision asks: of the items predicted positive, how many were really positive
- recall asks: of the truly positive items, how many did the model catch
- F1 score balances precision and recall
- the confusion matrix shows counts of true positives, false positives, true negatives, and false negatives
Those metrics are especially useful for fraud detection, medical screening, and other cases where class imbalance makes accuracy misleading.
Using TensorFlow Metrics During Training
Keras provides built-in precision and recall metrics. Here is a small example:
This gives you precision and recall during training and evaluation, but not a confusion matrix or a universally available built-in F1 in every TensorFlow setup.
Computing the Confusion Matrix
After prediction, convert probabilities to class labels and build the confusion matrix explicitly.
If the output is:
then the classifier got every example right on this small toy dataset.
Computing F1 Score
F1 is derived from precision and recall:
F1 = 2 * precision * recall / (precision + recall)
You can compute it after prediction:
This approach is simple and works reliably no matter which exact TensorFlow version you are using.
Threshold Choice Matters
Precision and recall depend on the classification threshold. A sigmoid model does not magically decide that 0.5 is always optimal. Lowering the threshold usually increases recall and decreases precision. Raising it usually does the opposite.
That means metrics should be interpreted together with the decision rule:
If the application penalizes false positives heavily, a higher threshold may be better. If missing true positives is worse, you may want a lower threshold.
Common Pitfalls
The most common mistake is reporting only accuracy on imbalanced data. A model can achieve high accuracy by predicting the majority class almost all the time while having terrible recall for the minority class.
Another issue is computing metrics on raw probabilities instead of thresholded class predictions when the formulas expect discrete classes. Be explicit about the threshold you use.
A third pitfall is comparing F1 values across experiments while silently changing the threshold. If the threshold changes, the metric comparison is not apples to apples.
Finally, be careful with multi-class problems. Precision, recall, and F1 can be computed with micro, macro, or weighted averaging, and those averages answer different questions.
Summary
- Precision, recall, F1, and the confusion matrix reveal classifier behavior better than accuracy alone.
- Keras provides built-in precision and recall metrics during training.
- Use
tf.math.confusion_matrixafter prediction to inspect class-level errors. - Compute F1 from precision and recall when needed.
- Always interpret the metrics together with the decision threshold and class balance.

