I want to extract parts of an numpy ndarray based on arrays of index positions for some of the dimensions. Let me show this on an example
dummy = np.random.rand(5,2,100) X = np.array([[0,1],[4,1],[2,0]])
dummy is the original ndarray with dimensionality 5x2x100. This dimensionality is arbitrary, it could as well be 5x2x4x100.
X is a matrix of index values, here
X[:,0] are the indices of the first dimension of
X[:,1] those of the second dimension. The number of columns in X is always the number of dimensions in
dummy minus 1.
I want to extract an ndarray of the following form for this example
[ dummy[0,1,:], dummy[4,1,:], dummy[2,0,:] ]
If the number of dimensions in
dummy were fixed, this could just be done by
dummy[X[:,0],X[:,1],:] . Sadly the dimensionality can be different, e.g.
dummy could be a 5x2x4x6x100 ndarray and
X correspondingly would then be 3×4 . My attempts at dealing with it have not yielded the desired result.
dummy[X,:]yields a 3x2x2x100 ndarray for this example same as
- Iteratively reducing
dummyby doing something like
dummy = dummy[X[:,i],:]with
ian iterator over the number of columns of
Xalso does not reduce the ndarray in the example past 3x2x100
I have a feeling that this should be pretty simple with numpy indexing, but I guess my search for a solution was missing the right terms for this.
Does anyone have a solution to this?
This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.