Explore Courses Blog Tutorials Interview Questions
+1 vote
in Machine Learning by (160 points)

Is Tensorflow similar to scikit learn's one hot encoder for processing of categorical data? Does using placeholder of tf.string behave as categorical data?

I can pre-process the data manaually prior sending to tensorflow, but having built in is very easy.

1 Answer

+3 votes
by (10.9k points)
edited by

tf.one_hot is one of the functions which can convert a set of sparse labels to a dense one-hot representation, as of TensorFlow 0.8. In some cases, tf.nn.sparse_softmax_cross_entropy_with_logits can be used to calculate cross-entropy directly on sparse labels instead of converting them to one-hot.In case you want to to do in the old primitive way refer to the following code which uses sparse-to-dense operator:

numLabel = 10

sprsLabel= tf.reshape(lBatch, [-1, 1])

deriveSize = tf.shape(lbatch)[0]

i = tf.reshape(tf.range(0, deriveSize, 1), [-1, 1])

conc = tf.concat(1, [i, sprsLabel])

outshape = tf.pack([deriveSize, numLabel])

labels = tf.sparse_to_dense(conc, outshape, 1.0, 0.0)

This gives a label as an output which is a one-hot matric having a size equal to batchsize x numLabel.

Hope this answer helps.

If you want to get professional experience in TensorFlowjoin this Machine Learning Course.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94.1k users

Browse Categories