Binning in Numpy

前端 未结 1 400
迷失自我
迷失自我 2021-01-14 09:25

I have an array A which I am trying to put into 10 bins. Here is what I\'ve done.

A = range(1,94)
hist = np.histogram(A, bins=10)
np.digitize(A, hist[1])


        
相关标签:
1条回答
  • 2021-01-14 09:55

    The output of np.histogram actually has 10 bins; the last (right-most) bin includes the greatest element because its right edge is inclusive (unlike for other bins).

    The np.digitize method doesn't make such an exception (since its purpose is different) so the largest element(s) of the list get placed into an extra bin. To get the bin assignments that are consistent with histogram, just clamp the output of digitize by the number of bins, using fmin.

    A = range(1,94)
    bin_count = 10
    hist = np.histogram(A, bins=bin_count)
    np.fmin(np.digitize(A, hist[1]), bin_count)
    

    Output:

    array([ 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,  2,
            2,  2,  3,  3,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,
            4,  4,  4,  5,  5,  5,  5,  5,  5,  5,  5,  5,  6,  6,  6,  6,  6,
            6,  6,  6,  6,  6,  7,  7,  7,  7,  7,  7,  7,  7,  7,  8,  8,  8,
            8,  8,  8,  8,  8,  8,  9,  9,  9,  9,  9,  9,  9,  9,  9, 10, 10,
           10, 10, 10, 10, 10, 10, 10, 10])
    
    0 讨论(0)
提交回复
热议问题