Operation over multiple axes? (Or: Partial flattening?)
Hi, with a multidimensional array (say, 4-dimensional), I often want to project this onto one single dimension, i.e.. let "dat" be a 4D array, I am interested in dat.sum(0).sum(0).sum(0) # equals dat.sum(2).sum(1).sum(0) However, creating intermediate results looks more expensive than necessary; I would actually like to say dat.sum((0,1,2)) 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? Ciao, / / .o. /--/ ..o / / ANS ooo
2008/7/29 Hans Meine <meine@informatik.uni-hamburg.de>:
with a multidimensional array (say, 4-dimensional), I often want to project this onto one single dimension, i.e.. let "dat" be a 4D array, I am interested in
dat.sum(0).sum(0).sum(0) # equals dat.sum(2).sum(1).sum(0)
However, creating intermediate results looks more expensive than necessary; I would actually like to say
dat.sum((0,1,2))
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) Cheers Stéfan
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
On Tue, Jul 29, 2008 at 09:24, Hans Meine <meine@informatik.uni-hamburg.de> wrote:
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?
Yes, yes, and yes. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
2008/7/29 Hans Meine <meine@informatik.uni-hamburg.de>:
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?
That's correct, and yes -- it should! Would you like to document it yourself? If you register on http://sd-2116.dedibox.fr/pydocweb I'll give you editor's access. Regards Stéfan
participants (3)
-
Hans Meine -
Robert Kern -
Stéfan van der Walt