简体   繁体   中英

apply numpy.histogram to multidimensional array

I want to apply numpy.histogram() to a multi-dimensional array along an axis.

Say, for example I have a 2D array and I want to apply histogram() along axis=1 .

Code:

import numpy

array = numpy.array([[0.6, 0.7, -0.3, 1.0, -0.8], [0.2, -1.0, -0.5, 0.5, 0.8], 
                    [0.25, 0.3, -0.1, -0.8, 1.0]])
bins = [-1.0, -0.5, 0, 0.5, 1.0, 1.0]
hist, bin_edges = numpy.histogram(array, bins)
print(hist)

Output:

[3 3 3 4 2]

Expected Output:

[[1 1 0 2 1],
 [1 1 1 2 0],
 [1 1 2 0 1]]

How can I get my expected output?

I tried to use the solution suggested in this post , but it doesn't get me to the expected output.

For nd cases, you can do this with np.histogram2d just by making a dummy x-axis ( i ):

def vec_hist(a, bins):
    i = np.repeat(np.arange(np.product(a.shape[:-1]), a.shape[-1]))
    return np.histogram2d(i, a.flatten(), (a.shape[0], bins)).reshape(a.shape[:-1], -1)

Output

vec_hist(array, bins)
Out[453]: 
(array([[ 1.,  1.,  0.,  2.,  1.],
        [ 1.,  1.,  1.,  2.,  0.],
        [ 1.,  1.,  2.,  0.,  1.]]),
 array([ 0.        ,  0.66666667,  1.33333333,  2.        ]),
 array([-1.       , -0.5      ,  0.       ,  0.5      ,  0.9999999,  1.       ]))

For histograms over arbitrary axis, you'll probably need to create i using np.meshgrid and np.ravel_multi_axis and then use that to reshape the resulting histogram.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM