## 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.

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:**

#### 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:**

#### 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:**

#### 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:**

### 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 –