9951 explained code solutions for 126 technologies


python-kerasHow do I use the to_categorical function from TensorFlow in Python to convert data into a format suitable for a neural network?


The to_categorical function from TensorFlow in Python is used to convert data into a format suitable for a neural network. It is used to convert an array of numerical values into a binary matrix, where each row corresponds to one of the possible categories.

For example, the following code snippet converts a list of labels into a binary matrix:

import tensorflow as tf

labels = [0, 1, 2, 3]

binary_matrix = tf.keras.utils.to_categorical(labels)

print(binary_matrix)

Output example

[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]

The code consists of the following parts:

  1. Importing the tensorflow library as tf.
  2. Creating a list of labels.
  3. Using the to_categorical function to convert the labels into a binary matrix.
  4. Printing the binary matrix.

The to_categorical function is useful for creating training data for a neural network. It is a convenient way to convert numerical values into a binary matrix, which can be used by the neural network to classify data.

Helpful links

Edit this code on GitHub