Efficiently get indices of histogram bins in Python

前端 未结 5 1182
清歌不尽
清歌不尽 2020-12-13 19:12

Short Question

I have a large 10000x10000 elements image, which I bin into a few hundred different sectors/bins. I then need to perform some iterative calculation

5条回答
  •  醉梦人生
    2020-12-13 20:13

    You can halve the computation time by sorting the array first, then use np.searchsorted.

    vals = np.random.random(1e8)
    vals.sort()
    
    nbins = 100
    bins = np.linspace(0, 1, nbins+1)
    ind = np.digitize(vals, bins)
    
    results = [func(vals[np.searchsorted(ind,j,side='left'):
                         np.searchsorted(ind,j,side='right')])
               for j in range(1,nbins)]
    

    Using 1e8 as my test case, I go from 34 seconds of computation to about 17.

提交回复
热议问题