使用具有大量维度的numpy.array

wrw*_*rwt 4 python numpy

如果您尝试在numpy中创建具有大量维度的数组,则会引发异常:

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
Run Code Online (Sandbox Code Playgroud)

这有什么简单的解决方法吗?

numpy不允许创建此类数组的原因是什么?

Jai*_*ime 8

来自NumPy源代码:

/*
 * 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
Run Code Online (Sandbox Code Playgroud)

您可以更改这些定义并从源代码构建符合您需求的不兼容版本.