[Numpy-discussion] indexing of arbitrary axis and arbitrary slice?
Sat Mar 16 11:49:13 CDT 2013
On 16 Mar 2013 16:41, "Chao YUE" <firstname.lastname@example.org> wrote:
> Dear all,
> Is there some way to index the numpy array by specifying arbitrary axis
and arbitrary slice, while
> not knowing the actual shape of the data?
> For example, I have a 3-dim data, data.shape = (3,4,5)
> Is there a way to retrieve data[:,0,:] by using something like
> by this way you don't have to know the actual shape of the array.
> for for 4-dim data, np.retrieve_data(data,axis=2,slice=0) will actually
I don't know of anything quite like that, but it's easy to fake it:
def retrieve_data(a, ax, idx):
full_idx = [slice(None)] * a.ndim
full_idx[ax] = idx
Or for the specific case where you do know the axis in advance, you just
don't know how many trailing axes there are, use
a[:, :, 0, ...]
and the ... will expand to represent the appropriate number of :'s.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion