Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Function

tf.keras.utils.to_categorical

Converts integer class labels into a binary class matrix. The class count can be supplied explicitly or inferred from the largest label.

How it works

The function takes integer class labels and creates a NumPy matrix whose rows encode the corresponding classes. The number of columns comes from num_classes, or, when omitted, from one more than the largest label; the class dimension is placed last.

Syntax

tf.keras.utils.to_categorical(x, num_classes=None)

Parameters

NameTypeDescription
xArray-likeArray-like integer class values to convert; values range from 0 through num_classes - 1.
num_classesint or NoneTotal number of classes. If None, inferred as max(x) + 1. Defaults to None.

Returns

A binary matrix representation of the input as a NumPy array, with the class axis last.

Key points

  • Transforms integer class labels into one-hot-style binary rows.
  • x accepts array-like input containing the class values.
  • num_classes declares the complete class count when provided.
  • The output is a NumPy array rather than a TensorFlow tensor.
  • For multidimensional inputs, the class dimension is appended as the final axis.
  • One-hot targets can be used with categorical crossentropy.

Common mistakes

  • Leaving num_classes unset derives the class count from the largest value in x, so labels must provide the intended range.
  • Input labels must be integer class values within the zero-based range implied by num_classes.

Example: Convert integer labels to one-hot rows

import numpy as np
import tensorflow as tf

labels = np.array([0, 1, 2, 1])
one_hot = tf.keras.utils.to_categorical(labels, num_classes=3)
print(one_hot)

Converts integer class labels into rows of a three-column one-hot matrix.

When to use it

Create one-hot training labels

Preparing targets for a multiclass neural-network output.

Prepare categorical-crossentropy targets

Matching target representation to a multiclass probability loss.

Build binary class features

Representing class indices as fixed-width vectors for downstream NumPy processing.

Interview questions

QWhat does tf.keras.utils.to_categorical do?

It converts an integer class vector into a binary, or one-hot, class matrix. Each input label becomes a row with one active class position.

Follow-up: What determines the output width? If num_classes is supplied, it determines the width; otherwise TensorFlow infers it as max(x) + 1.

QHow is num_classes handled?

num_classes specifies the total number of classes and defaults to None. When it is None, the function infers the value from the maximum input label plus one.

Follow-up: Why can inference be unsafe for a dataset split? A split that omits the highest class produces a narrower matrix than the full label space, so pass the global class count explicitly.

QWhat does the function return, and where is the class axis?

The result is a NumPy array containing the binary matrix representation of the input. Its class axis is the final axis, making it suitable for losses such as categorical crossentropy.

Follow-up: When should labels remain integer-valued instead? Use an integer-label loss when the model and loss expect sparse targets, avoiding one-hot expansion.

  • tf.keras.utils
  • keras.ops.categorical_crossentropy

Source: tf.keras.utils.to_categorical | TensorFlow v2.16.1. Checked 2026-10-15.