[Numpy-discussion] summation along a non-fast axis
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):
> for j in range(x.shape):
> 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):
for j in range(a.shape):
# 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.
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 833 bytes
Desc: This is a digitally signed message part
More information about the NumPy-Discussion