小编典典

大量使用numpy.array

python

如果尝试在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

有没有简单的解决方法?

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


阅读 325

收藏
2021-01-20

共1个答案

小编典典

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

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

2021-01-20