Cython: Convert memory view to NumPy array

无人久伴 提交于 2019-12-03 10:34:07

You should just be able to use np.asarray directly on the memoryview itself, so something like:

np.asarray(my_memview)

should work. For example if your cython source file contains this:

import numpy as np
cimport numpy as np
def test(double[:,:] x):
    print type(x)
    print type(np.asarray(x))

Then after compiling it, you should be able to do the following from the python side:

a = np.random.normal(size=(5,5))
test(a)

Which produces the following output:

<type '_cython_magic_0182fca918bfa3618c3553e51b13e8ba._memoryviewslice'>
<type 'numpy.ndarray'>

Note: I'm using the cythonmagic extension for IPython.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!