[Numpy-discussion] rebin

Russell E Owen rowen at u.washington.edu
Mon Aug 30 09:07:07 CDT 2004


At 9:14 AM -0400 2004-08-30, Perry Greenfield wrote:
>On Aug 27, 2004, at 8:34 PM, Russell E. Owen wrote:
>
>>  Any suggestions on an efficient means to bin a 2-d array?
>>  ...
>>  For example, to 2x2 bin a two-dimensional image, one would:
>>  average (0,0), (0,1), (1,0), (1,1) to form (0,0)
>>  average (0,2), (0,3), (1,2), (1,3) to form (0,1)
>>  ...
>>
>Note that a boxcar smoothing costs no more than doing the above averaging.
>So in numarray, you could do the following:
>
>from numarray.convolve import boxcar
>b = boxcar(a, (n,n))
>rebinnedarray = b[::n,::n].copy()
>
>or something like this (I haven't tried to figure out the correct offset
>for the slice) where one wants to rebin by a factor of n in both dimensions.
>We should probably add a built in function to do this.

Thanks! Great suggestion!

I think the polished version (for nxm binning) is:

from numarray.convolve import boxcar
b = boxcar(a, (n,m), mode='constant', cval=0)
rebinnedarray = b[n//2::n,m//2::m].copy()

A rebin function would be handy since using boxcar is a bit tricky.

-- Russell




More information about the Numpy-discussion mailing list