Python Keras | keras.utils.to_categorical() - GeeksforGeeks

23 Jun, 2021
Keras provides numpy utility library, which provides functions to perform actions on numpy arrays. Using the method to_categorical(), a numpy array (or) a vector which has integers that represent different categories, can be converted into a numpy array (or) a matrix which has binary values and has columns equal to the number of categories in the data.
Syntax: tf.keras.utils.to_categorical(y, num_classes=None, dtype=”float32′′)
Parameters: 
y (input vector): A vector which has integers representing various classes in the data.

num_classes: Total number of classes. If nothing is mentioned, it considers the largest number of the input vector and adds 1, to get the number of classes.
Its default value is "None".

dtype: It is the desired data type of the output values. 
By default, it's value is 'float32'.
Output: This function returns a matrix of binary values (either ‘1’ or ‘0’). It has number of rows equal to the length of the input vector and number of columns equal to the number of classes.
Code: Converting Cifar10 dataset labels vector to categorical data matrix: 
Python3
# Loading the dataset from keras.datasets import cifar10(train_images, train_labels), (test_images, test_labels)= cifar10.load_data() # Labels before applying the function# Training set labelsprint(train_labels)print(train_labels.shape) # Testing set labelsprint(test_labels)print(test_labels.shape) # Applying the function to training set labels and testing set labelsfrom keras.utils import to_categoricaltrain_labels = to_categorical(train_labels, dtype ="uint8")test_labels = to_categorical(test_labels, dtype ="uint8") # Labels after applying the function# Training set labelsprint(train_labels)print(train_labels.shape) # Testing set labelsprint(test_labels)print(test_labels.shape)
Output: 
#Labels before applying the function
#Training set labels
array([[6],
       [9],
       [9],
       ...,
       [9],
       [1],
       [1]], dtype=uint8)

#Training set labels shape
(50000, 1)

#Testing set labels
array([[3],
       [8],
       [8],
       ...,
       [5],
       [1],
       [7]], dtype=uint8)

#Testing set labels shape
(10000, 1)

#Labels after applying the function
#Training set labels
[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 1]
 [0 0 0 ... 0 0 1]
 ...
 [0 0 0 ... 0 0 1]
 [0 1 0 ... 0 0 0]
 [0 1 0 ... 0 0 0]]

#Training set labels shape
(50000, 10)

#Testing set labels
[[0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 1. 0.]
 [0. 0. 0. ... 0. 1. 0.]
 ...
 [0. 0. 0. ... 0. 0. 0.]
 [0. 1. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 1. 0. 0.]]

#Testing set labels shape
(10000, 10)
Code: Considering an input vector with 7 classes. (It can have values ranging from 0 to 6(n-1)).
Python3
# Initializing Input vectorclass_vector =[2, 5, 6, 1, 4, 2, 3, 2]print(class_vector) # Applying the function on input class vectorfrom keras.utils import to_categoricaloutput_matrix = to_categorical(class_vector, num_classes = 7, dtype ="int32") print(output_matrix)
Output: 
[[0 0 1 0 0 0 0]
 [0 0 0 0 0 1 0]
 [0 0 0 0 0 0 1]
 [0 1 0 0 0 0 0]
 [0 0 0 0 1 0 0]
 [0 0 1 0 0 0 0]
 [0 0 0 1 0 0 0]
 [0 0 1 0 0 0 0]]
 
adnanirshad158
Machine Learning
Python
Machine Learning
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
ML | Linear Regression
Decision Tree
Reinforcement learning
Decision Tree Introduction with example
Python | Decision tree implementation
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe