One-hot encoding

After data partitioning, we will carry out a one-hot encoding of the response variable. One-hot encoding helps to represent a categorical variable in zeros and ones. The code and output for one-hot encoding is as follows:

# One-hot encoding
trainLabels <- to_categorical(trainingtarget)
testLabels <- to_categorical(testtarget)
print(testLabels[1:10,])

OUTPUT
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 1 0 0
## [3,] 1 0 0
## [4,] 0 0 1
## [5,] 0 0 1
## [6,] 0 1 0
## [7,] 1 0 0
## [8,] 1 0 0
## [9,] 1 0 0
## [10,] 1 0 0

As you can see in the preceding code, with the help of the to_categorical function from the Keras package, we convert the target variable to a binary class matrix, where the presence or absence of a class is simply represented by 1 or 0 respectively. In this example, we have three classes for the target variable, which are converted to three dummy variables. This process is also called one-hot encoding. First, 10 rows from testLabels are printed. The first row indicates the normal category for the patient with (1,0,0), the sixth row indicates the suspect category for the patient with (0,1,0), and the fourth row provides an example of the pathologic category of a patient with (0,0,1).

Once we complete these steps for data preparation, we move to the next step, where we create the classification model to classify a patient as normal, suspect, or pathological.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset