Cython - 自定义python对象的类型

时间:2017-07-20 08:55:57

标签: performance numpy types cython

我试图在python中快速填充数组。 我使用的代码是:

`%%cython
# init array
cdef N = 1000000 # should return an int but bugged
cdef int[:,:] = np.empty((N,10))
# populate array
for i in xrange(N):
    if i % 10000 == 0:
        print i
    annoy_index[i,:] = t.get_nns_by_item(i,n=10, search_k=10000, include_distances=False)`

annoy_index是一个2d的整数数组。 t对象是来自pypi包的自定义对象,它没有明显的类型,它的方法t.get_nns_by_items返回一个整数列表。 有关如何使代码工作的任何想法? 谢谢 错误如下:

-----------------------------
Error compiling Cython file:
------------------------------------------------------------
...
cdef int N = 1000000
cdef int[:,:] annoy_index = np.empty((N,10))
                             ^
------------------------------------------------------------

/node2-stage/chamberlinm/.cache/ipython/cython/_cython_magic_83a8ecb650ff8c24ca387e9de72c36e9.pyx:2:30: undeclared name not builtin: np

Error compiling Cython file:
------------------------------------------------------------
...
cdef int[:,:] annoy_index = np.empty((N,10))
# kinda slow 
for i in xrange(N):
    if i % 10000 == 0:
        print i
    annoy_index[i,:] = t.get_nns_by_item(i,n=10, search_k=10000, include_distances=False)
                       ^
------------------------------------------------------------

/node2-stage/chamberlinm/.cache/ipython/cython/_cython_magic_83a8ecb650ff8c24ca387e9de72c36e9.pyx:7:24: undeclared name not builtin: t
In [10]:

0 个答案:

没有答案