9951 explained code solutions for 126 technologies


python-kerasHow can I use the to_categorical attribute in the tensorflow.python.keras.utils module?


The to_categorical() attribute in the tensorflow.python.keras.utils module can be used to convert a class vector (integers) to binary class matrix. This is useful for working with categorical data in machine learning models.

Here is an example of using to_categorical():

from tensorflow.python.keras.utils import to_categorical

# example of class vector
y = [0, 1, 2, 2, 1]

# convert to binary class matrix
y_cat = to_categorical(y)
print(y_cat)

The output of this code is:

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

The to_categorical() attribute takes two parameters:

  • y: class vector to be converted into a matrix
  • num_classes: total number of classes

The y parameter is the class vector that needs to be converted to a binary class matrix. The num_classes parameter is the total number of classes in the data.

Helpful links

Edit this code on GitHub