One-hot encoding

For the one-hot encoding of the response variables, we use the following code:

# Labels
trainy <- c(0,0,0,1,1,1,2,2,2)
validy <- c(0,1,2)
testy <- c(0,0,1,1,2,2)

# One-hot encoding
trainLabels <- to_categorical(trainy)
validLabels <- to_categorical(validy)
testLabels <- to_categorical(testy)
trainLabels

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

From the preceding code, we can see the following:

  • We have stored target values for each image in trainy , validy, and testy, where 0, 1, and 2 indicate bicycle, car, and airplane images respectively.
  • We carry out one-hot encoding of trainy , validy, and testy by using the to_categorical function. One-hot encoding here helps to convert a factor variable into a combination of zeros and ones.

Now we have the data in a format that can be used for developing a deep neural network classification model, and that is what we will do in the next section.

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

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