如何将numpy字符串类型的数组传递给Cython中的函数 [英] How to pass a numpy array of string types to a function in Cython
问题描述
传递一个dtype np.float64_t
的numpy数组可以正常工作(如下),但是我不能传递字符串数组.
Passing a numpy array of dtype np.float64_t
works fine ( below), but I can't pass string arrays.
这是可行的方法:
# 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字符串dtypes的匹配类型标识符".
But I can't make this work: I can't find the matching 'type identifiers' for numpy string dtypes.
# 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
并假装一切正常,只要我可以进行强制转换?
Per looking through numpy.pxd
, I see the following ctypedef
statements. Maybe that's enough to say I can use uint8_t
and pretend everything is normal, as long as I can do some casting?
ctypedef unsigned char npy_uint8
ctypedef npy_uint8 uint8_t
只需要看一下铸件的昂贵程度即可.
Just have to see how expensive that casting will be.
推荐答案
看起来您不走运.
http://cython.readthedocs.org/en/latest /src/tutorial/numpy.html
尚不支持某些数据类型,例如布尔数组和字符串数组.
Some data types are not yet supported, like boolean arrays and string arrays.
此答案不再有效,如Saullo Castro的答案所示,但出于历史目的,我将其保留.
This answer is no longer valid as shown by Saullo Castro's answer, but I'll leave it for historical purposes.
这篇关于如何将numpy字符串类型的数组传递给Cython中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!