传递一个np.float64_t
数据类型的numpy数组是可以正常工作的(如下),但我不能传递字符串数组。
以下是有效的代码示例:
# cython_testing.pyx
import numpy as np
cimport numpy as np
ctypedef np.float64_t dtype_t
cdef func1 (np.ndarray[dtype_t, ndim=2] A):
print A
def testing():
chunk = np.array ( [[94.,3.],[44.,4.]], dtype=np.float64)
func1 (chunk)
但是我无法使其工作: 我找不到适配NumPy字符串数据类型的匹配“类型标识符”。
# cython_testing.pyx
import numpy as np
cimport numpy as np
ctypedef np.string_t dtype_str_t
cdef func1 (np.ndarray[dtype_str_t, ndim=2] A):
print A
def testing():
chunk = np.array ( [['huh','yea'],['swell','ray']], dtype=np.string_)
func1 (chunk)
编译错误为:
Error compiling Cython file:
------------------------------------------------------------
ctypedef np.string_t dtype_str_t
^
------------------------------------------------------------
cython_testing.pyx:9:9: 'string_t' is not a type identifier
更新
经过查看 numpy.pxd
,我看到以下 ctypedef
语句。也许这已经足够说明我可以使用 uint8_t
并且假装一切正常,只要我能进行一些强制类型转换?
ctypedef unsigned char npy_uint8
ctypedef npy_uint8 uint8_t
只需看看转换的代价有多高。
dtype=object
可以加速更新答案,但到目前为止我发现它等同于不指定dtype
。您是如何测量100倍的加速值的? - Saullo G. P. Castroa = np.array(['some', 'strings', 'in', 'an', 'array'] * 10 ** 5)
。 - Ted Petrou