使用numpy.array处理大量维度的数据

7

如果你试图在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不允许创建这样的数组?


1
如果你有这么多维度,要么你会有很多长度为1的维度可以删除,要么你将会遇到将如此大的数组装入内存的问题。numpy.zeros([2]*33)甚至无法适应32位地址空间。 - user2357112
@user2357112 是的,我有很多长度为1的维度,但是我不能移除它们。 - DLunin
@user2357112 有了这些维度,许多操作都变得简单了,而且(主要原因是)很多代码已经依赖于它。 - DLunin
1个回答

11

来自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
您可以更改这些定义并从源代码构建一个与您需求不兼容但适合您需要的版本。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接