[Numpy-discussion] Optimizing mean(axis=0) on a 3D array

Martin Spacek numpy at mspacek.mm.st
Sun Aug 27 07:28:03 CDT 2006

Tim Hochberg wrote:

> Here's an approach (mean_accumulate) that avoids making any copies of 
> the data. It runs almost 4x as fast as your approach (called baseline 
> here) on my box. Perhaps this will be useful:
> def mean_accumulate(data, indices):
>     result = np.zeros([32, 32], float)
>     for i in indices:
>         result += data[i]
>     result /= len(indices)
>     return result

Great! I got a roughly 9x speed improvement using take() in combination 
with this approach. Thanks Tim!

Here's what my code looks like now:

 >>> def mean_accum(data):
 >>>     result = np.zeros(data[0].shape, np.float64)
 >>>     for dataslice in data:
 >>>         result += dataslice
 >>>     result /= len(data)
 >>>     return result
 >>> # frameis are int64
 >>> frames = data.take(frameis.astype(np.int32), axis=0)
 >>> meanframe = mean_accum(frames)

I'm surprised that using a python for loop is faster than the built-in 
mean method. I suppose mean() can't perform the same in-place operations 
because in certain cases doing so would fail?


More information about the Numpy-discussion mailing list