[英]C++ Python convert data from vector to ndarray
I am creating a C++ project which includes some python. 我正在创建一个包含一些python的C ++项目。 I have used
boost::python
but now I am stuck. 我使用过
boost::python
但现在我被卡住了。 I have some data stored in a std::vector
. 我有一些数据存储在
std::vector
。 The data should be send to python to do some calculations. 应该将数据发送到python进行一些计算。 The Python NumPy functions I use need the data in
ndarray
format, so I need a way to convert it. 我使用的Python NumPy函数需要
ndarray
格式的数据,所以我需要一种方法来转换它。 Since I want to do some more things with the results in C++ I want to convert the result back from ndarray
to std::vector
. 由于我想用C ++中的结果做更多的事情,我想将结果从
ndarray
转换回std::vector
。
How can I convert std::vector
to ndarray
and vice versa? 如何将
std::vector
转换为ndarray
,反之亦然? Can anyone post some sample code? 有人可以发布一些示例代码吗? I know there is a lib called
boost.numpy
out there, but are there other ways, because I was not able to build this lib... 我知道有一个名为
boost.numpy
的lib,但还有其他方法,因为我无法构建这个lib ...
Can I use boost::python::numeric
for the job? 我可以使用
boost::python::numeric
作业吗? Can anyone provide sample code? 有人可以提供示例代码吗?
Is it possible without copying data? 是否可以不复制数据?
Maybe you are willing to use the C Numpy API . 也许你愿意使用C Numpy API 。
You should look at C/C++ PyArray_SimpleNewFromData
to create brand new ndarray in your c++ code. 您应该查看C / C ++
PyArray_SimpleNewFromData
以在您的c ++代码中创建全新的 ndarray。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.