[Numpy-discussion] Any numpy trick for my problem ?

Charles R Harris charlesr.harris@gmail....
Wed Oct 15 05:56:52 CDT 2008


On Wed, Oct 15, 2008 at 4:47 AM, Uwe Schmitt <rocksportrocker@googlemail.com
> wrote:

> Hi,
>
> I got a matrix of 2100 lines, and I want to calculate  blockwise mean
> vectors.
> Each block consists of 10 consecutive rows.
>
> My code looks like this:
>
>   rv = []
>   for i in range(0, 2100, 10):
>       rv.append( mean(matrix[i:i+10], axis=0))
>
>   return array(rv)
>
> Is there a more elegant and may be faster method to perform this
> calculation ?
>

Something like

In [1]: M = np.random.ranf((40,5))

In [2]: M.reshape(4,10,5).mean(axis=1)
Out[2]:
array([[ 0.57979278,  0.50013352,  0.66783389,  0.4009187 ,  0.36379445],
       [ 0.46938844,  0.34449102,  0.56419189,  0.49134703,  0.61380198],
       [ 0.5644788 ,  0.61734034,  0.3656104 ,  0.63147275,  0.46319345],
       [ 0.56556899,  0.59012606,  0.39691084,  0.26566127,  0.57107896]])

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20081015/8abcbe94/attachment.html 


More information about the Numpy-discussion mailing list