Python/Numpy array dimension confusion
<p>Suppose <code>batch_size = 64</code>.
I created a batch : <code>batch = np.zeros((self._batch_size,), dtype=np.int64)</code>. Suppose I have batch of chars such that <code>batch = ['o', 'w', ....'s']</code> of 64 size and <code>'o'</code> will be represented as <code>[0,0, .... 0]</code> 1-hot vector of size 27.
So, is there any way such that batch will still have shape of batch_size and not batch_size x vocabulary_size?
Code is as follows :</p>

<pre><code>batch = np.zeros((self._batch_size,), dtype=np.int64)  
temp1 = list()
for b in range(self._batch_size):
  temp = np.zeros(shape=(vocabulary_size), dtype=np.int64)
  temp[char2id(self._text[self._cursor[b]])] = 1.0
  temp1.append(temp)
  self._cursor[b] = (self._cursor[b] + 1) % self._text_size
batch = np.asarray(list)
return batch
</code></pre>

<p>This return batch as dimension of batch_size x vocabulary_size.</p>

<pre><code>batch = np.zeros((self._batch_size,), dtype=np.int64)  
for b in range(self._batch_size):
  batch[b, char2id(self._text[self._cursor[b]])] = 1.0
  self._cursor[b] = (self._cursor[b] + 1) % self._text_size
return batch
</code></pre>

<p>This code returns an error of too many few indices.<br>
Is there any way of specifying array size as <code>[batch_size :, None]</code>?</p>

<p>In the 1st block the initialization of <code>batch</code> to <code>zeros</code> does nothing for you, because <code>batch</code> is replaced with the <code>asarray(temp1)</code> later. (Note my correction).  <code>temp1</code> is a list of 1d arrays (<code>temp</code>), and produces a 2d arrray.</p>

<p>In the 2nd if you start with <code>batch=np.zeros((batch_size, vocab_size))</code> you would avoid the index number error.</p>

<p>You can't use <code>None</code> instead of a real integer.  <code>None</code> does not work like a broadcasting <code>newaxis</code> here. Arrays don't grow by assigning a new larger index.  Even when used in indexing <code>np.zeros((batchsize,))[:,None]</code> the result is 2d, shape (batchsize,1).</p>

<p>Why do you want a 1d array?  It's possible to construct a 1d array of dtype object that contains arrays (or any other object), but for many purposes it is just a glorified list.</p>

python numpy