【问题标题】:how to pass an array from c++ to python function and retrieve python returned array to c++如何将数组从 c++ 传递给 python 函数并将 python 返回的数组检索到 c++
【发布时间】:2012-03-29 09:40:49
【问题描述】:

我正在编写一个 c++ 程序来调用 python 函数并检索返回数组。但我总是收到如下错误:

只有长度为 1 的数组可以转换为 Python 标量

和我的 C++ 代码:

 int main(int argc, char *argv[])
{
    int i;
    PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;

    if (argc < 3) 
    {
        printf("Usage: exe_name python_source function_name\n");
        return 1;
    }

    // Initialize the Python Interpreter
    Py_Initialize();

    // Build the name object
    pName = PyString_FromString(argv[1]);

    // Load the module object
    pModule = PyImport_Import(pName);

    // pDict is a borrowed reference 
    pDict = PyModule_GetDict(pModule);

    // pFunc is also a borrowed reference 
    pFunc = PyDict_GetItemString(pDict, argv[2]);

    if (PyCallable_Check(pFunc)) 
    {
        // Prepare the argument list for the call
        if( argc > 3 )
        {
                pArgs = PyTuple_New(argc - 3);
                for (i = 0; i < argc - 3; i++)
                {
                    pValue = PyInt_FromLong(atoi(argv[i + 3]));
                    if (!pValue)
                    {
                        PyErr_Print();
                        return 1;
                    }
                    PyTuple_SetItem(pArgs, i, pValue);  
                }

                pValue = PyObject_CallObject(pFunc, pArgs);

                if (pArgs != NULL)
                {
                    Py_DECREF(pArgs);
                }
        } else
        {
                pValue = PyObject_CallObject(pFunc, NULL);
        }

        if (pValue != NULL) 
        {
            cout <<pValue;
            printf("Return of call : %ld\n", PyInt_AsLong(pValue));
            PyErr_Print();
            Py_DECREF(pValue);
        }
        else 
        {
            PyErr_Print();
        }
    } else 
    {
        PyErr_Print();
    }

    // Clean up
    Py_DECREF(pModule);
    Py_DECREF(pName);

    // Finish the Python Interpreter
    Py_Finalize();

    system("PAUSE");
    return 0;
}

Python 函数:

 import numpy as np
    _ZERO_THRESHOLD = 1e-9      # Everything below this is zero
def data():
    print "process starting..."
    N = 5
    obs = np.matrix([np.random.normal(size=5) for _ in xrange(N)])              
    V = pca_svd(obs)
    print "V:"
    print V[0:5]

    pca = IPCA(obs.shape[1], 3)
    for i in xrange(obs.shape[0]):
        x = obs[i,:].transpose()
        print " "
        print "new input:"
        print x
        pca.update(x)

    U = pca.components
    A = pca.variances
    B = U.T*x
    print B
    return B

我知道这句话有问题

PyInt_AsLong(pValue) 谁能告诉我如何解决这个问题,以便将矩阵从 python 检索到 c++

非常感谢。

【问题讨论】:

标签: c++ python c numpy


【解决方案1】:

您正在使用 PyInt_AsLong(pValue) 将 Python 对象 pValue 转换为 C long 标量。如果pValue 是一个 Numpy 数组,这意味着您正在尝试将数组转换为数字,这仅适用于长度为 1 的数组:

只有长度为 1 的数组可以转换为 Python 标量

不使用PyInt_AsLong,而是使用Numpy's C API提供的PyArray_*函数来访问数据;特别是,请参阅Array API 部分。

【讨论】:

  • 如果你在这里提供一个最小的例子就好了,但是 +1 表示正确的方向。
【解决方案2】:

您想使用 NumPy C API,可能是为了获取数据指针

#include <numpy/arrayobject.h>
...
p = (uint8_t*)PyArray_DATA(pValue);

在确定您确实获得了正确尺寸的数组之后。有关示例代码,请参阅我的hello.hpp

【讨论】:

    猜你喜欢
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 2021-06-08
    • 2019-06-20
    • 2020-03-02
    • 1970-01-01
    相关资源
    最近更新 更多