How to bin a 2D array in numpy?

前端 未结 3 946
清歌不尽
清歌不尽 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 13:01

    At first I was also going to suggest that you use np.histogram2d rather than reinventing the wheel, but then I realized that it would be overkill to use that and would need some hacking still.

    If I understand correctly, you just want to sum over submatrices of your input. That's pretty easy to brute force: going over your output submatrix and summing up each subblock of your input:

    import numpy as np
    
    def submatsum(data,n,m):
        # return a matrix of shape (n,m)
        bs = data.shape[0]//n,data.shape[1]//m  # blocksize averaged over
        return np.reshape(np.array([np.sum(data[k1*bs[0]:(k1+1)*bs[0],k2*bs[1]:(k2+1)*bs[1]]) for k1 in range(n) for k2 in range(m)]),(n,m))
    
    # set up dummy data
    N,M = 4,6
    data_matrix = np.reshape(np.arange(N*M),(N,M))
    
    # set up size of 2x3-reduced matrix, assume congruity
    n,m = N//2,M//3
    reduced_matrix = submatsum(data_matrix,n,m)
    
    # check output
    print(data_matrix)
    print(reduced_matrix)
    

    This prints

    print(data_matrix)
    [[ 0  1  2  3  4  5]
     [ 6  7  8  9 10 11]
     [12 13 14 15 16 17]
     [18 19 20 21 22 23]]
    
    print(reduced_matrix)
    [[ 24  42]
     [ 96 114]]
    

    which is indeed the result for summing up submatrices of shape (2,3).

    Note that I'm using // for integer division to make sure it's python3-compatible, but in case of python2 you can just use / for division (due to the numbers involved being integers).

提交回复
热议问题