python vec,传递一个C ++的std ::矢量,以numpy的数组在Python

I am trying a pass a vector of doubles that I generate in my C++ code to a python numpy array. I am looking to do some downstream processing in Python and want to use some python facilities, once I populate the numpy array. One of the biggest things I want to do is to be able to plot things, and C++ is a bit clumsy when it comes to that. Also I want to be able to leverage Python's statistical power.

Though I am not very clear as to how to do it. I spent a lot of time going through the Python C API documentation. I came across a function PyArray_SimpleNewFromData that apparently can do the trick. I still am very unclear as far as the overall set up of the code is concerned. I am building certain very simple test cases to help me understand this process. I generated the following code as a standlone Empty project in Visual Studio express 2012. I call this file Project1

#include

#include "C:/Python27/Lib/site-packages/numpy/core/include/numpy/arrayobject.h"

PyObject * testCreatArray()

{

float fArray[5] = {0,1,2,3,4};

npy_intp m = 5;

PyObject * c = PyArray_SimpleNewFromData(1,&m,PyArray_FLOAT,fArray);

return c;

}

My goal is to be able to read the PyObject in Python. I am stuck because I don't know how to reference this module in Python. In particular how do I import this Project from Python, I tried to do a import Project1, from the project path in python, but failed. Once I understand this base case, my goal is to figure out a way to pass the vector container that I compute in my main function to Python. I am not sure how to do that either.

Any experts who can help me with this, or maybe post a simple well contained example of some code that reads in and populates a numpy array from a simple c++ vector, I will be grateful. Many thanks in advance.

解决方案

Since there is no answer to this that is actually helpful for people that might be looking for this sort of thing I figured I'd put an easy solution.

First you will need to create a python extension module in C++, this is easy enough to do and is all in the python c-api documentation so i'm not going to go into that.

Now to convert a c++ std::vector to a numpy array is extremely simple. You first need to import the numpy array header

#include

and in your intialising function you need to import_array()

PyModINIT_FUNC

inittestFunction(void){

(void) Py_InitModule("testFunction". testFunctionMethods);

import_array();

}

now you can use the numpy array functions that are provided.

The one that you will want for this is as the OP said a few years back PyArray_SimpleNewFromData, it's stupidly simple to use. All you need is an array of type npy_intp, this is the shape of the array to be created. make sure it is the same as your vector using testVector.size(), (and for multiple dimensions do testVector[0].size(), testVector[0][0].size() ect. vectors are guaranteed to be continuous in c++11 unless it's a bool).

//create testVector with data initialised to 0

std::vector<:vector>> testVector;

testVector.resize(width, std::vector(height, 0);

//create shape for numpy array

npy_intp dims[2] = {width, height}

//convert testVector to a numpy array

PyArrayObject* numpyArray = (PyArrayObject*)PyArray_SimpleNewFromData(2, dims, NPY_UINT16, (uint16_t*)testVector.data());

To go through the paramaters. First you need to cast it to a PyArrayObject, otherwise it will be a PyObject and when returned to python won't be a numpy array.

The 2, is the number of dimensions in the array.

dims, is the shape of the array. This has to be of type npy_intp

NPY_UINT16 is the data type that the array will be in python.

you then use testVector.data() to get the data of the array, cast this to either void* or a pointer of the same data type as your vector.

Hope this helps anyone else who may need this.

(Also if you don't need pure speed I would advise avoiding using the C-API, it causes quite a few problems and cython or swig are still probably your best choices. There is also c types which can be quite helpful.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
std::list 和std::vector有什么区别? 回答: std::list和std::vector是C++中的两种不同的容器。它们在存储方式、访问效率、插入/删除效率和内存占用等方面有所不同。std::vector使用数组实现,将元素存储在连续的内存空间中,可以通过下标进行随机访问,访问效率较高。而std::list使用链表实现,元素可以分布在内存中的任意位置,不能进行随机访问,但在插入和删除操作上效率较高。std::vector要求预先分配足够的内存空间来存储所有元素,不能动态地增加或减少容器大小,而std::list可以根据实际情况动态地分配和释放内存。因此,如果需要大量进行随机访问,则推荐使用std::vector;如果需要经常进行插入和删除操作,则建议使用std::list。如果需要既能高效地执行插入/删除操作,又需要具备一定的随机访问能力,则可以考虑使用std::deque(双端队列)。\[1\] #### 引用[.reference_title] - *1* [c++std::list 与 std::vector的区别](https://blog.csdn.net/oHeHui1/article/details/130808625)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [std::vectorstd::list的区别](https://blog.csdn.net/c_shell_python/article/details/88753515)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [std::vectorstd::list的执行速度比较 (C/C++) (STL)](https://blog.csdn.net/ljx0305/article/details/4523408)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值