how can i use categorical one-hot labels for training with keras?

  • Last Update :
  • Techknowledgy :

Your model should look like this:

from keras.layers.wrappers
import TimeDistributed

model = Sequential()
model.add(LSTM(100, input_dim = num_features, return_sequences = True))
model.add(TimeDistributed(Dense(1, activation = 'sigmoid')))

This can be easily spotted when examining the summary of the model:

print(model.summary())

Suggestion : 2

The Keras API provides a to_categorical() method that can be used to one-hot encode integer data. If the integer data represents all the possible values of the classes, then the to_categorical() method can be used directly; otherwise, the number of classes can be passed to the method as the num_classes parameter.,One-hot encoding is the representation of categorical variables as binary vectors. In Python, there are several ways to perform one-hot encoding on categorical data:,The code snippet below illustrates the usage of the to_categorical() method:,Let’s have a look at how one-hot encoding can be performed in Keras.

import numpy as np
from keras.utils
import to_categorical
# # # Categorical data to be converted to numeric data
colors = ["red", "green", "yellow", "red", "blue"]

# # # Universal list of colors
total_colors = ["red", "green", "blue", "black", "yellow"]

# # # map each color to an integer
mapping = {}
for x in range(len(total_colors)):
   mapping[total_colors[x]] = x

# integer representation
for x in range(len(colors)):
   colors[x] = mapping[colors[x]]

one_hot_encode = to_categorical(colors)
print(one_hot_encode)

Suggestion : 3

Last Updated : 21 Jun, 2022,GATE CS 2021 Syllabus

Output:

array(['Male', 'Female'], dtype = object)
array(['Nice', 'Good', 'Great'], dtype = object)
16._
   Employee_Id Gen_new Rem_new Gender_Female Gender_Male Remarks_Good Remarks_Great Remarks_Nice
   0 45 1 2 0 1 0 0 1
   1 78 0 0 1 0 1 0 0
   2 56 0 1 1 0 0 1 0
   3 12 1 1 0 1 0 1 0
   4 7 0 2 1 0 0 0 1
   5 68 0 1 1 0 0 1 0
   6 23 1 0 0 1 1 0 0
   7 45 0 2 1 0 0 0 1
   8 89 1 1 0 1 0 1 0
   9 75 0 2 1 0 0 0 1
   10 47 0 0 1 0 1 0 0
   11 62 1 2 0 1 0 0 1