Cython: Convert memory view to NumPy array Cython: Convert memory view to NumPy array numpy numpy

Cython: Convert memory view to NumPy array


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 npcimport numpy as npdef 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.