[Numpy-discussion] howto reduce along arbitrary axis

Chao YUE chaoyuejoy at gmail.com
Tue Mar 26 10:59:58 EDT 2013


Oh sorry, my fault...

here is the answer by Nathaniel Smith:

def retrieve_data(a, ax, idx):
    full_idx = [slice(None)] * a.ndim
    full_idx[ax] = idx
    return a[tuple(full_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.
probably you can sue something simlaer.

Chao

On Tue, Mar 26, 2013 at 3:33 PM, Neal Becker <ndbecker2 at gmail.com> wrote:

> Thank you, but do you also have an answer to this question?  I only see
> the question.
>
>
> On Tue, Mar 26, 2013 at 10:23 AM, Chao YUE <chaoyuejoy at gmail.com> wrote:
>
>> Hi Neal,
>>
>> I forward you this mail which I think might be of help to your question.
>>
>> Chao
>>
>> ---------- Forwarded message ----------
>> From: Chao YUE <chaoyuejoy at gmail.com>
>> Date: Sat, Mar 16, 2013 at 5:40 PM
>> Subject: indexing of arbitrary axis and arbitrary slice?
>> To: Discussion of Numerical Python <numpy-discussion at scipy.org>
>>
>>
>> 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
>> np.retrieve_data(data,axis=2,slice=0),
>> 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
>> be data[:,0,:,:]
>>
>> thanks in advance,
>>
>> Chao
>>
>> --
>>
>> ***********************************************************************************
>> Chao YUE
>> Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
>> UMR 1572 CEA-CNRS-UVSQ
>> Batiment 712 - Pe 119
>> 91191 GIF Sur YVETTE Cedex
>> Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
>>
>> ************************************************************************************
>>
>>
>>
>> --
>>
>> ***********************************************************************************
>> Chao YUE
>> Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
>> UMR 1572 CEA-CNRS-UVSQ
>> Batiment 712 - Pe 119
>> 91191 GIF Sur YVETTE Cedex
>> Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
>>
>> ************************************************************************************
>>
>
>


-- 
***********************************************************************************
Chao YUE
Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
UMR 1572 CEA-CNRS-UVSQ
Batiment 712 - Pe 119
91191 GIF Sur YVETTE Cedex
Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
************************************************************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130326/5fc32344/attachment.html>


More information about the NumPy-Discussion mailing list