numpy ndarray with more that 32 dimensions
<p>When I try to create an numpy array with more than 32 dimensions I get an error: </p>

<pre><code>import numpy as np

np.ndarray([1] * 33)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-2-78103e601d91&gt; in &lt;module&gt;()
----&gt; 1 np.ndarray([1] * 33)

ValueError: sequence too large; cannot be greater than 32
</code></pre>

<p>I found this: <a href="http://stackoverflow.com/questions/35192518/using-numpy-array-with-large-number-of-dimensions">Using numpy.array with large number of dimensions</a> to be related to this question but I want to do this without building my own version.</p>

<p>My use case:<br>
I am working with Joint Probability Distributions and I am trying to represent each variable on an axis so that computations on it (marginalize, reduce) is a single line operation. For example for a marginalize operation I can simply do a sum over the axis of that variable. For multiplication I can simply do a simple numpy multiplication (after checking if the axes are the same).</p>

<p>Is there a possible workaround this?</p>

<p>How about maintaining the data in a 1d, and selectively reshaping to focus on a given dimension</p>

<p>To illustrate</p>

<pre><code>In [252]: x=np.arange(2*3*4*5)
</code></pre>

<p>With full reshape</p>

<pre><code>In [254]: x.reshape(2,3,4,5).sum(2)
Out[254]: 
array([[[ 30,  34,  38,  42,  46],
        [110, 114, 118, 122, 126],
        [190, 194, 198, 202, 206]],

       [[270, 274, 278, 282, 286],
        [350, 354, 358, 362, 366],
        [430, 434, 438, 442, 446]]])
</code></pre>

<p>With partial reshape - same numbers, different result shape</p>

<pre><code>In [255]: x.reshape(6,4,5).sum(1)
Out[255]: 
array([[ 30,  34,  38,  42,  46],
       [110, 114, 118, 122, 126],
       [190, 194, 198, 202, 206],
       [270, 274, 278, 282, 286],
       [350, 354, 358, 362, 366],
       [430, 434, 438, 442, 446]])
</code></pre>

<p>I'm not going to test this on anything approaching 32+ dimensions.  As was noted in the comment, if many of the dimensions are larger than 1 the overall array size will be excessively large.</p>

python python-3.x numpy multidimensional-array python-2.x