# [Numpy-discussion] summing over more than one axis

Jeremiah jerlark386@gmail....
Thu Aug 19 16:08:57 CDT 2010

```Word size.
imagine 2.123456789 vs  22.12345679

the more that can stored to the right of the decimal, the more precise.
That larger the number is on the left, the less that can stored on the right.

On Thu, Aug 19, 2010 at 4:03 PM, John Salvatier
<jsalvati@u.washington.edu> wrote:
> Precise in what sense? Numerical accuracy? If so, why is that?
>
> On Thu, Aug 19, 2010 at 12:13 PM, <josef.pktd@gmail.com> wrote:
>>
>> On Thu, Aug 19, 2010 at 11:29 AM, Joe Harrington <jh@physics.ucf.edu>
>> wrote:
>> > On Thu, 19 Aug 2010 09:06:32 -0500, G?khan Sever <gokhansever@gmail.com>
>> > wrote:
>> >
>> >>On Thu, Aug 19, 2010 at 9:01 AM, greg whittier <gregwh@gmail.com> wrote:
>> >>
>> >>> I frequently deal with 3D data and would like to sum (or find the
>> >>> mean, etc.) over the last two axes.  I.e. sum a[i,j,k] over j and k.
>> >>> I find using .sum() really convenient for 2d arrays but end up
>> >>> reshaping 2d arrays to do this.  I know there has to be a more
>> >>> convenient way.  Here's what I'm doing
>> >>>
>> >>> 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.
>> >>>
>> >>> Thanks,
>> >>> Greg
>> >>>
>> >>
>> >>Using two sums
>> >>
>> >>np.sum(np.sum(a, axis=-2), axis=1)
>> >
>> > Be careful.  This works for sums, but not for operations like median;
>> > the median of the row medians may not be the global median.  So, you
>> > need to do the medians in one step.  I'm not aware of a method cleaner
>> > than manually reshaping first.  There may also be speed reasons to do
>> > things in one step.  But, two steps may look cleaner in code.
>>
>> I think, two .sums() are the most accurate, if precision matters. One
>> big summation is often not very precise.
>>
>> Josef
>>
>>
>> >
>> > --jh--
>> > _______________________________________________
>> > 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
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
```