使用Cython时缺少numpy属性

时间:2018-10-27 22:28:30

标签: python numpy cython

我有一个非常简单的cython模块,名为empty_test.pyx

cimport numpy as cnp


cpdef return_empty():
    return cnp.empty(0, dtype=np.int32)

当我尝试运行return_empty时,出现此错误:

empty_test.pyx:5:14: cimported module has no attribute 'empty'

这是我的setup.py文件:

from distutils.core import setup
from Cython.Build import cythonize

import numpy as np
setup(
    ext_modules=cythonize(['empty_test.pyx'],
    ),
    include_dirs = [np.get_include()],
)

我知道我可以尝试使用import numpy as np而不是cimport numpy as np,但是我正在尝试使用numpy代码的C版本。

1 个答案:

答案 0 :(得分:3)

为了实现这一点,您必须直接访问numpy的C-API,至少部分由Cython包装。您需要的PyArray_SimpleNew已经是cimported with numpy

因此,您的功能将变为:

%%cython
cimport numpy as cnp

cnp.import_array()  # needed to initialize numpy-API

cpdef return_empty():
    cdef cnp.npy_intp dim = 0
    return cnp.PyArray_SimpleNew(1, &dim, cnp.NPY_INT32)

现在:

>>> return_empty()
array([], dtype=int32)

很明显,由于引用计数的原因,仍然存在一些Python开销,但与使用np.empty()时相比,它要少得多:

>>> %timeit return_empty()   
159 ns ± 2.81 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
>>> %timeit return_empty_py
751 ns ± 8.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

使用PyArray_SimpleNew也比使用Cython的array(被认为by you in another question)要快(大约3倍):

%%cython
from cython.view cimport array as cvarray

# shape=(0,) doesn't work
cpdef create_almost_empty_carray():
    return cvarray(shape=(1,), itemsize=sizeof(int), format="i")

因此:

>>> %timeit create_almost_empty_carray()
435 ns ± 5.85 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

已使用功能return_empty_py的列表:

%%cython
cimport numpy as cnp
import numpy as np

cpdef return_empty_py():
    return np.empty(0, np.int32)