python - 将二维 C 数组传递给 python numpy

标签 python c++ arrays numpy

我需要一些关于将 C 数组传递给 python(numpy) 的帮助。 我有 double NumRows x NumInputs 的二维数组,似乎 PyArray_SimpleNewFromData 没有以正确的方式转换它 - 很难看到,因为调试器没有显示太多,只有指针。

传递二维数组的正确方法是什么?

int NumRows = X_test.size();
int NumInputs = X_test_row.size();

double **X_test2 = new double*[NumRows];
for(int i = 0; i < NumRows; ++i) 
{
    X_test2[i] = new double[NumInputs];
}


for(int r = 0; r < NumRows; ++r) 
{
    for(int c = 0; c < NumInputs; ++c) 
    {
        X_test2[r][c] = X_test[r][c];
    }
}




const char *ScriptFName = "100-ABN-PREDICT";
char *FunctionName=NULL;

FunctionName="PredictGBC_DBG"; 

npy_intp Dims[2];
Dims[0]= NumRows;
Dims[1] = NumInputs;

PyObject *ArgsArray;
PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pArgs;

int row, col, rows, cols, size, type;

const double* outArray;
double ArrayItem;

//===================

Py_Initialize();

pName = PyBytes_FromString(ScriptFName);

pModule = PyImport_ImportModule(ScriptFName);

if (pModule != NULL)
{
    import_array(); // Required for the C-API

    ArgsArray = PyArray_SimpleNewFromData (2, Dims, NPY_DOUBLE, X_test2);//SOMETHING WRONG 

    pDict = PyModule_GetDict(pModule);

    pArgs = PyTuple_New (1);
    PyTuple_SetItem (pArgs, 0, ArgsArray);

    pFunc = PyDict_GetItemString(pDict, FunctionName);

    if (pFunc && PyCallable_Check(pFunc))
    {

        pValue = PyObject_CallObject(pFunc, pArgs);//CRASHING HERE

        if (pValue != NULL) 
        {
            rows = PyArray_DIM(pValue, 0);
            cols = PyArray_DIM(pValue, 1);
            size = PyArray_SIZE(pValue);
            type = PyArray_TYPE(pValue);


            // get direct access to the array data
            //PyObject* m_obj;
            outArray = static_cast<const double*>(PyArray_DATA(pValue));


            for (row=0; row < rows; row++) 
            {
                ArrayItem = outArray[row];
                y_pred.push_back(ArrayItem);
            }

        }
        else 
        {
            y_pred.push_back(EMPTY_VAL);
        }
    }
    else 
    {
        PyErr_Print();
    }//pFunc && PyCallable_Check(pFunc)



}//(pModule!=NULL
else
{
    PyErr_SetString(PyExc_TypeError, "Cannot call function ?!");
    PyErr_Print();
}




Py_DECREF(pValue);
Py_DECREF(pFunc);

Py_DECREF(ArgsArray);  
Py_DECREF(pModule);
Py_DECREF(pName);


Py_Finalize (); 

最佳答案

您必须将数据复制到连续的内存块。为了表示二维数组,numpy 不使用指向一维数组的指针数组。 Numpy 希望数组存储在连续的内存块中,在(默认情况下)row major order 中。 .

如果您使用 PyArray_SimpleNew(...) 创建数组,numpy 会为您分配内存。您必须将 X_test2 复制到此数组,例如,使用 std::memcpystd::copy 在行上循环。

也就是说,改变这个:

ArgsArray = PyArray_SimpleNewFromData (2, Dims, NPY_DOUBLE, X_test2);//SOMETHING WRONG 

像这样:

// PyArray_SimpleNew allocates the memory needed for the array.
ArgsArray = PyArray_SimpleNew(2, Dims, NPY_DOUBLE);

// The pointer to the array data is accessed using PyArray_DATA()
double *p = (double *) PyArray_DATA(ArgsArray);

// Copy the data from the "array of arrays" to the contiguous numpy array.
for (int k = 0; k < NumRows; ++k) {
    memcpy(p, X_test2[k], sizeof(double) * NumInputs);
    p += NumInputs;
}

(看起来X_test2X_test的拷贝,所以你可能想修改上面的代码,直接从X_test复制到numpy 数组。)

关于python - 将二维 C 数组传递给 python numpy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27940848/

相关文章:

Python – Pandas : Get max value of last 5 days

c++ - istream_iterator 在读取字符时忽略 EOF (Ctrl+D)

c++ - C++ 映射可以包含指向我的任意类的指针吗?

arrays - fortran 中的方位角平均值

python - Python中链式赋值的现状

python - 可以在 PostgreSQL INSERT 语句中放置的最大 VALUES 数量是多少?

python - Django Rest 框架 authtoken 'module' 对象没有属性 'views'

c++ - 可以具有存储相互连续的实例的类似 vector 的容器?

c# - 如何在 C# 中连接两个数组?

我们可以将这个嵌套循环finding_indice 转换为递归吗?