[Numpy-discussion] Operation over multiple axes? (Or: Partial flattening?)
Hans Meine
meine at informatik.uni-hamburg.de
Tue Jul 29 10:24:27 EDT 2008
On Dienstag 29 Juli 2008, Stéfan van der Walt wrote:
> > One way to achieve this is partial flattening, which I did like this:
> >
> > dat.reshape((numpy.prod(dat.shape[:3]), dat.shape[3])).sum(0)
> >
> > Is there a more elegant way to do this?
>
> That looks like a good way to do it. You can clean it up ever so slightly:
>
> x.reshape([-1, x.shape[-1]]).sum(axis=0)
Thanks, that looks more elegant indeed. I am not sure if I've read about -1
in shapes before. I assume it represents "the automatically determined rest"
and may only appear once? Should this be documented in the reshape
docstring?
Ciao, / / .o.
/--/ ..o
/ / ANS ooo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20080729/b42239ee/attachment.sig>
More information about the NumPy-Discussion
mailing list