TensorFlow
Precision
Recall
F1 `Score`
Confusion Matrix

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:

python
1import numpy as np
2import tensorflow as tf
3
4x = np.array([
5    [0.1, 0.2],
6    [0.9, 0.8],
7    [0.2, 0.1],
8    [0.8, 0.9],
9], dtype="float32")
10
11y = np.array([0, 1, 0, 1], dtype="float32")
12
13model = tf.keras.Sequential([
14    tf.keras.layers.Input(shape=(2,)),
15    tf.keras.layers.Dense(8, activation="relu"),
16    tf.keras.layers.Dense(1, activation="sigmoid"),
17])
18
19model.compile(
20    optimizer="adam",
21    loss="binary_crossentropy",
22    metrics=[
23        tf.keras.metrics.Precision(name="precision"),
24        tf.keras.metrics.Recall(name="recall"),
25    ],
26)
27
28model.fit(x, y, epochs=5, verbose=0)

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.

python
1import tensorflow as tf
2import numpy as np
3
4probs = model.predict(x, verbose=0).reshape(-1)
5preds = (probs >= 0.5).astype("int32")
6labels = y.astype("int32")
7
8cm = tf.math.confusion_matrix(labels, preds, num_classes=2)
9print(cm.numpy())

If the output is:

text
[[2 0]
 [0 2]]

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:

python
1tp = int(((preds == 1) & (labels == 1)).sum())
2fp = int(((preds == 1) & (labels == 0)).sum())
3fn = int(((preds == 0) & (labels == 1)).sum())
4
5precision = tp / (tp + fp) if (tp + fp) else 0.0
6recall = tp / (tp + fn) if (tp + fn) else 0.0
7f1 = 2 * precision * recall / (precision + recall) if (precision + recall) else 0.0
8
9print("precision:", precision)
10print("recall:", recall)
11print("f1:", f1)

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:

python
preds = (probs >= 0.7).astype("int32")

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_matrix after 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.

Course illustration
Course illustration

All Rights Reserved.