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']:
| red | blue | green |
|---|---|---|
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 0 | 0 | 1 |
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}')In [12]:
oheColors = tf.one_hot(oneHotReadyColors, depth=len(colors))
oheColorsOut [12]: