[Numpy-discussion] summing over more than one axis

Dave dave.hirschfeld@gmail....
Thu Aug 19 10:08:43 CDT 2010


greg whittier <gregwh <at> gmail.com> writes:
> 
> a = np.arange(27).reshape(3,3,3)
> 
> # sum over axis 1 and 2
> result = a.reshape((a.shape[0], a.shape[1]*a.shape[2])).sum(axis=1)
> 
> Is there a cleaner way to do this?  I'm sure I'm missing something obvious.
>

Only slightly more convenient is:

a.reshape(a.shape[0], -1).sum(-1)

HTH,
Dave




More information about the NumPy-Discussion mailing list