- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在做一个需要转换 ndarray
的项目在 Python 中为 vector
在 C++ 中,然后返回处理过的 vector
在 ndarray
中从 C++ 回到 Python .我正在使用 Boost.Python 及其 NumPy 扩展。我的问题具体在于从 ndarray
转换至 vector
,因为我正在使用扩展的 vector 类:
class Vector
{
public:
Vector();
Vector(double x, double y, double z);
/* ... */
double GetLength(); // Return this objects length.
/* ... */
double x, y, z;
};
ndarray
我收到的是n
x 2
并填充了 x,y 数据。然后我用一个函数在 C++ 中处理数据,该函数返回 std::vector<Vector>
.然后这个 vector 应该作为 ndarray
返回给 Python。 , 但只有 x 和 y 值。
我编写了以下代码,灵感来自“how to return numpy.array from boost::python?”和来自 Boost NumPy 示例的 gaussian.cpp。
#include <vector>
#include "Vector.h"
#include "ClothoidSpline.h"
#include <boost/python/numpy.hpp>
namespace py = boost::python;
namespace np = boost::python::numpy;
std::vector<Vector> getFineSamples(std::vector<Vector> data)
{
/* ... */
}
np::ndarray wrapper(np::ndarray const & input)
{
std::vector<Vector> data;
/* Python ndarray --> C++ Vector */
Py_intptr_t const* size = input.get_shape();
Py_intptr_t const* strides = input.get_strides();
double x;
double y;
double z = 0.0;
for (int i = 0; i < size[0]; i++)
{
x = *reinterpret_cast<double const *>(input.get_data() + i * strides[0] + 0 * strides[1]);
y = *reinterpret_cast<double const *>(input.get_data() + i * strides[0] + 1 * strides[1]);
data.push_back(Vector::Vector(x,y,z));
}
/* Run Algorithm */
std::vector<Vector> v = getFineSamples(data);
/* C++ Vector --> Python ndarray */
Py_intptr_t shape[1] = { v.size() };
np::ndarray result = np::zeros(2, shape, np::dtype::get_builtin<std::vector<Vector>>());
std::copy(v.begin(), v.end(), reinterpret_cast<double*>(result.get_data()));
return result;
}
编辑:我知道这是一次(可能)失败的尝试,与编辑我的代码相比,我对解决此问题的更好方法更感兴趣。
总结:
boost::python::numpy::ndarray
到 std::vector<Vector>
?std::vector<Vector>
到boost::python::numpy::ndarray
, 只返回 x 和 y?最后一点:我对 Python 几乎一无所知,而且我是 C++ 的初学者/中等水平。
最佳答案
我会考虑您的问题标题,以便为找到这篇文章的人提供更笼统的答案。
你有一个 boost::python::numpy::ndarray
称为 input
包含 doubles
并且您想将其转换为 std::vector<double>
称为 v
:
int input_size = input.shape(0);
double* input_ptr = reinterpret_cast<double*>(input.get_data());
std::vector<double> v(input_size);
for (int i = 0; i < input_size; ++i)
v[i] = *(input_ptr + i);
现在,你有一个 std::vector<double>
称为 v
并且您想将其转换回 boost::python::numpy::ndarray
的 doubles
称为 output
:
int v_size = v.size();
py::tuple shape = py::make_tuple(v_size);
py::tuple stride = py::make_tuple(sizeof(double));
np::dtype dt = np::dtype::get_builtin<double>();
np::ndarray output = np::from_data(&v[0], dt, shape, stride, py::object());
假设你正在包装这个函数,不要忘记在将它返回给 python 之前你需要创建一个对该数组的新引用:
np::ndarray output_array = output.copy();
关于python - 如何使用 Boost.Python 将 NumPy ndarray 转换为 C++ vector 并返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43758709/
在这个例子中,我有一个一维 ndarray 列表,长度为 9,列表有 9 个元素,每个元素有 shape=(2048,) ,所以完全9 * (2048,) , 我得到这些 ndarray来自 mxne
Python/NumPy 中的三种“全部”方法有什么区别?性能差异的原因是什么? ndarray.all() 是否总是三者中最快的? 这是我运行的计时测试: In [59]: a = np.full(
我似乎在 this 中遇到了 XY 问题关于如何判断数组是否共享相同内存的问题。我检查的方式是错误的,我不知道为什么。 举几个例子 test = np.ones((3,3)) test2 = np.a
我在使用 mxnet 进行基本 IO 方面遇到问题。我正在尝试使用 mxnet.io.NDArrayIter 读取内存数据集以在 mxnet 中进行训练。我有下面的代码(为了简洁而精简),它预处理代码
首先,这不是作业问题;而是作业问题。它是与我的工作相关的实际问题的抽象。我真的很感谢所有的意见! 我需要运行类似于下面的计算,按顺序运行数万次,它的计算时间显着影响我的模拟的总持续时间: 在这个抽象中
这个问题在这里已经有了答案: Index multidimensional array with index array (1 个回答) 关闭 2 年前。 我想根据一些坐标从 src ndarray
因此 numpy ndarray 非常方便,因为您只需为任何一维函数 f 和任何 ndarray A 输入 f(A) 即可,它将按元素应用 f。有人告诉我,这也是将函数应用于 ndarray 并避免
我有一个 ndarray 字符串。我想将它转换回 ndarray。我尝试了 newval = np.fromstring(val, dtype=float) 。但它给出了ValueError:字符串大
我正在 python/numpy 中做一些机器学习工作,其中我想用一维 ndarray 索引一个二维 ndarray,这样我就可以得到一个带有索引值的一维数组。 我让它与一些丑陋的代码一起工作,我想知
我想根据某些维度的索引位置数组提取 numpy ndarray 的一部分。让我用一个例子来说明这一点 示例数据 dummy = np.random.rand(5,2,100) X = np.array
这个问题在这里已经有了答案: Find the row indexes of several values in a numpy array (8 个答案) 关闭 2 年前。 我有 a = np.a
我想提取 numpy.ndarray 的第一个轴成为numpy.ndarray的列表. 例如,arr_A包含形状为 (3, 100, 200) 的 numpy ndarray,它将转换为形状为 (10
我一直在尝试用 ndarrays 的 numpy ndarray 转换数组数组。 这是我的数据类型: dt = 'i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,f8,i
我目前正在研究一种可以拆分 numpy.ndarray 的方法进入给定数量的子阵列,只要该数量小于窗口移动的轴。 示例: 给定一个形状为 (15, 40, 3) 的 numpy.ndarray 我想分
我用 arcpy 模块创建了一个 NumPy 结构化数组(称为 arr): arr = arcpy.da.FeatureClassToNumPyArray('MPtest','SHAPE@XYZ',e
是否可以将一个 numpy 数组的特定行的引用存储在另一个 numpy 数组中? 我有一个二维节点数组,例如 nodes = np.array([[1, 2], [2, 3], [3, 4], [4,
我有两个长度相同的 pandas 系列,如下所示: S1 = 0 -0.483415 1 -0.514082 2 -0.515724 3 -0.519375 4
当使用 numpy 时,假设我有一个任意的、以前创建的名为 my_ndarray 的 ndarray。如果可能的话,我希望能够执行以下操作...... my_bytes = my_ndarray.to
我在尝试使用 numpy 打乱多维数组时遇到问题。可以使用以下代码重现该问题: import numpy as np s=(300000, 3000) n=s[0] print ("Allocate"
当您调用 DataFrame.to_numpy() 时,pandas 将找到可以容纳 DataFrame 中所有数据类型的 NumPy 数据类型。但是如何进行反向操作呢? 我有一个“numpy.nda
我是一名优秀的程序员,十分优秀!