How can I initialize an empty Numpy array with a given number of dimensions?
<p>I basically want to initialize an empty 6-tensor, like this:</p>

<pre><code>a = np.array([[[[[[]]]]]])
</code></pre>

<p>Is there a better way than writing the brackets explicitly?</p>

<blockquote>
  <p>Iteratively adding rows of that rank-1 using np.concatenate(a,b,axis=0)</p>
</blockquote>

<p>Don't.  Creating an array iteratively is slow, since it has to create a new array at each step.  Plus <code>a</code> and <code>b</code> have to match in all dimensions except the concatenation one.</p>

<pre><code>np.concatenate((np.array([[[]]]),np.array([1,2,3])), axis=0)
</code></pre>

<p>will give you dimensions error.</p>

<p>The only thing you can concatenate to such an array is an array with size 0 dimenions</p>

<pre><code>In [348]: np.concatenate((np.array([[]]),np.array([[]])),axis=0)
Out[348]: array([], shape=(2, 0), dtype=float64)
In [349]: np.concatenate((np.array([[]]),np.array([[1,2]])),axis=0)
------
ValueError: all the input array dimensions except for the concatenation axis must match exactly
In [354]: np.array([[]])
Out[354]: array([], shape=(1, 0), dtype=float64)
In [355]: np.concatenate((np.zeros((1,0)),np.zeros((3,0))),axis=0)
Out[355]: array([], shape=(4, 0), dtype=float64)
</code></pre>

<p>To work iteratively, start with a empty list, and <code>append</code> to it; then make the array at the end.</p>

<p><code>a = np.zeros((1,1,1,1,1,0))</code> could be concatenated on the last axis with another <code>np.ones((1,1,1,1,1,n))</code> array.</p>

<pre><code>In [363]: np.concatenate((a,np.array([[[[[[1,2,3]]]]]])),axis=-1)
Out[363]: array([[[[[[ 1.,  2.,  3.]]]]]])
</code></pre>

python arrays numpy