Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using numpy.array with large number of dimensions

Tags:

python

numpy

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?

like image 862
DLunin Avatar asked Feb 04 '16 04:02

DLunin


People also ask

How many dimensions can a Numpy array have?

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.

How do I make my Numpy array bigger?

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.

What is the largest array size in Python?

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.


1 Answers

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.

like image 140
Jaime Avatar answered Oct 23 '22 22:10

Jaime