multi-class classification
TensorFlow
precision and recall
class-wise evaluation
machine learning metrics

Class wise precision and recall for multi class classification in Tensorflow?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

For multiclass classification, class-wise precision and recall are usually computed after prediction time, not as a single built-in Keras metric during training. The common workflow is to run the model, convert probabilities to predicted class IDs, build a confusion matrix, and then derive precision and recall for each class from that matrix.

Why accuracy is not enough

Accuracy collapses everything into one number. That can hide important class-specific failures, especially when:

  • classes are imbalanced
  • one class is much harder than the others
  • some errors matter more than others

Class-wise precision and recall answer more specific questions:

  • precision for class i: when the model predicts class i, how often is it correct
  • recall for class i: when the true label is class i, how often does the model find it

Start from predicted class IDs

Assume you have a softmax model:

python
1import numpy as np
2import tensorflow as tf
3
4model = tf.keras.Sequential([
5    tf.keras.layers.Input(shape=(10,)),
6    tf.keras.layers.Dense(16, activation="relu"),
7    tf.keras.layers.Dense(3, activation="softmax")
8])
9
10x_test = np.random.random((8, 10)).astype("float32")
11y_true = np.array([0, 1, 2, 1, 0, 2, 2, 1])
12
13y_pred_probs = model.predict(x_test, verbose=0)
14y_pred = np.argmax(y_pred_probs, axis=1)

For multiclass softmax, argmax is the usual way to convert output probabilities into predicted labels.

Use a confusion matrix to get per-class counts

TensorFlow can build the confusion matrix directly:

python
cm = tf.math.confusion_matrix(y_true, y_pred, num_classes=3)
print(cm.numpy())

For a class i:

  • true positives are cm[i, i]
  • false positives are the rest of column i
  • false negatives are the rest of row i

From that, you can compute class-wise precision and recall:

python
1cm = cm.numpy()
2
3for i in range(cm.shape[0]):
4    tp = cm[i, i]
5    fp = cm[:, i].sum() - tp
6    fn = cm[i, :].sum() - tp
7
8    precision = tp / (tp + fp) if (tp + fp) else 0.0
9    recall = tp / (tp + fn) if (tp + fn) else 0.0
10
11    print(f"class {i}: precision={precision:.3f}, recall={recall:.3f}")

This is the clearest manual way to understand what the metrics mean.

sklearn is often the easiest reporting tool

In practice, many people train with TensorFlow and evaluate with scikit-learn reporting helpers:

python
from sklearn.metrics import classification_report

print(classification_report(y_true, y_pred, digits=3))

This prints class-wise precision, recall, F1-score, and support in one step. It is often the fastest answer when the question is about reporting metrics rather than implementing them from scratch.

Why built-in Keras precision and recall can be confusing here

Keras has Precision and Recall metrics, but they are more natural for binary or one-vs-rest thresholded setups. For multiclass softmax, people often expect them to automatically emit a separate metric per class, which is not the default experience.

That is why confusion-matrix or classification_report workflows are so common for multiclass problems: they make the class dimension explicit.

Macro, micro, and weighted averages are different summaries

Once you have class-wise metrics, you may also see:

  • macro average: unweighted average across classes
  • weighted average: average weighted by class support
  • micro average: computed from globally aggregated counts

Those are useful summaries, but they should not replace the actual per-class numbers when class-specific behavior matters.

Common Pitfalls

  • Looking only at accuracy and missing class-specific failures.
  • Forgetting to convert softmax outputs to class IDs with argmax.
  • Expecting built-in Keras precision and recall metrics to automatically give per-class results in the desired format.
  • Mixing one-hot labels and integer labels without converting consistently.
  • Reporting only macro averages when stakeholders need per-class detail.

Summary

  • For multiclass TensorFlow models, class-wise precision and recall are usually computed after prediction.
  • Convert softmax outputs with argmax, then build a confusion matrix.
  • Precision and recall for each class come directly from the confusion matrix counts.
  • 'sklearn.metrics.classification_report is often the easiest reporting tool.'
  • Keep per-class metrics visible instead of relying only on global accuracy or averages.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design