Reputation: 990
If i have a categorical label like this
labels = [cat,dog, bird, cow]
now i want to convert it like one hot encoding. Is it possible by using tensorflow. like this
output_label = [[1 0 0 0]
[0 1 0 0]
[0 0 1 0]
[0 0 0 1]]
Upvotes: 1
Views: 979
Reputation: 10709
First you need to transform your categorical data to numerical format. You could do that for example like this:
def categorical_to_numerical(labels):
num_labels=[]
for k in labels:
if k == 'cat':
num_labels.append(0)
if k == 'dog':
num_labels.append(1)
if k == 'bird':
num_labels.append(2)
if k == 'cow':
num_labels.append(3)
return num_labels
print labels
// prints ['cat','dog', 'bird', 'cow', 'dog', 'bird']
print categorical_to_numerical(labels)
// prints [0, 1, 2, 3, 1, 2]
Now you can easily use a tensorflow built-in function called tf.one_hot
:
indices = categorical_to_numerical(labels)
detph = 4 // because you have four categories
one_hot_labels = tf.one_hot(indices, depth)
Read more about tf.one_hot
here.
Upvotes: 1