# Keras: One-hot Encode/Decode Sequence Data

A one-hot encoding is a representation of categorical variables (e.g. cat, dog, rat) as binary vectors (e.g. [1,0,0], [0,1,0], [0,0,1]).

### Required Steps:

1. Map categorical values to integer values. For example:
• cat is mapped to 1,
• dog is mapped to 2, and
• rat is mapped to 3.
2. Represent each integer value as a binary vector that is all zero values except the index of the integer. For example:
• 0 is mapped to [1,0,0],
• 1 is mapped to [0,1,0], and
• 2 is mapped to [0,0,1].

### How to Perform One-hot Encoding/Decoding in Keras:

The wonderful Keras library offers a function called to_categorical() that allows you to one-hot encode your integer data. Here's how:

#### 1. Import Dependencies

import numpy as np
from keras.utils import to_categorical


#### 2. Create Toy Dataset

data = np.array([1, 5, 3, 8])
print(data)

[1 5 3 8]


#### 3. Encode

def encode(data):
print('Shape of data (BEFORE encode): %s' % str(data.shape))
encoded = to_categorical(data)
print('Shape of data (AFTER  encode): %s\n' % str(encoded.shape))
return encoded

encoded_data = encode(data)
print(encoded_data)

Shape of data (BEFORE encode): (4,)
Shape of data (AFTER  encode): (4, 9)

[[0. 1. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 1. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 1.]]


#### 4. Decode

def decode(datum):
return np.argmax(datum)

for i in range(encoded_data.shape[0]):
datum = encoded_data[i]
print('index: %d' % i)
print('encoded datum: %s' % datum)
decoded_datum = decode(encoded_data[i])
print('decoded datum: %s' % decoded_datum)
print()

index: 0
encoded datum: [0. 1. 0. 0. 0. 0. 0. 0. 0.]
decoded datum: 1

index: 1
encoded datum: [0. 0. 0. 0. 0. 1. 0. 0. 0.]
decoded datum: 5

index: 2
encoded datum: [0. 0. 0. 1. 0. 0. 0. 0. 0.]
decoded datum: 3

index: 3
encoded datum: [0. 0. 0. 0. 0. 0. 0. 0. 1.]
decoded datum: 8

That is All

# 🐶

If you enjoyed this post and want to buy me a cup of coffee...

The thing is, I'll always accept a cup of coffee. So feel free to buy me one.

Cheers! ☕️