[Numpy-discussion] summation along a non-fast axis

Sebastian Berg sebastian at sipsolutions.net
Fri Jan 11 16:24:48 EST 2019

On Fri, 2019-01-11 at 12:32 -0800, Keith Goodman wrote:
> I remember back when a.sum(axis=0) was much slower than a.sum(axis=1)
> for something like a=np.ones((1000, 1000)). But now it runs in about
> the same time. How does numpy do it?

"now" is since numpy 1.7 or so :).

> Does numpy do something like
> for i in range(a.shape[0]):
>     for j in range(x.shape[1]):
>         result[j] += a[i, j]

Yeah, numpy reorders the operation. Maybe a bit closer to what happens
is to write it down with the result as a 2D array (as happens with
keepdims), since internally that is how numpy operates on it (although
it may optimize the `i*0` away):

for i in range(a.shape[0]):
    for j in range(a.shape[1]):
        # If sum is along axis 0:
        result[i*0, j] += a[i, j]

Since it doesn't matter which of the loop is the innermost one, the
machinery is capable of reordering them. I think it learned it with 1.7
(because that added a lot), but maybe it was even earlier.

- Sebastian

> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20190111/c82da709/attachment.sig>

More information about the NumPy-Discussion mailing list