[SciPy-User] down-sampling an array by averaging - vectorized form?

Jerome Kieffer Jerome.Kieffer@esrf...
Sat Feb 11 02:53:46 CST 2012


Hi,

Your needs are very close to "binning" (just devide the result by the number of input pixel in each output):

def binning(inputArray, binsize):
    """
    @param inputArray: input ndarray
    @param binsize: int or 2-tuple representing the size of the binning
    @return: binned input ndarray
    """
    inputSize = inputArray.shape
    outputSize = []
    assert(len(inputSize) == 2)
    if isinstance(binsize, int):
        binsize = (binsize, binsize)
    for i, j in zip(inputSize, binsize):
        assert(i % j == 0)
        outputSize.append(i // j)

    if numpy.array(binsize).prod() < 50:
        out = numpy.zeros(tuple(outputSize))
        for i in xrange(binsize[0]):
            for j in xrange(binsize[1]):
                out += inputArray[i::binsize[0], j::binsize[1]]
    else:
        temp = inputArray.copy()
        temp.shape = (outputSize[0], binsize[0], outputSize[1], binsize[1])
        out = temp.sum(axis=3).sum(axis=1)
    return out


This function implements 2 methods: 
- one faster for small binning based on a loop and a sum of all elements
- one for larger binning (8x8 and +) based on a reshape and two sum on two different axis

HTH
-- 
Jérôme Kieffer
Data analysis unit - ESRF


More information about the SciPy-User mailing list