Numpy rebinning a 2D array

后端 未结 3 1561
孤街浪徒
孤街浪徒 2021-02-08 04:50

I am looking for a fast formulation to do a numerical binning of a 2D numpy array. By binning I mean calculate submatrix averages or cumulative values. For ex. x = numpy.arange(

3条回答
  •  梦毁少年i
    2021-02-08 05:37

    You can use a higher dimensional view of your array and take the average along the extra dimensions:

    In [12]: a = np.arange(36).reshape(6, 6)
    
    In [13]: a
    Out[13]: 
    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],
           [24, 25, 26, 27, 28, 29],
           [30, 31, 32, 33, 34, 35]])
    
    In [14]: a_view = a.reshape(3, 2, 3, 2)
    
    In [15]: a_view.mean(axis=3).mean(axis=1)
    Out[15]: 
    array([[  3.5,   5.5,   7.5],
           [ 15.5,  17.5,  19.5],
           [ 27.5,  29.5,  31.5]])
    

    In general, if you want bins of shape (a, b) for an array of (rows, cols), your reshaping of it should be .reshape(rows // a, a, cols // b, b). Note also that the order of the .mean is important, e.g. a_view.mean(axis=1).mean(axis=3) will raise an error, because a_view.mean(axis=1) only has three dimensions, although a_view.mean(axis=1).mean(axis=2) will work fine, but it makes it harder to understand what is going on.

    As is, the above code only works if you can fit an integer number of bins inside your array, i.e. if a divides rows and b divides cols. There are ways to deal with other cases, but you will have to define the behavior you want then.

提交回复
热议问题