[Numpy-discussion] easy way to collapse the last two dimension of an array?

Chao YUE chaoyuejoy@gmail....
Mon Jun 3 05:20:23 CDT 2013


Thanks to the two of you!!

Chao

On Mon, Jun 3, 2013 at 11:53 AM, Sebastian Berg
<sebastian@sipsolutions.net>wrote:

> On Mon, 2013-06-03 at 10:44 +0200, Chao YUE wrote:
> > Dear all,
> >
> > I have an array with 4 dim:
> >
> > In [24]: dd.shape
> > Out[24]: (12, 13, 120, 170)
> >
> > I would like to collapse the last two dim for applying np.sum(axis=-1)
> >
> If you use Numpy >= 1.7. you can also just use dd.sum(axis=(-1,-2))
>
> - Sebastian
>
>
> > In [25]: dd.reshape(12,13,-1).shape
> > Out[25]: (12, 13, 20400)
> >
> > is there a more general way to do this? something like
> > In [21]: dd.reshape(*dd.shape[0:2],-1).shape
> > ------------------------------------------------------------
> >    File "<ipython console>", line 1
> > SyntaxError: only named arguments may follow *expression (<ipython
> > console>, line 1)
> >
> > thanks a lot for any hints,
> >
> > cheers,
> >
> > 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
> >
> >
> ************************************************************************************
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion@scipy.org
> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
***********************************************************************************
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.scipy.org/pipermail/numpy-discussion/attachments/20130603/354c90c0/attachment.html 


More information about the NumPy-Discussion mailing list