summation along a non-fast axis

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? Does numpy do something like for i in range(a.shape[0]): for j in range(x.shape[1]): result[j] += a[i, j]

On Fri, 2019-01-11 at 12:32 -0800, Keith Goodman wrote:
"now" is since numpy 1.7 or so :).
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

On Fri, 2019-01-11 at 12:32 -0800, Keith Goodman wrote:
"now" is since numpy 1.7 or so :).
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
participants (2)
-
Keith Goodman
-
Sebastian Berg