How to bin a 2D array in numpy?

前端 未结 3 948
清歌不尽
清歌不尽 2020-12-30 12:17

I\'m new to numpy and I have a 2D array of objects that I need to bin into a smaller matrix and then get a count of the number of objects in each bin to make a heatmap. I fo

3条回答
  •  不思量自难忘°
    2020-12-30 12:50

    Another solution is to have a look at the binArray function on the comments here: Binning a numpy array

    To use your example :

    data_matrix = numpy.ndarray((500,500),dtype=float)
    binned_data = binArray(data_matrix, 0, 10, 10, np.sum)
    binned_data = binArray(binned_data, 1, 10, 10, np.sum)
    

    The result sum all square of size 10x10 in data_matrix (of size 500x500) to obtain a single value per square in binned_data (of size 50x50).

    Hope this help !

提交回复
热议问题