1

So I was following some of the examples provided on this post: How to one-hot-encode sentences at the character level?

And they seem to hot encoding at character level. However, I am not able to figure out to hot encode at character level for a string with ints in it.

for example:

"hello" # h=7, e=4 l=11 o=14

would be:

[[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
 [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

I was able to achieve that with the help of the post I mentioned above. But would someone be able to help me hot encoding the following:

"Hello0311"

Any help and guidance is highly apprecaited

1 Answers1

1

You can use the singleton encoding function provided by Keras directly. Something like this:

import numpy as np
from keras.utils import np_utils
y_train_label = [7,4,11,11,14]
y_train_label_onehot = np_utils.to_categorical(y_train_label)
print('one_hot:',y_train_label_onehot)

The results:Figure one hot

Wendong Zheng
  • 276
  • 2
  • 10