Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

NumPy fancy indexing with provided output array

$
0
0

I have a two-dimensional array and index it with a pair of arrays (in fact my arrays are much larger, millions of elements):

a = np.array([[1, 2, 3], [4, 5, 6]])b = a[[0, 0, 0, 1], [0, 1, 2, 0]]

Indexing will allocate a new array. Is there a way to do this indexing with an output array provided?

I looked at np.take and np.choose, but it seems that they don't work with a pair of arrays. I managed to use np.take(..., out=buf) if I ravel the array and manually construct the 1-d instance, but it causes more memory accesses, and almost kills the improvement over eliminating allocation for the indexing result.


Viewing all articles
Browse latest Browse all 23131

Trending Articles