EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Keras Tutorial keras.utils.to_categorical
Secondary Sidebar
Keras Tutorial
  • Basic
    • What is Keras?
    • Keras Install
    • Keras Applications
    • Keras Sequential
    • Keras Model Predict
    • Keras Save Model
    • Keras conv2D
    • Keras ImageDataGenerator
    • Keras input
    • Keras Datasets
    • Keras Early Stopping
    • Keras input
    • Keras Model Save
    • Keras LSTM Example
    • Keras Flatten
    • Keras Optimizers
    • Keras Layers
    • Keras Dense
    • Keras fit
    • Keras Model
    • Keras Metrics
    • Keras Batch Normalization
    • Keras CNN
    • Keras predict
    • Keras Dropout
    • Keras Embedding
    • Keras LSTM
    • Keras GPU
    • Keras Tuner
    • Keras VGG16
    • Keras Generator
    • Keras Pre-trained Models
    • Keras Custom Loss Function
    • keras.utils.to_categorical
    • Keras Neural Network
    • Keras Preprocessing
    • Keras Regularization
    • Keras Softmax
    • Keras Regression
    • Keras MaxPooling2D
    • Keras U-Net
    • Keras Initializers
    • Keras Transformer
    • Keras Data Augmentation
    • Keras ResNet50
    • Keras Verbose
    • Keras Plot Model
    • Keras OCR
    • Keras Utils Sequence
    • Keras Binary Classification
    • Keras Padding
    • UpSampling2d
    • Keras EfficientNet
    • Keras pad_sequences

keras.utils.to_categorical

keras.utils.to_categorical

Introduction to keras.utils.to_categorical

keras.utils.to_categorical provides the numpy utility library which provides functions for performing actions onto the arrays of numpy. By using the method of to_categorical() vector numpy array with an integer that represents the different categories was converted into a numpy array which binary contains the matrix values for the number of data categories. We can use the utilities to create the keras program fully deterministic and useful for user applications.

Key Takeaways

  • We need to use the three-parameter while using the keras utils to_categorical function. By using the to_categorical function we can create our own vector also we use the train and test labels.
  • The keras utils to_categorical function will be returning the output in 0 or 1 format which contains binary.

Overview of keras.utils.to_categorical

We can set all random seeds for the python program. The function of to_categorical is used to convert the class vector to the matrix of the binary class. We can apply limitations in cases where we are involving the network communications which was creating additional sources of randomness while non-deterministic apps are involved. The to_categorical function is useful while converting the binary class matrix.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

The easiest approach for dealing with the variables which are categorical is removing the same from the dataset. The remove variable approach of the dataset will work when our column does not contain the information which was useful. This assumption will make the sense because it contains the indisputable ranking from the categories.

All the categorical variable ordering into the values which contains the ordinal variables. For free-based models, we can accept the ordinal variables. For the free-based models, we can accept the ordinal encoding which was tree-based with the variables which were ordinal. The on-hot encoding into the to_categorical variables will create a new column which was indicating the presence of possible values in the original data. Into the ordinal encoding, the encoding which was hot will not assume category ordering. So we can accept this approach to work on the same.

keras.utils.to_categorical Parameters

As we have seen in the above point keras utils to_categorical is used to convert the binary class matrix. Below syntax shows that keras utils to_categorical variable function are as follows. In the below example, we can see that keras utils to_categorical uses three parameters while using the same in our program.

The keras utils to_categorical uses the tensorflow module to use the categorical parameters in our program we need to install the tensorflow module in our system. The keras utils to_categorical all the parameter is important while developing code.

Below is the syntax of keras utils to_categorical as follows:

Syntax:

to_categorical(y, dtype, num_classes)

In the above example, tf is nothing but the object of the tensorflow module. We can also see that we have to use the to_categorical function with keras for defining the parameters.

Below is the function which we need to use while using keras as follows:

1. Y – The y parameter we can also say that it is the input vector of the to_categorical function. This is nothing but the input vector which contains the integer which was representing the various classes in the specified data. This is also a class vector that converts into the matrix. We can convert the integer from 0 to num_classes. The parameter y into the to_categorical function is an array-like class value that was used to convert the same into the matrix.

2. num_classes – This parameter is defined as the number of classes that we are using. It will define the total number of classes. If suppose we have not mentioned a number of classes then the to_categorical function will consider as an input vector which was the largest number and add 1 value to the number of classes. The num_classes is a required parameter in a to_categorical function in keras. We can also say if we have not defined any value it will be referred to as max (y) + 1.

3. dtype – This parameter is desired data type of the variable which contains the output. If suppose we have not defined any value of this parameter then the default value of this parameter is float32. This parameter is expected by user input.

The keras utils to_categorical function will return the binary value matrix which contains the values either 0 or 1. It contains an equal number of rows from the length of the input vector and column number which was equal to the class number which we have defined in our code.

Examples of keras.utils.to_categorical

Given below are the examples mentioned:

Example #1

Below is the example of keras utils to_categorical as follows. We are using the training and testing label to define the example as follows. In the below example we are importing the keras dataset module also we are importing the keras utils module to define the example as follows. We are using the default num_classes and using the dtype float32.

Code:

from keras.datasets import cifar10
……
print(train_labels)
print(train_labels.shape)
print(test_labels)
print(test_labels.shape)
from keras.utils import to_categorical
label1 = to_categorical(train_labels, dtype = "float32")
label2 = to_categorical(test_labels, dtype = "float32")
print(label1)
print(label1.shape)
print(label2)
print(label2.shape)

Output:

keras.utils.to_categorical - testing label

keras.utils.to_categorical 2

Example #2

In the below example, we are using our own matrix value to define the example of to_categorical as follows.

Code:

from keras.datasets import cifar10
from keras.utils import to_categorical
cl_vect = [9, 17, 12, 15, 26, 21, 34, 12, 43, 51]
print (cl_vect)
op_mat = to_categorical (cl_vect, num_classes = 55, dtype = "int32")
print (op_mat)

Output:

keras.utils.to_categorical - matrix value

Example #3

Now, we are using num_classes as 60 and dtype as int32 to define the example of keras utils to_categorical as follows.

Code:

from keras.datasets import cifar10
from keras.utils import to_categorical
cl_vect =[9, 17, 12, 15, 26, 21, 34, 12, 43, 51]
print (cl_vect)
op_mat = to_categorical(cl_vect, num_classes = 60, dtype = "int32")
print (op_mat)

Output:

num_classes as 60 and dtype as int32

Example #4

We are using num_classes as 5 and dtype as float32 to define the example of keras utils to_categorical.

Code:

from keras.datasets import cifar10
from keras.utils import to_categorical
cl_vect = [3, 1, 4, 2]
print (cl_vect)
op_mat = to_categorical (cl_vect, num_classes = 5, dtype = "float32")
print (op_mat)

Output:

num_classes as 5 and dtype as float32

FAQ

Given below are the FAQs mentioned:

Q1. What is the use of keras utils to_categorical function?

Answer: The keras utils to_categorical function is used to convert the vector of class to the matrix of binary class. It will return output in 1 or 0 formats.

Q2. Which libraries do we need to import while using utils to_categorical function in keras?

Answer: We need to import the keras dataset and keras utils library while using utils to_categorical function in keras.

Q3. How many parameters we are defining while using utils to_categorical function in keras?

Answer: We are using three parameters i.e. y, num_classes, and dtype while using utils to_categorical function in keras.

Conclusion

We can use the utilities to create the keras program fully deterministic and useful for user applications. Keras.utils.to_categorical provide the numpy utility library which provides functions for performing actions onto the arrays of numpy. The function of to_categorical is used to convert the class vector to the matrix of the binary class.

Recommended Articles

This is a guide to keras.utils.to_categorical. Here we discuss the introduction, parameters, examples, and FAQ. You may also have a look at the following articles to learn more –

  1. Keras Tuner
  2. What is Keras GPU?
  3. Keras Embedding
  4. Keras predict
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more