If you try to make an array with large number of dimensions in numpy, it throws an exception:
In [1]: import numpy as np
In [2]: a = np.zeros((1,) * 33)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-32dc30f6e439> in <module>()
----> 1 a = np.zeros((1,) * 33)
ValueError: sequence too large; must be smaller than 32
Are there any simple workarounds to this?
What are the reasons why numpy doesn't allow creating such arrays?
In general numpy arrays can have more than one dimension. One way to create such array is to start with a 1-dimensional array and use the numpy reshape() function that rearranges elements of that array into a new shape.
resize() can create an array of larger size than the original array. To convert the original array into a bigger array, resize() will add more elements (than available in the original array) by copying the existing elements (repeated as many times as required) to fill the desired larger size.
On a regular 32bit system, this is (4294967295 / 2) / 4 or 536870912. Therefore the maximum size of a python list on a 32 bit system is 536,870,912 elements.
From the NumPy source code:
/*
* There are several places in the code where an array of dimensions
* is allocated statically. This is the size of that static
* allocation.
*
* The array creation itself could have arbitrary dimensions but all
* the places where static allocation is used would need to be changed
* to dynamic (including inside of several structures)
*/
#define NPY_MAXDIMS 32
#define NPY_MAXARGS 32
You could change those defines and build from source a non-compatible version that fits your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With