C++ 2D 矢量到 2D pybind11 数组
C++ 2D vector to 2D pybind11 array
我对C++相当陌生,并且正在努力解决pybind问题。我根本无法弄清楚如何说服 pybind 将 2D 矢量从 C++ 转换为 python 可读格式。
这就是我想到的代码:
py::array_t<float> to_matrix(std::vector<std::vector<float>> &vals)
{
int N = vals.size();
int M = 6;
py::array_t<float>({N, M}) arr;
for (int i = 0; (i < N); i++)
{
for (int j = 0; (j < M); j++)
{
arr[i][j] = vals[i][j];
};
};
return arr;
};
来自 C++ 的输入是一个包含 N 行和 6 列的向量向量,只是一个很长的数据点列表。理想情况下,我希望将输出作为numpy数组,但是任何python数据结构都可以(例如,列表列表(。
文档使它听起来很容易,但我无法弄清楚。我做错了什么?
提前感谢您的任何帮助。
这里有一些事情发生,但让我们从一个简单的示例开始。以下函数将从硬编码std::vector<std::vector<float>>
创建 2D 数组
py::array_t<float> to_matrix()
{
std::vector<std::vector<float>> vals = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15}
};
size_t N = vals.size();
size_t M = vals[0].size();
py::array_t<float, py::array::c_style> arr({N, M});
auto ra = arr.mutable_unchecked();
for (size_t i = 0; i < N; i++)
{
for (size_t j = 0; j < M; j++)
{
ra(i, j) = vals[i][j];
};
};
return arr;
};
PYBIND11_MODULE(foo, m)
{
m.def("to_matrix", &to_matrix);
}
需要注意两件事,首先,数组形状是数组的构造函数参数。其次是使用mutable_unchecked
获取可用于进行赋值的代理对象。
在您的情况下,向量的向量将来自C++代码中的其他地方。
但请注意,pybind11 还提供了用于包装容器(如std::vector
(的样板文件。这些在标头pybind11/stl_bind.h
中可用,并允许您执行此操作
std::vector<std::vector<float>> make_vector()
{
std::vector<std::vector<float>> vals = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15}
};
return vals;
}
PYBIND11_MODULE(foo, m)
{
py::bind_vector<std::vector<std::vector<float>>>(m, "FloatVector2D");
m.def("make_vector", &make_vector);
}
这样的对象不会完全等同于 numpy 数组(没有shape
属性等(
相关文章:
- 2D数组来自文本输入,中间有空格
- Pybind11:将元组列表从Python传递到C++
- 将值指定给向量(2D)的向量中的某个位置
- 如何使用用户输入在C++中正确填充2D数组
- 如何在C++中检查2D数组中负值的输入验证
- Windows/Cygwin - 不能使用 pybind11 - 犯错误
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 用pybind11包装C++抽象类时出错
- 在 2D 向量中使用第三个 [ ] 有什么意义?
- 使用Pybind11向Python公开Eigen::张量
- 四边形的 2D 旋转
- 打印第二列时的2d字符矢量打印空间
- 如何在C++中使用pybind11加载一个pickle python列表
- 使用JsonCpp将数据返回到带有pybind11的python会在python调用中产生Symbol not foun
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何在C++函数中声明静态 2D 数组?
- 我是 C++ 的初学者,我想知道如何在 2D 矢量中获取重复值
- 如何声明一个可以在整个程序中使用的全局 2d 3d 4d .. 数组(堆版本)变量?
- C++ 2D 矢量到 2D pybind11 数组
- 用pybind11铸造2D阵列