[Python] resize with averaging or rebin a numpy 2d array

Discussão em 'Python' iniciado por Stack, Fevereiro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to reimplement in python an IDL function:

    http://star.pst.qub.ac.uk/idl/REBIN.html

    which downsizes by an integer factor a 2d array by averaging.

    For example:

    >>> a=np.arange(24).reshape((4,6))
    >>> a
    array([[ 0, 1, 2, 3, 4, 5],
    [ 6, 7, 8, 9, 10, 11],
    [12, 13, 14, 15, 16, 17],
    [18, 19, 20, 21, 22, 23]])


    I would like to resize it to (2,3) by taking the mean of the relevant samples, the expected output would be:

    >>> b = rebin(a, (2, 3))
    >>> b
    array([[ 3.5, 5.5, 7.5],
    [ 15.5, 17.5, 19.5]])


    i.e. b[0,0] = np.mean(a[:2,:2]), b[0,1] = np.mean(a[:2,2:4]) and so on.

    I believe I should reshape to a 4 dimensional array and then take the mean on the correct slice, but could not figure out the algorithm. Would you have any hint?

    Continue reading...

Compartilhe esta Página