[Numpy-discussion] Operation over multiple axes? (Or: Partial flattening?)

Hans Meine meine@informatik.uni-hamburg...
Tue Jul 29 07:52:13 CDT 2008


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080729/c50534dd/attachment.bin 


More information about the Numpy-discussion mailing list