作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
传递 dtype 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
只需要看看类型转换成本有多高。
最佳答案
对于 Cython 0.20.1,它使用 cdef np.ndarray
工作,无需指定数据类型和维数:
import numpy as np
cimport numpy as np
cdef func1(np.ndarray A):
print A
def testing():
chunk = np.array([['huh','yea'], ['swell','ray']])
func1(chunk)
关于python - 如何将字符串类型的 numpy 数组传递给 Cython 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11003037/
我是一名优秀的程序员,十分优秀!