返回Cython的阵列
我需要为大型缓冲区分配内存,而不实际对其进行初始化(结果以后将用作缓冲区,并且每个位置都将写入,因此初始化只是浪费时间)。
我可以在Cython中轻松地使用:
cdef char *buff = <char *> malloc(n * sizeof(char))
if not buff :
raise MemoryError()
问题是我不确定如何将其作为Ndarray返回到Python(类似于Numpy Array)。该文档显示了如何将其转换为列表并返回列表,但不是作为array.https://cython.readthedocs.io/en/latest/src/tutorial/memory_allocation.html
return [x for x in my_array[:number]]
返回列表并不是很有用,因为我希望将其解释为布尔数组(此缓冲区将用作过滤元素的掩码)。
# Index and coefficient array are both numpy ND-arrays
index_array = index_array[mask]
coeff_array = coeff_array[mask]
计算口罩后,如何将缓冲区作为布尔数组返回?
I need to allocate memory for a large buffer, without actually initializing it (it will be used as a result buffer later and every location will be written to, so initialization is just a waste of time).
I can easily do this in Cython using:
cdef char *buff = <char *> malloc(n * sizeof(char))
if not buff :
raise MemoryError()
The problem is I'm not sure how to return this as an ndarray to python (similar to numpy array). The documentation shows how to convert this to a list and return it, but not as an array.https://cython.readthedocs.io/en/latest/src/tutorial/memory_allocation.html
return [x for x in my_array[:number]]
Returning a list isn't very useful as I want this to be interpreted as a boolean array (this buffer will be used as a mask to filter out elements).
# Index and coefficient array are both numpy ND-arrays
index_array = index_array[mask]
coeff_array = coeff_array[mask]
how can I return the buffer as a boolean array after I calculated a mask?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论