[英]How to get data from np.array to std::vector in c++ using <numpy/arrayobject.h>?
This is my first question on this site. 这是我在本网站上的第一个问题。
First of all, I need to make a module with one function for python in C++, which must work with numpy, using <numpy/arrayobject.h>
. 首先,我需要使用<numpy/arrayobject.h>
为C ++中的python创建一个模块,该模块必须与numpy一起使用。 This function takes one numpy array and returns two numpy arrays. 此函数采用一个numpy数组,并返回两个numpy数组。 All arrays are one-dimensional. 所有数组都是一维的。
The first question is how to get the data from a numpy array? 第一个问题是如何从numpy数组中获取数据? I want to collect the information from array in std::vector, so then I can easily work with it C++. 我想从std :: vector中的数组中收集信息,因此我可以轻松地使用C ++来工作。
The second: am I right that function should return a tuple of arrays, then user of my module can write like this in python: arr1, arr2 = foo(arr)
? 第二个:我是对的,该函数应该返回一个数组元组,然后我模块的用户可以在python中这样写: arr1, arr2 = foo(arr)
? And how to return like this? 以及如何返回这样?
Thank you very much. 非常感谢你。
NumPy includes lots of functions and macros that make it pretty easy to access the data of an ndarray
object within a C or C++ extension. NumPy包含许多函数和宏 ,这些函数和宏使访问C或C ++扩展中的ndarray
对象的数据变得非常容易。 Given a 1D ndarray
called v
, one can access element i
with PyArray_GETPTR1(v, i)
. 给定一个称为v
一维ndarray
,可以使用PyArray_GETPTR1(v, i)
访问元素i
。 So if you want to copy each element in the array to a std::vector
of the same type, you can iterate over each element and copy it, like so (I'm assuming an array of double
s): 因此,如果要将数组中的每个元素复制到相同类型的std::vector
,则可以遍历每个元素并复制它,就像这样(我假设是double
数组):
npy_intp vsize = PyArray_SIZE(v);
std::vector<double> out(vsize);
for (int i = 0; i < vsize; i++) {
out[i] = *reinterpret_cast<double*>(PyArray_GETPTR1(v, i));
}
One could also do a bulk memcpy
-like operation, but keep in mind that NumPy ndarray
s may be mis-aligned for the data type, have non-native byte order, or other subtle attributes that make such copies less than desirable. 也可以执行类似批量memcpy
的操作,但是请记住,NumPy ndarray
可能针对数据类型未对齐,具有非本地字节顺序或其他细微属性,从而使此类副本不太理想。 But assuming that you are aware of these, one could do: 但是假设您知道这些,则可以执行以下操作:
npy_intp vsize = PyArray_SIZE(v);
std::vector<double> out(vsize);
std::memcpy(out.data(), PyArray_DATA(v), sizeof(double) * vsize);
Using either approach, out
now contains a copy of the ndarray
's data, and you can manipulate it however you like. 使用这两种方法, out
现在包含的副本ndarray
的数据,你可以操纵它,只要你喜欢。 Keep in mind that, unless you really need the data as a std::vector
, the NumPy C API may be perfectly fine to use in your extension as a way to access and manipulate the data. 请记住,除非您真的需要将数据作为std::vector
,否则NumPy C API可能会完美地用于扩展中,以作为访问和操作数据的方式。 That is, unless you need to pass the data to some other function which must take a std::vector
or you want to use C++ library code that relies on std::vector
, I'd consider doing all your processing directly on the native array types. 也就是说,除非您需要将数据传递给其他必须采用std::vector
函数,否则您要使用依赖于std::vector
C ++库代码,否则我将考虑直接在本机上进行所有处理数组类型。
As to your last question, one generally uses PyArg_BuildValue
to construct a tuple which is returned from your extension functions. 关于最后一个问题,通常使用PyArg_BuildValue
构造一个元组,该元组从您的扩展函数返回。 Your tuple would just contain two ndarray
objects. 您的元组将只包含两个ndarray
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.