简体   繁体   中英

Iterating over ndarray columns in C/C++

How would one get a view of a PyArrayObject* similar to the following python code?

# n-column array x
# d is the length of each column
print(x.shape)  # => (d, n)

by_column = [x[::,i] for i in range(x.shape[1])]

assert len(by_column) == n

print(by_column[n-1].shape)  # => (d,)

So far my code is this:

// my_array is a PyArrayObject* 
std::vector<PyArrayObject*> columns = {};

npy_intp* dims = my_array->dimensions;
npy_intp* strides = my_array->strides;

std::vector<int> shape = {};
for (int i = 0; &dims[i] != strides; i++){
    shape.push_back(dims[i]);
}

switch (shape.size()) {
    case 1: {
        // handle 1D array by simply iterating
    }
    case 2: {
        int columns = shape[1];
        // What now?
    }
}

I'm having trouble finding any reference to do this in C/C++ in both the documentation and the source code, could you give an example of how one would do this?

The C/C++ API for numpy seems really convoluted when compared to something like std::vector, and the documentation isn't very beginner-friendly either, so any references to easier guides would be appreciated too.

You should access the internal structure of PyArrayObject via the PyArray_XXX functions like PyArray_NDIM . To get the contents of a sequence, you use PyObject_GetItem with a tuple key, where in your use case the tuple will have a PySliceObject as the first element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM