9951 explained code solutions for 126 technologies


python-kerasHow do I use TensorFlow, Python, Keras, and utils to_categorical?


TensorFlow, Python, Keras, and utils to_categorical() are used together to create a neural network that can classify data into categories. The to_categorical() function is used to convert a numerical label vector to a binary class matrix. This is necessary for the neural network to interpret the data correctly.

Example code

import numpy as np
from keras.utils import to_categorical

# define a label vector
labels = np.array([1, 2, 3, 4])

# convert to binary class matrix
binary_labels = to_categorical(labels)

print(binary_labels)

Output example

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

The code above first imports numpy and the to_categorical() function from the keras.utils library. Then a label vector is defined with four numerical labels. Finally, the to_categorical() function is used to convert the numerical label vector to a binary class matrix. The output shows the converted binary class matrix.

Code explanation

  1. import numpy as np - imports the numpy library and assigns it the alias np.
  2. from keras.utils import to_categorical - imports the to_categorical() function from the keras.utils library.
  3. labels = np.array([1, 2, 3, 4]) - defines a label vector with four numerical labels.
  4. binary_labels = to_categorical(labels) - uses the to_categorical() function to convert the numerical label vector to a binary class matrix.
  5. print(binary_labels) - prints the converted binary class matrix.

Helpful links

Edit this code on GitHub