3

How can I create one hot encoding of words with each word represented by a sparse vector of vocab size and the index of that particular word equated to 1 , using tensorflow ?

something like

oneHotEncoding(words = ['a','b','c','d']) -> [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]] ?

Shadab Shaikh
  • 61
  • 3
  • 6
  • [here](https://stackoverflow.com/questions/33681517/tensorflow-one-hot-encoder) is an explanation of how to use one hot encoding in Tensorflow – AAA Aug 14 '17 at 22:01

1 Answers1

1

Scikits one hot encoder takes an int-array (http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html). Building on your example you could us a dictionary to map words to integers and go from there:

import numpy as np
from sklearn.preprocessing import OneHotEncoder
wdict = {'a': 0, 'b': 1, 'c': 2, 'd': 3}
dictarr = np.asarray(wdict.values()).reshape(-1, 1)
enc = OneHotEncoder()
enc.fit(dictarr)
enc.transform([[2]]).toarray()

which yields

array([[ 0.,  0.,  1.,  0.]])
GrimTrigger
  • 571
  • 1
  • 5
  • 10