Database Reference
In-Depth Information
print "Encoding of 'doctor': %d" %
all_occupations_dict['doctor']
print "Encoding of 'programmer': %d" %
all_occupations_dict['programmer']
You will see the following output:
Encoding of 'doctor': 2
Encoding of 'programmer': 14
Finally, we can encode the value of
programmer
. We will start by creating a
numpy
ar-
ray of a length that is equal to the number of possible occupations (k in this case) and
filling it with zeros. We will use the
zeros
function of
numpy
to create this array.
We will then extract the index of the word
programmer
and assign a value of
1
to the
array value at this index:
K = len(all_occupations_dict)
binary_x = np.zeros(K)
k_programmer = all_occupations_dict['programmer']
binary_x[k_programmer] = 1
print "Binary feature vector: %s" % binary_x
print "Length of binary vector: %d" % K
This will give us the resulting binary feature vector of length
21
:
Binary feature vector: [ 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
Length of binary vector: 21