大量使用numpy.array

发布于 2021-01-29 14:10:00

如果尝试在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不允许创建此类数组的原因是什么?

关注者
0
被浏览
190
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    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
    

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



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看