[Numpy-discussion] rebin

Perry Greenfield perry at stsci.edu
Mon Aug 30 06:15:08 EDT 2004

On Aug 27, 2004, at 8:34 PM, Russell E. Owen wrote:

> Any suggestions on an efficient means to bin a 2-d array? REBIN is the 
> function I'm trying to mimic. Binning allows one to combine sets of 
> pixels from
> one array to form a new array that is smaller by a given factor along 
> each
> dimension.
> To nxm bin a 2-dimensional array, one averages (or sums or ?) each nxm 
> block of
> entries from the input image to form the corresponding entry of the 
> output
> image.
> 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)
> ...
> In case it helps, in my immediate case I'm binning a boolean array (a 
> mask) and
> thus can live with almost any kind of combination.
Note that a boxcar smoothing costs no more than doing the above 
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 
We should probably add a built in function to do this.


More information about the NumPy-Discussion mailing list