1
votes

I was trying to hot-encode data. Data is list of vocabulary_size = 17005207.

To hot-encode, I made a list of inputs of num_labels = 100. Following code:

inputs = []
for i in range(vocabulary_size):
  inputs.append(np.arange(num_labels) == data[i]).astype(np.float32)

Throws me an Error:

AttributeError: 'NoneType' object has no attribute 'astype'

I tried dtype = np.float32 inside append function but again erroneous.
When I try this :

inputs = []
for i in range(vocabulary_size):
  inputs.append(np.arange(num_labels) == data[i])
inputs = np.array(inputs,dtype=np.float32)

I get correct answer : Hot-Encoded Input Sequence of vocabulary_size x num_labels.

Any Alternative Solution Of One Line Without Using Numpy?

Solved :Can I be done directly using numpy array(input) with list(data)?

Info about data : data = np.ndarray(len(words), dtype=np.int32)

Reformat function:

  def reformat(data):
      num_labels = vocabulary_size
      print (type(data))
      data = (np.arange(num_labels) == data[:,None]).astype(np.int32)
      return data  
      print (data,len(data))
      return data

New Question : The dimension of data is (vocabulary_size,)...How to convert data using ravel or reshape into dimension of (1,vocabulary_size)?

1
Your error comes from the fact that you are trying to get the astype attribute from what is returned by the inputs.append() call and that is the None object. In other words you try to call None.astype() which clearly won't work. - jadsq
Calm down. With three edits so fast, one cannot answer your question since it changes every second. You seem to understand that ravel or reshape can help you: instead of hurrying to ask in SO, have you tried reading the numpy.reshape doc ? - YuppieNetworking
Sry @YuppieNetworking if it was too much.Just wanted to be clear.I did read the docs.Just wanted exact implementation as where should be parameters to reshape or ravel. - SupposeXYZ

1 Answers

0
votes

Not sure whether I've understood correctly what you're asking for, but if what you want is a oneliner, you could transform you're already working code into this:

inputs = np.array([np.arange(num_labels) == data[i] for i in range(vocabulary_size)], dtype=np.float32)