python - 为什么在 cython 函数中请求 numpy 数组的形状时,我得到 8 个维度?
问题描述
我有以下功能,
%%cython
cdef cytest(double[:,:] arr):
return print(arr.shape)
def pytest(arr):
return cytest(arr)
我使用以下 numpy 数组运行 pytest,
dummy = np.ones((2,2))
pytest(dummy)
我得到以下结果,
[2, 2, 0, 0, 0, 0, 0, 0]
解决方案
这是因为在 中C
,数组的形状是固定的。数组的最大维数cython
是 8。Cython 将数组的所有维数存储在这个固定长度的数组中。
这可以通过这样做来验证:
%%cython
cdef cytest(double[:,:,:,:,:,:,:,:,:] arr): # works up to 8 ':'
return arr.shape
def pytest(arr):
return cytest(arr)
编译它时,它会抛出这个错误:
Error compiling Cython file:
------------------------------------------------------------
...
cdef cytest(double[:,:,:,:,:,:,:,:,:] arr):
^
------------------------------------------------------------
/path/to/_cython_magic_9a9aea2a10d5eb901ad6987411e371dd.pyx:1:19: More dimensions than the maximum number of buffer dimensions were used.
这实质上意味着预设的最大维数为 8,我假设您可以通过更改 cython_magic 源文件来更改它。
推荐阅读
- c++ - 删除移动构造函数并从右值构造对象
- javascript - paper.js 清除光栅图像并加载新图像
- c# - 4.4 Bot Framework 中的 AzureBlobStorage 实现
- javascript - Mongoose - 后保存挂钩未触发
- android - atom文本编辑器适合写Java编程和android吗?
- mysql - MariaDB 安装失败,没有可用的软件包
- python - 将 django loader.render_to_string() 转换为用于 sendgrid 的烧瓶
- git - 卸载和安装 Homebrew 时出现 Git 错误
- go - 如何漂亮地打印 Golang 结构?
- c# - 无法运行 db2 查询?