Table Of Contents

One-Hot-Encoding

About

One-Hot Encoding takes many different tags/labels/categories and "reduces" them into Zeros-and-Ones.
Take for example a list of colors, ['red','green','blue']:

redbluegreen
100
010
001

Red could look like 100.
Blue could look like 010.
Green could look like 001.

In [11]:
import tensorflow as tf
## Using Tensorflow
colors = ['red','green','blue']

def toListOfNums(input_list):
    unique_elements_count = len(set(input_list))
    return list(range(unique_elements_count))

oneHotReadyColors = toListOfNums(colors)
print(f'colors: {colors}')
print(f'oneHotReadyColors: {oneHotReadyColors}')
colors: ['red', 'green', 'blue']
oneHotReadyColors: [0, 1, 2]
In [12]:
oheColors = tf.one_hot(oneHotReadyColors, depth=len(colors))
oheColors
Out [12]:
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]], dtype=float32)>