Binning of data along one axis in numpy

后端 未结 3 875
感情败类
感情败类 2021-01-02 01:56

I have a large two dimensional array arr which I would like to bin over the second axis using numpy. Because np.histogram flattens the array I\'m c

3条回答
  •  长发绾君心
    2021-01-02 02:21

    You could use np.apply_along_axis:

    x = np.array([range(20), range(1, 21), range(2, 22)])
    
    nbins = 2
    >>> np.apply_along_axis(lambda a: np.histogram(a, bins=nbins)[0], 1, x)
    array([[10, 10],
           [10, 10],
           [10, 10]])
    

    The main advantage (if any) is that it's slightly shorter, but I wouldn't expect much of a performance gain. It's possibly marginally more efficient in the assembly of the per-row results.

提交回复
热议问题